Skip to content

Commit df30acf

Browse files
authored
Merge pull request #614 from soot-oss/fix/emptybodyinterceptors
apply Bodyinterceptors II (zwei)
2 parents 4a6bcbd + 8e2fad1 commit df30acf

File tree

9 files changed

+51
-54
lines changed

9 files changed

+51
-54
lines changed

sootup.core/src/main/java/sootup/core/inputlocation/ClassLoadingOptions.java

-5
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@
3636
*/
3737
public interface ClassLoadingOptions {
3838

39-
/** To get the stored Value */
40-
default <T> T getValue(ClassLoadingOptionKey key) {
41-
return null;
42-
}
43-
4439
/**
4540
* The interceptors are executed in order on each loaded method body, allowing it to be inspected
4641
* and manipulated.

sootup.core/src/main/java/sootup/core/views/View.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ public interface View<T extends SootClass> {
5454
Project getProject();
5555

5656
@Nonnull
57-
List<BodyInterceptor> getBodyInterceptors(AnalysisInputLocation<T> inputLocation);
58-
59-
@Nonnull
60-
List<BodyInterceptor> getBodyInterceptors();
57+
List<BodyInterceptor> getBodyInterceptors(AnalysisInputLocation inputLocation);
6158

6259
/** Return all classes in the view. */
6360
@Nonnull

sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/AsmClassSource.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ public Collection<? extends SootMethod> resolveMethods() throws ResolveException
116116
AsmMethodSource asmClassClassSourceContent = (AsmMethodSource) methodSource;
117117
asmClassClassSourceContent.setDeclaringClass(classSignature);
118118

119-
List<ClassType> exceptions = new ArrayList<>();
120-
exceptions.addAll(AsmUtil.asmIdToSignature(methodSource.exceptions));
119+
List<ClassType> exceptions =
120+
new ArrayList<>(AsmUtil.asmIdToSignature(methodSource.exceptions));
121121

122122
String methodName = methodSource.name;
123123
EnumSet<Modifier> modifiers = AsmUtil.getModifiers(methodSource.access);

sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/AsmJavaClassProvider.java

+18-6
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ public AsmJavaClassProvider(@Nonnull View<?> view) {
5151

5252
@Override
5353
public AbstractClassSource<JavaSootClass> createClassSource(
54-
AnalysisInputLocation<? extends SootClass<?>> srcNamespace,
54+
AnalysisInputLocation<? extends SootClass<?>> analysisInputLocation,
5555
Path sourcePath,
5656
ClassType classType) {
57-
SootClassNode classNode = new SootClassNode();
57+
SootClassNode classNode = new SootClassNode(analysisInputLocation);
5858

5959
try {
6060
AsmUtil.initAsmClassSource(sourcePath, classNode);
@@ -70,10 +70,11 @@ public AbstractClassSource<JavaSootClass> createClassSource(
7070
"Can not create ClassSource from a module info descriptor!", sourcePath);
7171
} else {
7272
if (klassType instanceof AnnotationType) {
73-
return new AsmAnnotationClassSource(srcNamespace, sourcePath, klassType, classNode);
73+
return new AsmAnnotationClassSource(
74+
analysisInputLocation, sourcePath, klassType, classNode);
7475
}
7576

76-
return new AsmClassSource(srcNamespace, sourcePath, klassType, classNode);
77+
return new AsmClassSource(analysisInputLocation, sourcePath, klassType, classNode);
7778
}
7879
}
7980

@@ -85,8 +86,11 @@ public FileType getHandledFileType() {
8586

8687
class SootClassNode extends ClassNode {
8788

88-
SootClassNode() {
89+
private final AnalysisInputLocation<? extends SootClass<?>> analysisInputLocation;
90+
91+
SootClassNode(AnalysisInputLocation<? extends SootClass<?>> analysisInputLocation) {
8992
super(AsmUtil.SUPPORTED_ASM_OPCODE);
93+
this.analysisInputLocation = analysisInputLocation;
9094
}
9195

9296
@Override
@@ -98,7 +102,15 @@ public MethodVisitor visitMethod(
98102
@Nonnull String signature,
99103
@Nonnull String[] exceptions) {
100104

101-
AsmMethodSource mn = new AsmMethodSource(access, name, desc, signature, exceptions, view);
105+
AsmMethodSource mn =
106+
new AsmMethodSource(
107+
access,
108+
name,
109+
desc,
110+
signature,
111+
exceptions,
112+
view,
113+
view.getBodyInterceptors(analysisInputLocation));
102114
methods.add(mn);
103115
return mn;
104116
}

sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/AsmMethodSource.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ public class AsmMethodSource extends JSRInlinerAdapter implements BodySource {
129129
@Nullable private JavaClassType declaringClass;
130130

131131
private final View<?> view;
132+
private final List<BodyInterceptor> bodyInterceptors;
132133

133134
@Nonnull private final Set<LabelNode> inlineExceptionLabels = new HashSet<>();
134135

@@ -156,8 +157,10 @@ public class AsmMethodSource extends JSRInlinerAdapter implements BodySource {
156157
@Nonnull String desc,
157158
@Nonnull String signature,
158159
@Nonnull String[] exceptions,
159-
@Nonnull View<?> view) {
160+
View<?> view,
161+
@Nonnull List<BodyInterceptor> bodyInterceptors) {
160162
super(AsmUtil.SUPPORTED_ASM_OPCODE, null, access, name, desc, signature, exceptions);
163+
this.bodyInterceptors = bodyInterceptors;
161164
this.view = view;
162165
}
163166

@@ -244,7 +247,7 @@ public Body resolveBody(@Nonnull Iterable<Modifier> modifierIt) {
244247

245248
bodyBuilder.setMethodSignature(lazyMethodSignature.get());
246249

247-
for (BodyInterceptor bodyInterceptor : view.getBodyInterceptors()) {
250+
for (BodyInterceptor bodyInterceptor : bodyInterceptors) {
248251
try {
249252
bodyInterceptor.interceptBody(bodyBuilder, view);
250253
} catch (Exception e) {

sootup.java.bytecode/src/main/java/sootup/java/bytecode/interceptors/BytecodeBodyInterceptors.java

+14-13
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,20 @@
2929
/** Built-in sets of {@link BodyInterceptor}s for the bytecode frontend */
3030
public enum BytecodeBodyInterceptors {
3131
Default(
32-
// new CastAndReturnInliner(),
33-
// new DuplicateCatchAllTrapRemover(),
34-
// new UnreachableCodeEliminator(),
35-
// // new LocalSplitter(),
36-
// new Aggregator(),
37-
// //new UnusedLocalEliminator(),
38-
// new TypeAssigner(),
39-
// new LocalNameStandardizer(),
40-
// new CopyPropagator(),
41-
// new DeadAssignmentEliminator(),
42-
// new NopEliminator(),
43-
// new ConditionalBranchFolder(),
44-
// new EmptySwitchEliminator());
32+
/*
33+
new CastAndReturnInliner(),
34+
new UnreachableCodeEliminator(),
35+
// // new LocalSplitter(),
36+
// new Aggregator(),
37+
new TypeAssigner(),
38+
// new LocalNameStandardizer(),
39+
new CopyPropagator(),
40+
new DeadAssignmentEliminator(),
41+
new ConditionalBranchFolder(),
42+
new EmptySwitchEliminator(),
43+
new NopEliminator(),
44+
new UnusedLocalEliminator()
45+
*/
4546
);
4647

4748
@Nonnull private final List<BodyInterceptor> bodyInterceptors;

sootup.java.core/src/main/java/sootup/java/core/views/JavaView.java

+2-13
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
*/
2424

2525
import java.util.Collection;
26-
import java.util.Collections;
2726
import java.util.List;
2827
import java.util.Optional;
2928
import java.util.function.Function;
@@ -102,18 +101,8 @@ public JavaView(
102101

103102
@Nonnull
104103
@Override
105-
public List<BodyInterceptor> getBodyInterceptors(AnalysisInputLocation<JavaSootClass> clazz) {
106-
return this.classLoadingOptionsSpecifier.apply(clazz) != null
107-
? this.classLoadingOptionsSpecifier.apply(clazz).getBodyInterceptors()
108-
: getBodyInterceptors();
109-
}
110-
111-
@Nonnull
112-
@Override
113-
public List<BodyInterceptor> getBodyInterceptors() {
114-
// TODO add default interceptors from
115-
// sootup.java.bytecode.interceptors.BytecodeBodyInterceptors;
116-
return Collections.emptyList();
104+
public List<BodyInterceptor> getBodyInterceptors(AnalysisInputLocation clazz) {
105+
return this.classLoadingOptionsSpecifier.apply(clazz).getBodyInterceptors();
117106
}
118107

119108
public void configBodyInterceptors(

sootup.jimple.parser/src/main/java/sootup/jimple/parser/JimpleAnalysisInputLocation.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ List<AbstractClassSource<? extends AbstractClass<?>>> walkDirectory(
9898
@Nonnull
9999
public Collection<? extends SootClassSource<T>> getClassSources(@Nonnull View<?> view) {
100100
return walkDirectory(
101-
path, view.getIdentifierFactory(), new JimpleClassProvider(view.getBodyInterceptors()));
101+
path, view.getIdentifierFactory(), new JimpleClassProvider(view.getBodyInterceptors(this)));
102102
}
103103

104104
@Override
105105
@Nonnull
106106
public Optional<? extends SootClassSource<T>> getClassSource(
107107
@Nonnull ClassType type, @Nonnull View<?> view) {
108108
final JimpleClassProvider<T> classProvider =
109-
new JimpleClassProvider<>(view.getBodyInterceptors());
109+
new JimpleClassProvider<>(view.getBodyInterceptors(this));
110110

111111
final String ext = classProvider.getHandledFileType().toString().toLowerCase();
112112

sootup.jimple.parser/src/main/java/sootup/jimple/parser/JimpleView.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ public class JimpleView extends AbstractView<SootClass<?>> {
4242

4343
/** Creates a new instance of the {@link JavaView} class. */
4444
public JimpleView(@Nonnull JimpleProject project) {
45-
this(project, new FullCacheProvider<>(), analysisInputLocation -> null);
45+
this(
46+
project,
47+
new FullCacheProvider<>(),
48+
analysisInputLocation -> EmptyClassLoadingOptions.Default);
4649
}
4750

4851
public JimpleView(
@@ -78,15 +81,12 @@ public JimpleView(
7881

7982
@Nonnull
8083
@Override
81-
public List<BodyInterceptor> getBodyInterceptors(
82-
AnalysisInputLocation<SootClass<?>> inputLocation) {
83-
return classLoadingOptionsSpecifier.apply(inputLocation) != null
84-
? classLoadingOptionsSpecifier.apply(inputLocation).getBodyInterceptors()
85-
: getBodyInterceptors();
84+
public List<BodyInterceptor> getBodyInterceptors(AnalysisInputLocation inputLocation) {
85+
return classLoadingOptionsSpecifier.apply(inputLocation).getBodyInterceptors();
8686
}
8787

8888
@Nonnull
89-
public List<BodyInterceptor> getBodyInterceptors() {
89+
private List<BodyInterceptor> getBodyInterceptors() {
9090
return Collections.emptyList();
9191
}
9292

0 commit comments

Comments
 (0)