diff --git a/docs/migrating.md b/docs/migrating.md index dc2eca2059c..88d6ea981b1 100644 --- a/docs/migrating.md +++ b/docs/migrating.md @@ -1,5 +1,19 @@ # Migration Help +### Version 3.0.2 +- Identifiers are hash-consed: the `IdentifierFactory` hands out the same instance for equal `ClassType`s, `MethodSignature`s, `FieldSignature`s, their sub signatures and `PackageName`s. They can therefore be compared with `==` and used as keys of an `IdentityHashMap`. +- As a consequence the constructors of these classes are not public anymore. Obtain them from the `IdentifierFactory` of the `View` that the identifiers are used with: +```java +ClassType classType = view.getIdentifierFactory().getClassType("example.HelloWorld"); +``` +- `JavaIdentifierFactory.getInstance()` and `JavaModuleIdentifierFactory.getInstance()` were removed - the factory belongs to a `View` now. Use `view.getIdentifierFactory()`; if there is no `View` at hand (e.g. while setting one up), instantiate it with `new JavaIdentifierFactory()`. +- `JavaModuleIdentifierFactory.getInstance(module)` was replaced by the instance method `forModule(module)`: +```java +JavaModuleIdentifierFactory factory = + (JavaModuleIdentifierFactory) moduleView.getIdentifierFactory(); +JavaModuleIdentifierFactory factoryOfModule = factory.forModule("mymodule"); +``` + ### Version 1.3.0 - The Typehierarchy API is now returning `Stream` instead of `Collection`. The simplest fix to have the same behaviour as before would be to collect the Stream on your own ( e.g. via `.collect(Collectors.toList())` ). - Default BytecodeBodyinterceptors are enabled to improve Jimple. To mitigate that adapt the List of BodyInterceptors to your needs. diff --git a/docs/whatsnew.md b/docs/whatsnew.md index 5e11d5eb067..3b675fdd51d 100644 --- a/docs/whatsnew.md +++ b/docs/whatsnew.md @@ -23,6 +23,24 @@ Singletons offer a single view of a single program version, which makes it impos SootUp does not make use of singletons such the `Scene` class in the old Soot any more. It enables analyzing multiple programs simultaneously. +### Hash-Consed Identifiers + +Identifiers such as `ClassType`, `MethodSignature`, `FieldSignature`, their sub signatures and `PackageName` are hash-consed: +for a given name the `IdentifierFactory` always returns the very same instance. +This keeps the memory footprint of an analysis low and allows comparing identifiers with `==` as well as using them in an `IdentityHashMap`. +Because this invariant only holds if every identifier is created in one place, their constructors are not public - identifiers are created via the `IdentifierFactory` that belongs to the `View`: + +!!! example + + ```java + IdentifierFactory identifierFactory = view.getIdentifierFactory(); + + ClassType classType = identifierFactory.getClassType("example.HelloWorld"); + MethodSignature methodSignature = + identifierFactory.getMethodSignature( + classType, "main", "void", Collections.singletonList("java.lang.String[]")); + ``` + ### Immutable by Design SootUp has been designed with the goal of immutability in mind. diff --git a/pom.xml b/pom.xml index b458f47dec3..be14c73d58a 100644 --- a/pom.xml +++ b/pom.xml @@ -88,6 +88,10 @@ 3.12.0 0.11.0 3.2.8 + + 8192M + 4 true ${basedir}/target 6.1.3 @@ -313,10 +317,19 @@ ${maven-surefire-plugin.version} - -Xmx8192M ${surefireArgLine} + -Xmx${surefire.maxHeap} ${surefireArgLine} false - all - true + + classes + false + ${surefire.threadCount} + false diff --git a/sootup.analysis.interprocedural/src/main/java/sootup/analysis/interprocedural/icfg/ICFGDotExporter.java b/sootup.analysis.interprocedural/src/main/java/sootup/analysis/interprocedural/icfg/ICFGDotExporter.java index 5e79c7c9335..590abe0dcdc 100644 --- a/sootup.analysis.interprocedural/src/main/java/sootup/analysis/interprocedural/icfg/ICFGDotExporter.java +++ b/sootup.analysis.interprocedural/src/main/java/sootup/analysis/interprocedural/icfg/ICFGDotExporter.java @@ -25,6 +25,7 @@ import java.util.*; import java.util.stream.Collectors; import sootup.callgraph.CallGraph; +import sootup.core.IdentifierFactory; import sootup.core.graph.BasicBlock; import sootup.core.graph.ControlFlowGraph; import sootup.core.jimple.common.expr.JNewExpr; @@ -32,7 +33,6 @@ import sootup.core.jimple.common.stmt.Stmt; import sootup.core.model.SootMethod; import sootup.core.signatures.MethodSignature; -import sootup.core.signatures.MethodSubSignature; import sootup.core.types.VoidType; import sootup.core.util.DotExporter; import sootup.core.views.View; @@ -141,11 +141,12 @@ public static void connectEdgesToSubClasses( methodSignatureInSubClass.forEach( subclassmethodSignature -> { Optional method = view.getMethod(target); + IdentifierFactory identifierFactory = view.getIdentifierFactory(); MethodSignature initMethod = - new MethodSignature( + identifierFactory.getMethodSignature( subclassmethodSignature.getDeclClassType(), - new MethodSubSignature( - "", Collections.emptyList(), VoidType.getInstance())); + identifierFactory.getMethodSubSignature( + "", VoidType.getInstance(), Collections.emptyList())); if (method.isPresent() && !subclassmethodSignature.toString().equals(initMethod.toString())) { if (method.get().hasBody()) { diff --git a/sootup.analysis.interprocedural/src/test/java/sootup/analysis/interprocedural/icfg/ICFGDotExporterTest.java b/sootup.analysis.interprocedural/src/test/java/sootup/analysis/interprocedural/icfg/ICFGDotExporterTest.java index f2dd2584166..b3866041ac5 100644 --- a/sootup.analysis.interprocedural/src/test/java/sootup/analysis/interprocedural/icfg/ICFGDotExporterTest.java +++ b/sootup.analysis.interprocedural/src/test/java/sootup/analysis/interprocedural/icfg/ICFGDotExporterTest.java @@ -316,7 +316,7 @@ public static String[] addStatement(String[] statements, String statement) { } private MutableBlockControlFlowGraph createControlFlowGraph() { - JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory factory = new JavaIdentifierFactory(); JavaClassType intType = factory.getClassType("int"); final MutableBlockControlFlowGraph graph = new MutableBlockControlFlowGraph(); Local l3 = JavaJimple.newLocal("l3", intType); diff --git a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/Util/DexUtil.java b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/Util/DexUtil.java index 0595f27d5d5..5d9539d4ad4 100644 --- a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/Util/DexUtil.java +++ b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/Util/DexUtil.java @@ -28,17 +28,17 @@ import org.jf.dexlib2.iface.value.EncodedValue; import org.jspecify.annotations.NonNull; import sootup.apk.frontend.main.AndroidVersionInfo; +import sootup.core.IdentifierFactory; import sootup.core.types.*; import sootup.core.views.View; import sootup.java.core.AnnotationUsage; -import sootup.java.core.JavaIdentifierFactory; -import sootup.java.core.types.JavaClassType; public class DexUtil { private static AndroidVersionInfo androidVersionInfo; - public static Type toSootType(String typeDescriptor, int pos) { + public static Type toSootType( + String typeDescriptor, int pos, @NonNull IdentifierFactory identifierFactory) { Type type = null; char typeDesignator = typeDescriptor.charAt(pos); switch (typeDesignator) { @@ -70,13 +70,13 @@ public static Type toSootType(String typeDescriptor, int pos) { if (isByteCodeClassName(typeDescriptor)) { typeDescriptor = dottedClassName(typeDescriptor); } - type = getClassTypeFromClassName(typeDescriptor); + type = getClassTypeFromClassName(typeDescriptor, identifierFactory); break; case 'V': // void type = VoidType.getInstance(); break; case '[': // array - Type sootType = toSootType(typeDescriptor, pos + 1); + Type sootType = toSootType(typeDescriptor, pos + 1, identifierFactory); if (sootType != null) { type = Type.createArrayType(sootType, 1); } @@ -135,15 +135,16 @@ public static boolean isByteCodeClassName(String className) { && ((className.indexOf('/') != -1 || className.indexOf('.') == -1))); } - public static ClassType getClassTypeFromClassName(String name) { + public static ClassType getClassTypeFromClassName( + String name, @NonNull IdentifierFactory identifierFactory) { if (name.startsWith("[")) { name = "java.lang.Object"; } else if (isByteCodeClassName(name)) { name = dottedClassName(name); } - JavaClassType javaClassType; + ClassType javaClassType; try { - javaClassType = JavaIdentifierFactory.getInstance().getClassType(name); + javaClassType = identifierFactory.getClassType(name); } catch (Exception exception) { System.out.println("Exception when substring with className " + name); throw new RuntimeException(); diff --git a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/dexpler/DexClassSource.java b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/dexpler/DexClassSource.java index 94197c2d3b9..14d374de27a 100644 --- a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/dexpler/DexClassSource.java +++ b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/dexpler/DexClassSource.java @@ -198,7 +198,10 @@ protected List convertAnnotation(Set anno for (Annotation annotation : annotations) { for (AnnotationElement element : annotation.getElements()) { String name = element.getName(); - paramMap.put(name, convertAnnotationValue(element.getValue().getValueType())); + paramMap.put( + name, + convertAnnotationValue( + element.getValue().getValueType(), getView().getIdentifierFactory())); } ClassType at = getView() @@ -209,12 +212,14 @@ protected List convertAnnotation(Set anno return annotationUsage; } - private static Object convertAnnotationValue(Object annotationValue) { + private static Object convertAnnotationValue( + Object annotationValue, IdentifierFactory identifierFactory) { if (annotationValue instanceof EncodedValue) { - ClassConstant classConstant = JavaJimple.newClassConstant(annotationValue.toString()); - return ConstantUtil.fromObject(classConstant); + ClassConstant classConstant = + JavaJimple.newClassConstant(annotationValue.toString(), identifierFactory); + return ConstantUtil.fromObject(classConstant, identifierFactory); } - return ConstantUtil.fromObject(annotationValue); + return ConstantUtil.fromObject(annotationValue, identifierFactory); } private static Set resolveFields( @@ -225,7 +230,7 @@ private static Set resolveFields( .map( field -> { String fieldName = field.getName(); - Type fieldType = DexUtil.toSootType(field.getType(), 0); + Type fieldType = DexUtil.toSootType(field.getType(), 0, signatureFactory); FieldSignature fieldSignature = signatureFactory.getFieldSignature(fieldName, classSignature, fieldType); EnumSet modifiers = diff --git a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/dexpler/DexMethod.java b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/dexpler/DexMethod.java index b3be5013c9e..9bff13910a4 100644 --- a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/dexpler/DexMethod.java +++ b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/dexpler/DexMethod.java @@ -57,14 +57,16 @@ public JavaSootMethod makeSootMethod( if (Modifier.isAbstract(modifierFlags) || Modifier.isNative(modifierFlags)) { List parameters = method.getParameters().stream() - .map(methodParameter -> DexUtil.toSootType(methodParameter.getType(), 0)) + .map( + methodParameter -> + DexUtil.toSootType(methodParameter.getType(), 0, view.getIdentifierFactory())) .collect(Collectors.toList()); MethodSignature methodSignature = view.getIdentifierFactory() .getMethodSignature( declaringclassType, method.getName(), - DexUtil.toSootType(method.getReturnType(), 0), + DexUtil.toSootType(method.getReturnType(), 0, view.getIdentifierFactory()), parameters); DexMethodSource dexMethodSource = new DexMethodSource( @@ -76,7 +78,7 @@ public JavaSootMethod makeSootMethod( view); return dexMethodSource.makeSootMethod(); } else { - DexBody dexBody = new DexBody(method, dexEntry, declaringclassType); + DexBody dexBody = new DexBody(method, dexEntry, declaringclassType, view); return dexBody.makeSootMethod(method, declaringclassType, bodyInterceptors, view); } } diff --git a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/CheckCastInstruction.java b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/CheckCastInstruction.java index 6a0cdfca8dd..41d7c12a83f 100644 --- a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/CheckCastInstruction.java +++ b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/CheckCastInstruction.java @@ -46,7 +46,10 @@ public void jimplify(DexBody body) { Local castValue = body.getRegisterLocal(checkCastInstr.getRegisterA()); Type checkCastType = - DexUtil.toSootType(((TypeReference) checkCastInstr.getReference()).getType(), 0); + DexUtil.toSootType( + ((TypeReference) checkCastInstr.getReference()).getType(), + 0, + body.getIdentifierFactory()); JCastExpr castExpr = Jimple.newCastExpr(castValue, checkCastType); // generate "x = (Type) x" diff --git a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/ConstClassInstruction.java b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/ConstClassInstruction.java index a7ecd6f2353..fc2f548b4da 100644 --- a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/ConstClassInstruction.java +++ b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/ConstClassInstruction.java @@ -46,7 +46,8 @@ public void jimplify(DexBody body) { ReferenceInstruction constClass = (ReferenceInstruction) this.instruction; TypeReference tidi = (TypeReference) (constClass.getReference()); - ClassConstant classConstant = JavaJimple.newClassConstant(tidi.getType()); + ClassConstant classConstant = + JavaJimple.newClassConstant(tidi.getType(), body.getIdentifierFactory()); int dest = ((OneRegisterInstruction) instruction).getRegisterA(); JAssignStmt jAssignStmt = diff --git a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/ConstStringInstruction.java b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/ConstStringInstruction.java index 6a793e490f0..ee2870eff57 100644 --- a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/ConstStringInstruction.java +++ b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/ConstStringInstruction.java @@ -32,7 +32,6 @@ import sootup.core.jimple.basic.SimpleStmtPositionInfo; import sootup.core.jimple.common.constant.StringConstant; import sootup.core.jimple.common.stmt.JAssignStmt; -import sootup.java.core.JavaIdentifierFactory; public class ConstStringInstruction extends DexLibAbstractInstruction { public ConstStringInstruction(Instruction instruction, int codeAddress) { @@ -54,7 +53,7 @@ public void jimplify(DexBody body) { "Expected Instruction21c or Instruction31c but got neither."); } StringConstant stringConstant = - new StringConstant(s, JavaIdentifierFactory.getInstance().getType("java.lang.String")); + new StringConstant(s, body.getIdentifierFactory().getType("java.lang.String")); JAssignStmt jAssignStmt = Jimple.newAssignStmt( body.getRegisterLocal(dest), stringConstant, new SimpleStmtPositionInfo(lineNumber)); diff --git a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/FieldInstruction.java b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/FieldInstruction.java index 80b1c53481e..faf676d9257 100644 --- a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/FieldInstruction.java +++ b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/FieldInstruction.java @@ -25,6 +25,7 @@ import org.jf.dexlib2.iface.instruction.Instruction; import org.jf.dexlib2.iface.reference.FieldReference; import sootup.apk.frontend.Util.DexUtil; +import sootup.core.IdentifierFactory; import sootup.core.jimple.Jimple; import sootup.core.jimple.basic.SimpleStmtPositionInfo; import sootup.core.jimple.common.LValue; @@ -47,13 +48,14 @@ public FieldInstruction(Instruction instruction, int codeAddress) { super(instruction, codeAddress); } - private JFieldRef getSootFieldRef(FieldReference fieldReference, boolean isStatic) { + private JFieldRef getSootFieldRef( + FieldReference fieldReference, boolean isStatic, IdentifierFactory identifierFactory) { String className = DexUtil.dottedClassName(fieldReference.getDefiningClass()); FieldSignature fieldSignature = - new FieldSignature( - DexUtil.getClassTypeFromClassName(className), + identifierFactory.getFieldSignature( fieldReference.getName(), - DexUtil.toSootType(fieldReference.getType(), 0)); + DexUtil.getClassTypeFromClassName(className, identifierFactory), + DexUtil.toSootType(fieldReference.getType(), 0, identifierFactory)); if (isStatic) { return new JStaticFieldRef(fieldSignature); } else { @@ -68,20 +70,23 @@ private JFieldRef getSootFieldRef(FieldReference fieldReference, boolean isStati * Return a static SootFieldRef for a dexlib FieldReference. * * @param fref the dexlib FieldReference. + * @param identifierFactory the factory that creates the field signature * @return the JFieldRef for the given field Reference */ - protected JFieldRef getStaticSootFieldRef(FieldReference fref) { - return getSootFieldRef(fref, true); + protected JFieldRef getStaticSootFieldRef( + FieldReference fref, IdentifierFactory identifierFactory) { + return getSootFieldRef(fref, true, identifierFactory); } /** * Return a SootFieldRef for a dexlib FieldReference. * - * @return the JFieldRef for the given field Reference * @param fref the dexlib FieldReference. + * @param identifierFactory the factory that creates the field signature + * @return the JFieldRef for the given field Reference */ - protected JFieldRef getSootFieldRef(FieldReference fref) { - return getSootFieldRef(fref, false); + protected JFieldRef getSootFieldRef(FieldReference fref, IdentifierFactory identifierFactory) { + return getSootFieldRef(fref, false, identifierFactory); } /** diff --git a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/FillArrayDataInstruction.java b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/FillArrayDataInstruction.java index 8d5f1e865bc..77122bfe5ae 100644 --- a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/FillArrayDataInstruction.java +++ b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/FillArrayDataInstruction.java @@ -109,7 +109,9 @@ private NumericConstant getArrayElement(Number element, DexBody dexBody, int arr ArrayType arrayType = (ArrayType) DexUtil.toSootType( - ((TypeReference) instruction22c.getReference()).getType(), 0); + ((TypeReference) instruction22c.getReference()).getType(), + 0, + dexBody.getIdentifierFactory()); elementType = arrayType.getElementType(); break Outer; } diff --git a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/FilledNewArrayInstruction.java b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/FilledNewArrayInstruction.java index 444bc2bfd22..06f64ccd7b4 100644 --- a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/FilledNewArrayInstruction.java +++ b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/FilledNewArrayInstruction.java @@ -36,7 +36,6 @@ import sootup.core.jimple.common.stmt.JAssignStmt; import sootup.core.types.ArrayType; import sootup.core.types.Type; -import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.language.JavaJimple; public class FilledNewArrayInstruction extends FilledArrayInstruction { @@ -52,12 +51,16 @@ public void jimplify(DexBody body) { }; int usedRegister = filledNewArrayInstr.getRegisterCount(); - Type t = DexUtil.toSootType(((TypeReference) filledNewArrayInstr.getReference()).getType(), 0); + Type t = + DexUtil.toSootType( + ((TypeReference) filledNewArrayInstr.getReference()).getType(), + 0, + body.getIdentifierFactory()); // NewArrayExpr needs the ElementType as it increases the array dimension by 1 Type arrayType = ((ArrayType) t).getElementType(); JNewArrayExpr arrayExpr = JavaJimple.newNewArrayExpr( - arrayType, IntConstant.getInstance(usedRegister), JavaIdentifierFactory.getInstance()); + arrayType, IntConstant.getInstance(usedRegister), body.getIdentifierFactory()); // new local generated intentional, will be moved to real register by MoveResult Local arrayLocal = body.getStoreResultLocal(); JAssignStmt assign = diff --git a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/FilledNewArrayRangeInstruction.java b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/FilledNewArrayRangeInstruction.java index 18ef537741d..fda65f806d9 100644 --- a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/FilledNewArrayRangeInstruction.java +++ b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/FilledNewArrayRangeInstruction.java @@ -36,7 +36,6 @@ import sootup.core.jimple.common.stmt.JAssignStmt; import sootup.core.types.ArrayType; import sootup.core.types.Type; -import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.language.JavaJimple; public class FilledNewArrayRangeInstruction extends FilledArrayInstruction { @@ -46,12 +45,16 @@ public void jimplify(DexBody body) { Instruction3rc filledNewArrayInstr = (Instruction3rc) instruction; int usedRegister = filledNewArrayInstr.getRegisterCount(); - Type t = DexUtil.toSootType(((TypeReference) filledNewArrayInstr.getReference()).getType(), 0); + Type t = + DexUtil.toSootType( + ((TypeReference) filledNewArrayInstr.getReference()).getType(), + 0, + body.getIdentifierFactory()); // NewArrayExpr needs the ElementType as it increases the array dimension by 1 Type arrayType = ((ArrayType) t).getElementType(); JNewArrayExpr arrayExpr = JavaJimple.newNewArrayExpr( - arrayType, IntConstant.getInstance(usedRegister), JavaIdentifierFactory.getInstance()); + arrayType, IntConstant.getInstance(usedRegister), body.getIdentifierFactory()); Local arrayLocal = body.getStoreResultLocal(); JAssignStmt assignStmt = Jimple.newAssignStmt(arrayLocal, arrayExpr, new SimpleStmtPositionInfo(lineNumber)); diff --git a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/IgetInstruction.java b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/IgetInstruction.java index 157f3092ea6..3cd6697af55 100644 --- a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/IgetInstruction.java +++ b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/IgetInstruction.java @@ -40,7 +40,8 @@ public void jimplify(DexBody body) { FieldReference f = (FieldReference) ((ReferenceInstruction) instruction).getReference(); JInstanceFieldRef jInstanceFieldRef = Jimple.newInstanceFieldRef( - body.getRegisterLocal(object), getSootFieldRef(f).getFieldSignature()); + body.getRegisterLocal(object), + getSootFieldRef(f, body.getIdentifierFactory()).getFieldSignature()); JAssignStmt assignStmt = getAssignStmt(body.getRegisterLocal(dest), jInstanceFieldRef); setStmt(assignStmt); body.add(assignStmt); diff --git a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/InstanceOfInstruction.java b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/InstanceOfInstruction.java index 2cb1b1b4c81..58876dc0a07 100644 --- a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/InstanceOfInstruction.java +++ b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/InstanceOfInstruction.java @@ -39,7 +39,9 @@ public void jimplify(DexBody body) { Instruction22c i = (Instruction22c) instruction; int dest = i.getRegisterA(); int source = i.getRegisterB(); - Type sootType = DexUtil.toSootType(((TypeReference) i.getReference()).getType(), 0); + Type sootType = + DexUtil.toSootType( + ((TypeReference) i.getReference()).getType(), 0, body.getIdentifierFactory()); JInstanceOfExpr jInstanceOfExpr = Jimple.newInstanceOfExpr(body.getRegisterLocal(source), sootType); diff --git a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/IputInstruction.java b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/IputInstruction.java index 941a318507c..1908db2519c 100644 --- a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/IputInstruction.java +++ b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/IputInstruction.java @@ -41,7 +41,8 @@ public void jimplify(DexBody body) { FieldReference f = (FieldReference) ((ReferenceInstruction) instruction).getReference(); JInstanceFieldRef jInstanceFieldRef = Jimple.newInstanceFieldRef( - body.getRegisterLocal(object), getSootFieldRef(f).getFieldSignature()); + body.getRegisterLocal(object), + getSootFieldRef(f, body.getIdentifierFactory()).getFieldSignature()); Local sourceValue = body.getRegisterLocal(source); JAssignStmt jAssignStmt = getAssignStmt(jInstanceFieldRef, sourceValue); setStmt(jAssignStmt); diff --git a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/MethodInvocationInstruction.java b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/MethodInvocationInstruction.java index 8ef898f11e7..f2d7a2d2906 100644 --- a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/MethodInvocationInstruction.java +++ b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/MethodInvocationInstruction.java @@ -33,6 +33,7 @@ import org.jf.dexlib2.iface.reference.MethodReference; import sootup.apk.frontend.Util.DexUtil; import sootup.apk.frontend.main.DexBody; +import sootup.core.IdentifierFactory; import sootup.core.jimple.Jimple; import sootup.core.jimple.basic.SimpleStmtPositionInfo; import sootup.core.jimple.common.Immediate; @@ -41,7 +42,6 @@ import sootup.core.jimple.common.stmt.JAssignStmt; import sootup.core.jimple.common.stmt.JInvokeStmt; import sootup.core.signatures.MethodSignature; -import sootup.core.signatures.MethodSubSignature; import sootup.core.types.Type; public abstract class MethodInvocationInstruction extends DexLibAbstractInstruction @@ -71,35 +71,37 @@ public MethodInvocationInstruction(Instruction instruction, int codeAddress) { } protected void jimplifySpecial(DexBody body) { + IdentifierFactory identifierFactory = body.getIdentifierFactory(); MethodReference item = (MethodReference) ((ReferenceInstruction) instruction).getReference(); List parameters = buildParameters(body, item.getParameterTypes(), false); invocation = Jimple.newSpecialInvokeExpr( parameters.get(0), - new MethodSignature( - DexUtil.getClassTypeFromClassName(item.getDefiningClass()), - new MethodSubSignature( - item.getName(), - convertParameterTypes(item.getParameterTypes()), - DexUtil.toSootType(item.getReturnType(), 0))), + identifierFactory.getMethodSignature( + DexUtil.getClassTypeFromClassName(item.getDefiningClass(), identifierFactory), + item.getName(), + DexUtil.toSootType(item.getReturnType(), 0, identifierFactory), + convertParameterTypes(item.getParameterTypes(), identifierFactory)), buildArgs(parameters.subList(1, parameters.size()))); body.setDanglingInstruction(this); } protected void jimplifyStatic(DexBody body) { + IdentifierFactory identifierFactory = body.getIdentifierFactory(); MethodReference item = (MethodReference) ((ReferenceInstruction) instruction).getReference(); List parameters = buildParameters(body, item.getParameterTypes(), true); MethodSignature methodSignature = - new MethodSignature( - DexUtil.getClassTypeFromClassName(item.getDefiningClass()), + identifierFactory.getMethodSignature( + DexUtil.getClassTypeFromClassName(item.getDefiningClass(), identifierFactory), item.getName(), - convertParameterTypes(item.getParameterTypes()), - DexUtil.toSootType(item.getReturnType(), 0)); + DexUtil.toSootType(item.getReturnType(), 0, identifierFactory), + convertParameterTypes(item.getParameterTypes(), identifierFactory)); invocation = Jimple.newStaticInvokeExpr(methodSignature, buildArgs(parameters)); body.setDanglingInstruction(this); } protected void jimplifyVirtual(DexBody body) { + IdentifierFactory identifierFactory = body.getIdentifierFactory(); MethodReference item = (MethodReference) ((ReferenceInstruction) instruction).getReference(); List parameters = buildParameters(body, item.getParameterTypes(), false); // TODO check isIntertface by someother way @@ -108,11 +110,11 @@ protected void jimplifyVirtual(DexBody body) { // return; // } MethodSignature methodSignature = - new MethodSignature( - DexUtil.getClassTypeFromClassName(item.getDefiningClass()), + identifierFactory.getMethodSignature( + DexUtil.getClassTypeFromClassName(item.getDefiningClass(), identifierFactory), item.getName(), - convertParameterTypes(item.getParameterTypes()), - DexUtil.toSootType(item.getReturnType(), 0)); + DexUtil.toSootType(item.getReturnType(), 0, identifierFactory), + convertParameterTypes(item.getParameterTypes(), identifierFactory)); invocation = Jimple.newVirtualInvokeExpr( parameters.get(0), @@ -122,6 +124,7 @@ protected void jimplifyVirtual(DexBody body) { } protected void jimplifyInterface(DexBody body) { + IdentifierFactory identifierFactory = body.getIdentifierFactory(); MethodReference item = (MethodReference) ((ReferenceInstruction) instruction).getReference(); List parameters = buildParameters(body, item.getParameterTypes(), false); // TODO check isIntertface by someother way @@ -130,11 +133,11 @@ protected void jimplifyInterface(DexBody body) { // return; // } MethodSignature methodSignature = - new MethodSignature( - DexUtil.getClassTypeFromClassName(item.getDefiningClass()), + identifierFactory.getMethodSignature( + DexUtil.getClassTypeFromClassName(item.getDefiningClass(), identifierFactory), item.getName(), - convertParameterTypes(item.getParameterTypes()), - DexUtil.toSootType(item.getReturnType(), 0)); + DexUtil.toSootType(item.getReturnType(), 0, identifierFactory), + convertParameterTypes(item.getParameterTypes(), identifierFactory)); invocation = Jimple.newInterfaceInvokeExpr( parameters.get(0), @@ -143,11 +146,12 @@ protected void jimplifyInterface(DexBody body) { body.setDanglingInstruction(this); } - protected List convertParameterTypes(List paramTypes) { + protected List convertParameterTypes( + List paramTypes, IdentifierFactory identifierFactory) { List parameterTypes = new ArrayList(); if (paramTypes != null) { for (CharSequence type : paramTypes) { - parameterTypes.add(DexUtil.toSootType(type.toString(), 0)); + parameterTypes.add(DexUtil.toSootType(type.toString(), 0, identifierFactory)); } } return parameterTypes; diff --git a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/MoveExceptionInstruction.java b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/MoveExceptionInstruction.java index d553dc0d68d..e3460ee9227 100644 --- a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/MoveExceptionInstruction.java +++ b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/MoveExceptionInstruction.java @@ -42,7 +42,7 @@ public class MoveExceptionInstruction extends DexLibAbstractInstruction { public void jimplify(DexBody body) { int dest = ((OneRegisterInstruction) instruction).getRegisterA(); Local l = body.getRegisterLocal(dest); - JCaughtExceptionRef ref = JavaJimple.newCaughtExceptionRef(); + JCaughtExceptionRef ref = JavaJimple.newCaughtExceptionRef(body.getIdentifierFactory()); stmtToRetype = Jimple.newIdentityStmt(l, ref, new SimpleStmtPositionInfo(lineNumber)); setStmt(stmtToRetype); body.add(stmtToRetype); diff --git a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/NewArrayInstruction.java b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/NewArrayInstruction.java index a78f65df0b6..cd7f1c784a0 100644 --- a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/NewArrayInstruction.java +++ b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/NewArrayInstruction.java @@ -34,7 +34,6 @@ import sootup.core.jimple.common.stmt.JAssignStmt; import sootup.core.types.ArrayType; import sootup.core.types.Type; -import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.language.JavaJimple; public class NewArrayInstruction extends DexLibAbstractInstruction { @@ -49,12 +48,14 @@ public void jimplify(DexBody body) { int dest = newArray.getRegisterA(); Local size = body.getRegisterLocal(newArray.getRegisterB()); - Type t = DexUtil.toSootType(((TypeReference) newArray.getReference()).getType(), 0); + Type t = + DexUtil.toSootType( + ((TypeReference) newArray.getReference()).getType(), 0, body.getIdentifierFactory()); // NewArrayExpr needs the ElementType as it increases the array dimension by 1 Type elementType = ((ArrayType) t).getElementType(); JNewArrayExpr jNewArrayExpr = - JavaJimple.newNewArrayExpr(elementType, size, JavaIdentifierFactory.getInstance()); + JavaJimple.newNewArrayExpr(elementType, size, body.getIdentifierFactory()); Local l = body.getRegisterLocal(dest); JAssignStmt assign = diff --git a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/NewInstanceInstruction.java b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/NewInstanceInstruction.java index 97d59bbb257..53b346d8de3 100644 --- a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/NewInstanceInstruction.java +++ b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/NewInstanceInstruction.java @@ -43,7 +43,7 @@ public void jimplify(DexBody body) { Instruction21c i = (Instruction21c) instruction; int dest = i.getRegisterA(); String className = DexUtil.dottedClassName(((TypeReference) (i.getReference())).toString()); - ClassType classType = DexUtil.getClassTypeFromClassName(className); + ClassType classType = DexUtil.getClassTypeFromClassName(className, body.getIdentifierFactory()); JNewExpr jNewExpr = Jimple.newNewExpr(classType); JAssignStmt jAssignStmt = Jimple.newAssignStmt( diff --git a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/SgetInstruction.java b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/SgetInstruction.java index cf848ee541c..26f4e69ff2e 100644 --- a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/SgetInstruction.java +++ b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/SgetInstruction.java @@ -37,7 +37,9 @@ public class SgetInstruction extends FieldInstruction { public void jimplify(DexBody body) { int dest = ((OneRegisterInstruction) instruction).getRegisterA(); FieldReference f = (FieldReference) ((ReferenceInstruction) instruction).getReference(); - JStaticFieldRef r = Jimple.newStaticFieldRef(getStaticSootFieldRef(f).getFieldSignature()); + JStaticFieldRef r = + Jimple.newStaticFieldRef( + getStaticSootFieldRef(f, body.getIdentifierFactory()).getFieldSignature()); JAssignStmt assign = Jimple.newAssignStmt( body.getRegisterLocal(dest), r, new SimpleStmtPositionInfo(lineNumber)); diff --git a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/SputInstruction.java b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/SputInstruction.java index 507e6b030a9..d0ea79b565f 100644 --- a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/SputInstruction.java +++ b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/instruction/SputInstruction.java @@ -38,7 +38,8 @@ public void jimplify(DexBody body) { int source = ((OneRegisterInstruction) instruction).getRegisterA(); FieldReference f = (FieldReference) ((ReferenceInstruction) instruction).getReference(); JStaticFieldRef instanceField = - Jimple.newStaticFieldRef(getStaticSootFieldRef(f).getFieldSignature()); + Jimple.newStaticFieldRef( + getStaticSootFieldRef(f, body.getIdentifierFactory()).getFieldSignature()); Local sourceValue = body.getRegisterLocal(source); JAssignStmt assign = getAssignStmt(instanceField, sourceValue); setStmt(assign); diff --git a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/main/DexBody.java b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/main/DexBody.java index 7689ac7c916..2c7d24c1874 100644 --- a/sootup.apk.frontend/src/main/java/sootup/apk/frontend/main/DexBody.java +++ b/sootup.apk.frontend/src/main/java/sootup/apk/frontend/main/DexBody.java @@ -34,12 +34,14 @@ import org.jf.dexlib2.immutable.debug.ImmutableRestartLocal; import org.jf.dexlib2.immutable.debug.ImmutableStartLocal; import org.jf.dexlib2.util.MethodUtil; +import org.jspecify.annotations.NonNull; import org.jspecify.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import sootup.apk.frontend.Util.DexUtil; import sootup.apk.frontend.dexpler.DexMethodSource; import sootup.apk.frontend.instruction.*; +import sootup.core.IdentifierFactory; import sootup.core.graph.MutableBlockControlFlowGraph; import sootup.core.interceptor.BodyInterceptor; import sootup.core.jimple.Jimple; @@ -55,7 +57,6 @@ import sootup.core.types.Type; import sootup.core.types.UnknownType; import sootup.core.views.View; -import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.JavaSootMethod; import sootup.java.core.language.JavaJimple; import sootup.java.core.types.JavaClassType; @@ -114,7 +115,7 @@ public RegDbgEntry(int sa, int ea, int reg, String nam, String ty, String sig) { this.endAddress = ea; this.register = reg; this.name = nam; - this.type = DexUtil.toSootType(ty, 0); + this.type = DexUtil.toSootType(ty, 0, identifierFactory); this.signature = sig; } } @@ -129,7 +130,17 @@ public void addDeferredJimplification(DeferableInstruction i) { private final ArrayListMultimap localDebugs; - public DexBody(Method method, MultiDexContainer.DexEntry dexEntry, ClassType classType) { + @NonNull private final IdentifierFactory identifierFactory; + + /** The {@link IdentifierFactory} of the view this body is converted for. */ + @NonNull + public IdentifierFactory getIdentifierFactory() { + return identifierFactory; + } + + public DexBody( + Method method, MultiDexContainer.DexEntry dexEntry, ClassType classType, @NonNull View view) { + this.identifierFactory = view.getIdentifierFactory(); MethodImplementation code = method.getImplementation(); if (code == null) { throw new RuntimeException("error: no code for method " + method.getName()); @@ -142,7 +153,7 @@ public DexBody(Method method, MultiDexContainer.DexEntry dexEntry, ClassType cla parameterTypes = new ArrayList(); for (MethodParameter param : method.getParameters()) { parameterNames.add(param.getName()); - parameterTypes.add(DexUtil.toSootType(param.getType(), 0)); + parameterTypes.add(DexUtil.toSootType(param.getType(), 0, identifierFactory)); } takenLocalNames = new HashSet<>(); @@ -333,7 +344,7 @@ public JavaSootMethod makeSootMethod( .getMethodSignature( classType, method.getName(), - DexUtil.toSootType(method.getReturnType(), 0), + DexUtil.toSootType(method.getReturnType(), 0, identifierFactory), parameterTypes); Map> branchingStmtListMap = convertMultimap(branchingMap); Set blockBegin = new HashSet<>(); @@ -712,11 +723,10 @@ private void addTraps() { exceptionType = exceptionType + "$" + exceptionTypeList.size(); } exceptionTypeList.add(exceptionType); - Type t = DexUtil.toSootType(exceptionType, 0); + Type t = DexUtil.toSootType(exceptionType, 0, identifierFactory); // exceptions can only be of ReferenceType if (t instanceof JavaClassType) { - JavaIdentifierFactory identifierFactory = JavaIdentifierFactory.getInstance(); - JavaClassType type = identifierFactory.getClassType(((JavaClassType) t).getClassName()); + ClassType type = identifierFactory.getClassType(((JavaClassType) t).getClassName()); DexLibAbstractInstruction instruction = instructionAtAddress(handler.getHandlerCodeAddress()); if (!(instruction instanceof MoveExceptionInstruction)) { @@ -732,7 +742,7 @@ private void addTraps() { Stmt caughtStmt = Jimple.newIdentityStmt( local, - JavaJimple.newCaughtExceptionRef(), + JavaJimple.newCaughtExceptionRef(identifierFactory), StmtPositionInfo.getNoStmtPositionInfo()); insertBefore(caughtStmt, instruction.getStmt()); handlerStmt = caughtStmt; diff --git a/sootup.apk.frontend/src/test/java/sootup/apk/frontend/ApkToDexTest.java b/sootup.apk.frontend/src/test/java/sootup/apk/frontend/ApkToDexTest.java index aac6b128d9e..819a6e4193c 100644 --- a/sootup.apk.frontend/src/test/java/sootup/apk/frontend/ApkToDexTest.java +++ b/sootup.apk.frontend/src/test/java/sootup/apk/frontend/ApkToDexTest.java @@ -115,7 +115,7 @@ public void getParametersOfAbstractMethods() { .filter(javaSootMethod -> javaSootMethod.getName().equals(interestedMethodName)) .toList(); assert abstractMethods.size() == 1; - Type sootType = DexUtil.toSootType("Ljava/lang/CharSequence;", 0); + Type sootType = DexUtil.toSootType("Ljava/lang/CharSequence;", 0, view.getIdentifierFactory()); Type paramType = abstractMethods.get(0).getParameterTypes().get(0); assert paramType.equals(sootType); } diff --git a/sootup.apk.frontend/src/test/java/sootup/apk/frontend/instruction/FieldInstructionTest.java b/sootup.apk.frontend/src/test/java/sootup/apk/frontend/instruction/FieldInstructionTest.java index 4bd5fb387c3..2a7e01f4ce3 100644 --- a/sootup.apk.frontend/src/test/java/sootup/apk/frontend/instruction/FieldInstructionTest.java +++ b/sootup.apk.frontend/src/test/java/sootup/apk/frontend/instruction/FieldInstructionTest.java @@ -21,6 +21,7 @@ import sootup.core.jimple.common.ref.JInstanceFieldRef; import sootup.core.jimple.common.ref.JStaticFieldRef; import sootup.core.jimple.common.stmt.JAssignStmt; +import sootup.java.core.JavaIdentifierFactory; @ExtendWith(MockitoExtension.class) public class FieldInstructionTest { @@ -36,7 +37,10 @@ public class FieldInstructionTest { @Mock private Local localC; @BeforeEach - public void setUp() {} + public void setUp() { + // the instructions name their field's class and type through the body's factory + lenient().when(mockBody.getIdentifierFactory()).thenReturn(new JavaIdentifierFactory()); + } @Test public void testIputInstruction() { diff --git a/sootup.callgraph/src/main/java/sootup/callgraph/AbstractCallGraphAlgorithm.java b/sootup.callgraph/src/main/java/sootup/callgraph/AbstractCallGraphAlgorithm.java index 623e55ced71..ff6adec7874 100644 --- a/sootup.callgraph/src/main/java/sootup/callgraph/AbstractCallGraphAlgorithm.java +++ b/sootup.callgraph/src/main/java/sootup/callgraph/AbstractCallGraphAlgorithm.java @@ -516,11 +516,12 @@ protected void implicitStartRunCall( continue; } MethodSignature implicitRunMethodSig = - new MethodSignature( - methodSig.getDeclClassType(), - "run", - methodSig.getParameterTypes(), - methodSig.getType()); + view.getIdentifierFactory() + .getMethodSignature( + methodSig.getDeclClassType(), + "run", + methodSig.getType(), + methodSig.getParameterTypes()); JVirtualInvokeExpr runInvokeExpr = sourceMethodInvokeExpr.asJVirtualInvokeExpr().withMethodSignature(implicitRunMethodSig); InvokableStmt runInvokableStmt = new JInvokeStmt(runInvokeExpr, getNoStmtPositionInfo()); diff --git a/sootup.callgraph/src/test/java/sootup/callgraph/CallGraphAlgorithmTest.java b/sootup.callgraph/src/test/java/sootup/callgraph/CallGraphAlgorithmTest.java index 9a4717629cd..d473215a2a4 100644 --- a/sootup.callgraph/src/test/java/sootup/callgraph/CallGraphAlgorithmTest.java +++ b/sootup.callgraph/src/test/java/sootup/callgraph/CallGraphAlgorithmTest.java @@ -39,8 +39,7 @@ public void testAddClass() { "void", Collections.emptyList()); - JavaClassType newClass = - new JavaClassType("AdderA", identifierFactory.getPackageName("update.operation.cg")); + JavaClassType newClass = identifierFactory.getClassType("AdderA", "update.operation.cg"); CallGraph newCallGraph = algorithm.addClass(cg, newClass); assertEquals(0, cg.callsTo(mainMethodSignature).size()); diff --git a/sootup.callgraph/src/test/java/sootup/callgraph/CallGraphTest.java b/sootup.callgraph/src/test/java/sootup/callgraph/CallGraphTest.java index 3bbcf5e1d08..7b0fc85c4e5 100644 --- a/sootup.callgraph/src/test/java/sootup/callgraph/CallGraphTest.java +++ b/sootup.callgraph/src/test/java/sootup/callgraph/CallGraphTest.java @@ -30,7 +30,7 @@ public abstract class CallGraphTest { protected AbstractCallGraphAlgorithm algorithm; - protected JavaIdentifierFactory identifierFactory = JavaIdentifierFactory.getInstance(); + protected JavaIdentifierFactory identifierFactory = new JavaIdentifierFactory(); protected JavaClassType mainClassSignature; protected MethodSignature mainMethodSignature; protected JavaView view; diff --git a/sootup.callgraph/src/test/java/sootup/callgraph/InstantiateClassValueVisitorTest.java b/sootup.callgraph/src/test/java/sootup/callgraph/InstantiateClassValueVisitorTest.java index 30d15ced0a5..109bf136483 100644 --- a/sootup.callgraph/src/test/java/sootup/callgraph/InstantiateClassValueVisitorTest.java +++ b/sootup.callgraph/src/test/java/sootup/callgraph/InstantiateClassValueVisitorTest.java @@ -57,12 +57,12 @@ import sootup.core.jimple.common.ref.JThisRef; import sootup.core.signatures.FieldSignature; import sootup.core.signatures.MethodSignature; -import sootup.core.signatures.MethodSubSignature; import sootup.core.types.ArrayType; import sootup.core.types.ClassType; import sootup.core.types.PrimitiveType; import sootup.core.views.View; import sootup.java.bytecode.frontend.inputlocation.DefaultRuntimeAnalysisInputLocation; +import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.language.JavaJimple; import sootup.java.core.views.JavaView; @@ -131,8 +131,11 @@ private void fillList(List listWithAllValues, View view) { ClassType StringClass = identifierFactory.getClassType("java.lang.String"); MethodSignature toStringMethod = identifierFactory.getMethodSignature( - StringClass, new MethodSubSignature("toString", Collections.emptyList(), StringClass)); - FieldSignature stringField = new FieldSignature(StringClass, "a", StringClass); + StringClass, + new JavaIdentifierFactory() + .getMethodSubSignature("toString", StringClass, Collections.emptyList())); + FieldSignature stringField = + new JavaIdentifierFactory().getFieldSignature("a", StringClass, StringClass); Immediate stringConstant = new StringConstant("String", StringClass); listWithAllValues.add(BooleanConstant.getInstance(true)); listWithAllValues.add(DoubleConstant.getInstance(2.5)); @@ -140,9 +143,9 @@ private void fillList(List listWithAllValues, View view) { listWithAllValues.add(IntConstant.getInstance(3)); listWithAllValues.add(LongConstant.getInstance(3L)); listWithAllValues.add(stringConstant); - listWithAllValues.add(JavaJimple.newEnumConstant("3", "EnumTest")); - listWithAllValues.add(JavaJimple.newClassConstant("java/lang/String")); - listWithAllValues.add(JavaJimple.newMethodHandle(toStringMethod, 5)); + listWithAllValues.add(JavaJimple.newEnumConstant("3", "EnumTest", identifierFactory)); + listWithAllValues.add(JavaJimple.newClassConstant("java/lang/String", identifierFactory)); + listWithAllValues.add(JavaJimple.newMethodHandle(toStringMethod, 5, identifierFactory)); listWithAllValues.add(new MethodType(toStringMethod.getSubSignature(), StringClass)); listWithAllValues.add(new JAddExpr(stringConstant, stringConstant)); listWithAllValues.add(new JAndExpr(stringConstant, stringConstant)); @@ -182,9 +185,11 @@ private void fillList(List listWithAllValues, View view) { new JDynamicInvokeExpr( toStringMethod, Collections.singletonList(stringConstant), - new MethodSignature( - identifierFactory.getClassType(JDynamicInvokeExpr.INVOKEDYNAMIC_DUMMY_CLASS_NAME), - toStringMethod.getSubSignature()), + new JavaIdentifierFactory() + .getMethodSignature( + identifierFactory.getClassType( + JDynamicInvokeExpr.INVOKEDYNAMIC_DUMMY_CLASS_NAME), + toStringMethod.getSubSignature()), Collections.singletonList(stringConstant))); listWithAllValues.add(new JCastExpr(stringConstant, StringClass)); listWithAllValues.add(new JInstanceOfExpr(stringConstant, StringClass)); diff --git a/sootup.callgraph/src/test/java/sootup/callgraph/config/CallGraphConfigTest.java b/sootup.callgraph/src/test/java/sootup/callgraph/config/CallGraphConfigTest.java index bc4ebfe7f53..a442b04db6a 100644 --- a/sootup.callgraph/src/test/java/sootup/callgraph/config/CallGraphConfigTest.java +++ b/sootup.callgraph/src/test/java/sootup/callgraph/config/CallGraphConfigTest.java @@ -47,7 +47,7 @@ */ public class CallGraphConfigTest { - private final JavaIdentifierFactory identifierFactory = JavaIdentifierFactory.getInstance(); + private final JavaIdentifierFactory identifierFactory = new JavaIdentifierFactory(); private JavaView createView(String classPath) { List inputLocations = new ArrayList<>(); diff --git a/sootup.callgraph/src/test/java/sootup/callgraph/scope/CallGraphScopeTest.java b/sootup.callgraph/src/test/java/sootup/callgraph/scope/CallGraphScopeTest.java index a6c54c5ef9a..6c6ec19397f 100644 --- a/sootup.callgraph/src/test/java/sootup/callgraph/scope/CallGraphScopeTest.java +++ b/sootup.callgraph/src/test/java/sootup/callgraph/scope/CallGraphScopeTest.java @@ -27,7 +27,7 @@ @Tag("Java9") public class CallGraphScopeTest { - private final JavaIdentifierFactory identifierFactory = JavaIdentifierFactory.getInstance(); + private final JavaIdentifierFactory identifierFactory = new JavaIdentifierFactory(); private JavaView createLibraryView() { String classPath = "src/test/resources/callgraph/Library/binary/"; diff --git a/sootup.codepropertygraph/src/test/java/sootup/codepropertygraph/GraphTestSuiteBase.java b/sootup.codepropertygraph/src/test/java/sootup/codepropertygraph/GraphTestSuiteBase.java index 8a73ae59af9..1c475ddad91 100644 --- a/sootup.codepropertygraph/src/test/java/sootup/codepropertygraph/GraphTestSuiteBase.java +++ b/sootup.codepropertygraph/src/test/java/sootup/codepropertygraph/GraphTestSuiteBase.java @@ -16,10 +16,9 @@ import sootup.core.model.MethodModifier; import sootup.core.model.SootMethod; import sootup.core.signatures.MethodSignature; -import sootup.core.signatures.PackageName; import sootup.core.types.VoidType; +import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.JavaSootMethod; -import sootup.java.core.types.JavaClassType; public abstract class GraphTestSuiteBase { @@ -46,12 +45,13 @@ protected void verifyEdges( protected SootMethod createSootMethod( MutableControlFlowGraph controlFlowGraph, String methodName) { + JavaIdentifierFactory identifierFactory = new JavaIdentifierFactory(); MethodSignature methodSignature = - new MethodSignature( - new JavaClassType("TestClass", new PackageName("pkg")), + identifierFactory.getMethodSignature( + identifierFactory.getClassType("TestClass", "pkg"), methodName, - Collections.emptyList(), - VoidType.getInstance()); + VoidType.getInstance(), + Collections.emptyList()); return new JavaSootMethod( new OverridingBodySource( diff --git a/sootup.codepropertygraph/src/test/java/sootup/codepropertygraph/cfg/CfgCreatorTest.java b/sootup.codepropertygraph/src/test/java/sootup/codepropertygraph/cfg/CfgCreatorTest.java index c8879d2b327..b99c2f6f557 100644 --- a/sootup.codepropertygraph/src/test/java/sootup/codepropertygraph/cfg/CfgCreatorTest.java +++ b/sootup.codepropertygraph/src/test/java/sootup/codepropertygraph/cfg/CfgCreatorTest.java @@ -18,9 +18,8 @@ import sootup.core.jimple.common.stmt.*; import sootup.core.jimple.javabytecode.stmt.JSwitchStmt; import sootup.core.model.SootMethod; -import sootup.core.signatures.PackageName; import sootup.core.types.PrimitiveType; -import sootup.java.core.types.JavaClassType; +import sootup.java.core.JavaIdentifierFactory; public class CfgCreatorTest extends GraphTestSuiteBase { @Test @@ -171,7 +170,7 @@ private SootMethod createExceptionalEdgesMethod() { controlFlowGraph.setStartingStmt(throwStmt); controlFlowGraph.addExceptionalEdge( throwStmt, - new JavaClassType("CustomException", new PackageName("cfg.exceptions")), + new JavaIdentifierFactory().getClassType("CustomException", "cfg.exceptions"), returnStmt); return createSootMethod(controlFlowGraph, "exceptionalEdgesMethod"); diff --git a/sootup.core/src/main/java/sootup/core/jimple/common/constant/EnumConstant.java b/sootup.core/src/main/java/sootup/core/jimple/common/constant/EnumConstant.java index afc8118c532..f750b384a2b 100644 --- a/sootup.core/src/main/java/sootup/core/jimple/common/constant/EnumConstant.java +++ b/sootup.core/src/main/java/sootup/core/jimple/common/constant/EnumConstant.java @@ -23,6 +23,7 @@ */ import org.jspecify.annotations.NonNull; +import sootup.core.IdentifierFactory; import sootup.core.jimple.visitor.ConstantVisitor; import sootup.core.signatures.FieldSignature; import sootup.core.types.ClassType; @@ -33,10 +34,13 @@ public class EnumConstant implements Constant { private final ClassType type; private final FieldSignature signature; - public EnumConstant(@NonNull String value, @NonNull ClassType type) { + public EnumConstant( + @NonNull String value, + @NonNull ClassType type, + @NonNull IdentifierFactory identifierFactory) { this.value = value; this.type = type; - this.signature = new FieldSignature(type, value, type); + this.signature = identifierFactory.getFieldSignature(value, type, type); } @Override diff --git a/sootup.core/src/main/java/sootup/core/signatures/FieldSignature.java b/sootup.core/src/main/java/sootup/core/signatures/FieldSignature.java index 947629c8437..b4fe5b9169a 100644 --- a/sootup.core/src/main/java/sootup/core/signatures/FieldSignature.java +++ b/sootup.core/src/main/java/sootup/core/signatures/FieldSignature.java @@ -34,13 +34,11 @@ */ public class FieldSignature extends SootClassMemberSignature { - public FieldSignature( - @NonNull ClassType declaringClass, @NonNull String name, @NonNull Type type) { - this(declaringClass, new FieldSubSignature(name, type)); + FieldSignature(@NonNull ClassType declaringClass, @NonNull String name, @NonNull Type type) { + this(declaringClass, SignatureInterner.getFieldSubSignature(name, type)); } - public FieldSignature( - @NonNull ClassType declaringClass, @NonNull FieldSubSignature subSignature) { + FieldSignature(@NonNull ClassType declaringClass, @NonNull FieldSubSignature subSignature) { super(declaringClass, subSignature); } } diff --git a/sootup.core/src/main/java/sootup/core/signatures/FieldSubSignature.java b/sootup.core/src/main/java/sootup/core/signatures/FieldSubSignature.java index a1fc9fe9712..36a62048591 100644 --- a/sootup.core/src/main/java/sootup/core/signatures/FieldSubSignature.java +++ b/sootup.core/src/main/java/sootup/core/signatures/FieldSubSignature.java @@ -43,7 +43,7 @@ public class FieldSubSignature extends SootClassMemberSubSignature * @param name The method name. * @param type The type signature. */ - public FieldSubSignature(@NonNull String name, @NonNull Type type) { + FieldSubSignature(@NonNull String name, @NonNull Type type) { super(name, type); } diff --git a/sootup.core/src/main/java/sootup/core/signatures/MethodSignature.java b/sootup.core/src/main/java/sootup/core/signatures/MethodSignature.java index 6b650c1ced4..cc5e3c93dde 100644 --- a/sootup.core/src/main/java/sootup/core/signatures/MethodSignature.java +++ b/sootup.core/src/main/java/sootup/core/signatures/MethodSignature.java @@ -31,12 +31,14 @@ /** Represents the fully qualified signature of a method. */ public class MethodSignature extends SootClassMemberSignature { - public MethodSignature( + MethodSignature( @NonNull ClassType declaringClassSignature, @NonNull String methodName, @NonNull Iterable parameters, @NonNull Type fqReturnType) { - this(declaringClassSignature, new MethodSubSignature(methodName, parameters, fqReturnType)); + this( + declaringClassSignature, + SignatureInterner.getMethodSubSignature(methodName, fqReturnType, parameters)); } /** @@ -46,8 +48,7 @@ public MethodSignature( * @param declaringClass the declaring class signature * @param subSignature the sub-signature */ - public MethodSignature( - @NonNull ClassType declaringClass, @NonNull MethodSubSignature subSignature) { + MethodSignature(@NonNull ClassType declaringClass, @NonNull MethodSubSignature subSignature) { super(declaringClass, subSignature); } diff --git a/sootup.core/src/main/java/sootup/core/signatures/MethodSubSignature.java b/sootup.core/src/main/java/sootup/core/signatures/MethodSubSignature.java index 7b1cb713430..b27b30c9c88 100644 --- a/sootup.core/src/main/java/sootup/core/signatures/MethodSubSignature.java +++ b/sootup.core/src/main/java/sootup/core/signatures/MethodSubSignature.java @@ -52,7 +52,7 @@ public class MethodSubSignature extends SootClassMemberSubSignature * @param parameterTypes The signatures of the method parameters. * @param type The return type signature. */ - public MethodSubSignature( + MethodSubSignature( @NonNull String name, @NonNull Iterable parameterTypes, @NonNull Type type) { super(name, type); diff --git a/sootup.core/src/main/java/sootup/core/signatures/PackageName.java b/sootup.core/src/main/java/sootup/core/signatures/PackageName.java index b19e8211d59..66c6d31d183 100644 --- a/sootup.core/src/main/java/sootup/core/signatures/PackageName.java +++ b/sootup.core/src/main/java/sootup/core/signatures/PackageName.java @@ -34,7 +34,7 @@ public class PackageName { /** Represents the default package. */ - public static final PackageName DEFAULT_PACKAGE = new PackageName(""); + public static final PackageName DEFAULT_PACKAGE = SignatureInterner.getPackageName(""); public static final String PACKAGE_INFO = "package-info"; @@ -46,7 +46,7 @@ public class PackageName { * * @param packageName the package's name */ - public PackageName(final String packageName) { + protected PackageName(final String packageName) { this.packageName = packageName; } @@ -60,7 +60,7 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (!(o instanceof PackageName)) { + if (o == null || getClass() != o.getClass()) { return false; } PackageName that = (PackageName) o; diff --git a/sootup.core/src/main/java/sootup/core/signatures/SignatureInterner.java b/sootup.core/src/main/java/sootup/core/signatures/SignatureInterner.java new file mode 100644 index 00000000000..104a4784300 --- /dev/null +++ b/sootup.core/src/main/java/sootup/core/signatures/SignatureInterner.java @@ -0,0 +1,139 @@ +package sootup.core.signatures; + +/*- + * #%L + * Soot - a J*va Optimization Framework + * %% + * Copyright (C) 2018-2020 linghui Luo, Jan Martin Persch + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * . + * #L% + */ + +import com.google.common.collect.Interner; +import com.google.common.collect.Interners; +import org.jspecify.annotations.NonNull; +import sootup.core.IdentifierFactory; +import sootup.core.types.ClassType; +import sootup.core.types.Type; + +/** + * Hash-conses (interns) the identifier objects of {@code sootup.core.signatures}: for a given value + * there is at most one live instance, so equal signatures are the same object and may be compared + * with {@code ==}. + * + *

Interning happens on the constructed object itself, so identity follows each type's own {@code + * equals}/{@code hashCode} - there is no separate cache key that could drift out of sync with them. + * The interners hold their entries weakly, so a signature that nothing references any more is + * collected as usual. + * + *

This class exists in {@code sootup.core} because the constructors it calls are + * package-private: it is the only way to create these types, which is what makes the interning + * invariant hold. Most callers should not use it directly but go through an {@link + * IdentifierFactory}, which is the language-aware entry point. {@code sootup.core} itself has no + * {@link IdentifierFactory} implementation available (that lives in the language modules, which + * depend on this one), so code inside this module - its tests in particular - uses these methods + * instead. + */ +public final class SignatureInterner { + + private SignatureInterner() {} + + @NonNull private static final Interner PACKAGE_NAMES = Interners.newWeakInterner(); + + @NonNull + private static final Interner METHOD_SUB_SIGNATURES = + Interners.newWeakInterner(); + + @NonNull + private static final Interner FIELD_SUB_SIGNATURES = + Interners.newWeakInterner(); + + @NonNull + private static final Interner METHOD_SIGNATURES = Interners.newWeakInterner(); + + @NonNull + private static final Interner FIELD_SIGNATURES = Interners.newWeakInterner(); + + /** Returns the unique {@link PackageName} for the given package name. */ + @NonNull + public static PackageName getPackageName(@NonNull String packageName) { + return PACKAGE_NAMES.intern(new PackageName(packageName)); + } + + /** + * Interns an already constructed {@link PackageName}. Subclasses of {@link PackageName} live in + * the language modules and construct themselves; they pass the result through here so that they + * share the interning invariant. + */ + @NonNull + public static T internPackageName(@NonNull T packageName) { + @SuppressWarnings("unchecked") + T interned = (T) PACKAGE_NAMES.intern(packageName); + return interned; + } + + /** + * Returns the unique {@link MethodSubSignature} for the given name, return type and parameters. + */ + @NonNull + public static MethodSubSignature getMethodSubSignature( + @NonNull String name, + @NonNull Type returnType, + @NonNull Iterable parameterTypes) { + return METHOD_SUB_SIGNATURES.intern(new MethodSubSignature(name, parameterTypes, returnType)); + } + + /** Returns the unique {@link FieldSubSignature} for the given name and type. */ + @NonNull + public static FieldSubSignature getFieldSubSignature(@NonNull String name, @NonNull Type type) { + return FIELD_SUB_SIGNATURES.intern(new FieldSubSignature(name, type)); + } + + /** Returns the unique {@link MethodSignature} for the given declaring class and sub-signature. */ + @NonNull + public static MethodSignature getMethodSignature( + @NonNull ClassType declaringClass, @NonNull MethodSubSignature subSignature) { + return METHOD_SIGNATURES.intern(new MethodSignature(declaringClass, subSignature)); + } + + /** + * Returns the unique {@link MethodSignature} for the given declaring class, name, parameters and + * return type. + */ + @NonNull + public static MethodSignature getMethodSignature( + @NonNull ClassType declaringClass, + @NonNull String methodName, + @NonNull Iterable parameters, + @NonNull Type returnType) { + return getMethodSignature( + declaringClass, getMethodSubSignature(methodName, returnType, parameters)); + } + + /** Returns the unique {@link FieldSignature} for the given declaring class and sub-signature. */ + @NonNull + public static FieldSignature getFieldSignature( + @NonNull ClassType declaringClass, @NonNull FieldSubSignature subSignature) { + return FIELD_SIGNATURES.intern(new FieldSignature(declaringClass, subSignature)); + } + + /** Returns the unique {@link FieldSignature} for the given declaring class, name and type. */ + @NonNull + public static FieldSignature getFieldSignature( + @NonNull ClassType declaringClass, @NonNull String name, @NonNull Type type) { + return getFieldSignature(declaringClass, getFieldSubSignature(name, type)); + } +} diff --git a/sootup.core/src/main/java/sootup/core/typehierarchy/TypeHierarchy.java b/sootup.core/src/main/java/sootup/core/typehierarchy/TypeHierarchy.java index c2300e1c00c..5074800c232 100644 --- a/sootup.core/src/main/java/sootup/core/typehierarchy/TypeHierarchy.java +++ b/sootup.core/src/main/java/sootup/core/typehierarchy/TypeHierarchy.java @@ -126,9 +126,6 @@ default boolean isSubtype(@NonNull Type supertype, @NonNull Type potentialSubtyp return true; } - final String jlObject = "java.lang.Object"; - final String jiSerializable = "java.io.Serializable"; - final String jlCloneable = "java.lang.Cloneable"; if (supertype instanceof ArrayType superArrayType) { if (!(potentialSubtype instanceof ArrayType potentialSubArrayType)) { return false; @@ -145,28 +142,19 @@ default boolean isSubtype(@NonNull Type supertype, @NonNull Type potentialSubtyp && potentialSubArrayType.getDimension() == superArrayType.getDimension()) { // Arrays are covariant: Object[] x = new String[0]; return true; - } else if (superArrayType.getBaseType() instanceof ClassType - && (((ClassType) superArrayType.getBaseType()).getFullyQualifiedName().equals(jlObject) - || ((ClassType) superArrayType.getBaseType()) - .getFullyQualifiedName() - .equals(jiSerializable) - || ((ClassType) superArrayType.getBaseType()) - .getFullyQualifiedName() - .equals(jlCloneable))) { + } else if (superArrayType.getBaseType() instanceof ClassType baseClassType + && isArraySupertype(baseClassType)) { // Special case: Object[] x = new double[0][0], Object[][] y = new double[0][0][0], ... return potentialSubArrayType.getDimension() > superArrayType.getDimension(); } else { return false; } - } else if (supertype instanceof ClassType) { - String supertypeName = ((ClassType) supertype).getFullyQualifiedName(); + } else if (supertype instanceof ClassType supertypeClassType) { if (potentialSubtype instanceof ClassType) { - return isClassSubtype((ClassType) supertype, (ClassType) potentialSubtype); + return isClassSubtype(supertypeClassType, (ClassType) potentialSubtype); } else if (potentialSubtype instanceof ArrayType) { // Arrays are subtypes of java.lang.Object, java.io.Serializable and java.lang.Cloneable - return supertypeName.equals(jlObject) - || supertypeName.equals(jiSerializable) - || supertypeName.equals(jlCloneable); + return isArraySupertype(supertypeClassType); } else { throw new AssertionError("potentialSubtype has unexpected type"); } @@ -200,16 +188,38 @@ default Stream superClassesOf(@NonNull ClassType classType) { */ default boolean isClassSubtype( @NonNull ClassType supertype, @NonNull ClassType potentialSubtype) { - String supertypeName = supertype.getFullyQualifiedName(); - String potentialSubtypeName = potentialSubtype.getFullyQualifiedName(); - final String jlObject = "java.lang.Object"; // any potential subtype is a subtype of java.lang.Object except java.lang.Object itself // superClassOf() check is a fast path - return (supertypeName.equals(jlObject) && !potentialSubtypeName.equals(jlObject)) + return (isJavaLangObject(supertype) && !isJavaLangObject(potentialSubtype)) || superClassesOf(potentialSubtype).anyMatch(t -> t == supertype) || implementedInterfacesOf(potentialSubtype).anyMatch(t -> t == supertype); } + /** + * Returns whether the given type is one of the three types every array is a subtype of: {@code + * java.lang.Object}, {@code java.io.Serializable} and {@code java.lang.Cloneable}. + */ + private static boolean isArraySupertype(@NonNull ClassType type) { + return isJavaLangObject(type) + || hasName(type, "java.io", "Serializable") + || hasName(type, "java.lang", "Cloneable"); + } + + private static boolean isJavaLangObject(@NonNull ClassType type) { + return hasName(type, "java.lang", "Object"); + } + + /** + * Compares the class and package name separately instead of going through {@link + * ClassType#getFullyQualifiedName()}, which builds a new String on every call - this runs per + * subtype query. + */ + private static boolean hasName( + @NonNull ClassType type, @NonNull String packageName, @NonNull String className) { + return type.getClassName().equals(className) + && type.getPackageName().getName().equals(packageName); + } + Stream directlyImplementedInterfacesOf(@NonNull ClassType type); boolean isInterface(@NonNull ClassType type); diff --git a/sootup.core/src/main/java/sootup/core/typehierarchy/ViewTypeHierarchy.java b/sootup.core/src/main/java/sootup/core/typehierarchy/ViewTypeHierarchy.java index ded8fd9aa41..7ed7e41328f 100644 --- a/sootup.core/src/main/java/sootup/core/typehierarchy/ViewTypeHierarchy.java +++ b/sootup.core/src/main/java/sootup/core/typehierarchy/ViewTypeHierarchy.java @@ -69,7 +69,7 @@ public class ViewTypeHierarchy implements MutableTypeHierarchy { /** * Cheap, incrementally-mutated-in-place adjacency (dense ids + direct edges only). Built once, * memoized, then mutated directly by {@link #addType(SootClass)} - unlike {@link - * #typeHierarcBacking}, it never needs to be rebuilt wholesale. + * #typeHierarchyBacking}, it never needs to be rebuilt wholesale. */ private final Supplier lazyRaw; @@ -80,7 +80,7 @@ public class ViewTypeHierarchy implements MutableTypeHierarchy { * shift every subsequent pre-order position - mutation is rare relative to reads, so a full * rebuild on the first read after a mutation is cheap in practice. */ - private ViewTypeHierarchy.@Nullable TypeHierarchyEncoding typeHierarcBacking; + private ViewTypeHierarchy.@Nullable TypeHierarchyEncoding typeHierarchyBacking; private final ClassType objectClassType; @@ -143,11 +143,11 @@ private int resolveId(@NonNull ClassType type) { return resolved == null ? -1 : resolved; } - private TypeHierarchyEncoding getTypeHierarcBacking() { - TypeHierarchyEncoding d = typeHierarcBacking; + private TypeHierarchyEncoding getTypeHierarchyBacking() { + TypeHierarchyEncoding d = typeHierarchyBacking; if (d == null) { d = buildDerived(lazyRaw.get()); - typeHierarcBacking = d; + typeHierarchyBacking = d; } return d; } @@ -191,7 +191,7 @@ public Stream subinterfacesOf(@NonNull ClassType interfaceType) { if (!raw.isInterface.get(id)) { return Stream.empty(); } - TypeHierarchyEncoding d = getTypeHierarcBacking(); + TypeHierarchyEncoding d = getTypeHierarchyBacking(); int dense = d.denseOfInterfaceId[id]; return Arrays.stream(d.descendants[dense]) .filter(x -> x != dense) @@ -207,7 +207,7 @@ public Stream subtypesOf(@NonNull ClassType type) { } RawAdjacency raw = lazyRaw.get(); - TypeHierarchyEncoding d = getTypeHierarcBacking(); + TypeHierarchyEncoding d = getTypeHierarchyBacking(); return raw.isInterface.get(id) ? interfaceSubtypesOf(id, raw, d) : classSubtypesOf(id, raw, d); } @@ -294,7 +294,7 @@ public Collection getLowestCommonAncestors(ClassType a, ClassType b) int idA = resolveId(a); int idB = resolveId(b); RawAdjacency raw = lazyRaw.get(); - TypeHierarchyEncoding d = getTypeHierarcBacking(); + TypeHierarchyEncoding d = getTypeHierarchyBacking(); Set lcas; if (idA == -1 || idB == -1) { @@ -386,7 +386,7 @@ public Stream implementedInterfacesOf(@NonNull ClassType type) { return Stream.empty(); } RawAdjacency raw = lazyRaw.get(); - TypeHierarchyEncoding d = getTypeHierarcBacking(); + TypeHierarchyEncoding d = getTypeHierarchyBacking(); if (!raw.isInterface.get(id)) { // We ascend from the class through its superclasses to java.lang.Object, unioning together @@ -467,7 +467,7 @@ public boolean isClassSubtype( } RawAdjacency raw = lazyRaw.get(); - TypeHierarchyEncoding d = getTypeHierarcBacking(); + TypeHierarchyEncoding d = getTypeHierarchyBacking(); boolean superIsInterface = raw.isInterface.get(superId); if (!superIsInterface) { @@ -511,7 +511,7 @@ public void addType(@NonNull SootClass sootClass) { if (!changed) { return; } - typeHierarcBacking = null; + typeHierarchyBacking = null; subtypeClassesCache.clear(); lcaCache.clear(); modificationCount++; diff --git a/sootup.core/src/test/java/sootup/core/TestUtil.java b/sootup.core/src/test/java/sootup/core/TestUtil.java index e1998ec421e..8920bf389b1 100644 --- a/sootup.core/src/test/java/sootup/core/TestUtil.java +++ b/sootup.core/src/test/java/sootup/core/TestUtil.java @@ -19,6 +19,7 @@ import sootup.core.signatures.MethodSignature; import sootup.core.signatures.MethodSubSignature; import sootup.core.signatures.PackageName; +import sootup.core.signatures.SignatureInterner; import sootup.core.types.*; import sootup.core.types.PrimitiveType.IntType; @@ -40,7 +41,7 @@ public static SimpleStmtPositionInfo createDummySimpleStmtPositionInfo() { * @return a dummy method signature */ public static MethodSignature createDummyMethodSignature() { - return new MethodSignature( + return SignatureInterner.getMethodSignature( TestUtil.createDummyClassType(), TestUtil.createDummyMethodSubSignature()); } @@ -50,7 +51,8 @@ public static MethodSignature createDummyMethodSignature() { * @return a dummy method sub signature */ public static MethodSubSignature createDummyMethodSubSignature() { - return new MethodSubSignature("test", Collections.emptyList(), IntType.getInstance()); + return SignatureInterner.getMethodSubSignature( + "test", IntType.getInstance(), Collections.emptyList()); } /** @@ -60,7 +62,7 @@ public static MethodSubSignature createDummyMethodSubSignature() { * @return a dummy field signature */ public static FieldSignature createDummyFieldSignature() { - return new FieldSignature( + return SignatureInterner.getFieldSignature( TestUtil.createDummyClassType(), TestUtil.createDummyFieldSubSignature()); } @@ -70,7 +72,7 @@ public static FieldSignature createDummyFieldSignature() { * @return a dummy field sub signature */ public static FieldSubSignature createDummyFieldSubSignature() { - return new FieldSubSignature("test", IntType.getInstance()); + return SignatureInterner.getFieldSubSignature("test", IntType.getInstance()); } /** @@ -169,7 +171,7 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName("test"); + return SignatureInterner.getPackageName("test"); } }; } diff --git a/sootup.core/src/test/java/sootup/core/graph/ImmutableBlockControlFlowGraphTest.java b/sootup.core/src/test/java/sootup/core/graph/ImmutableBlockControlFlowGraphTest.java index 4ac5388b269..714de2696ab 100644 --- a/sootup.core/src/test/java/sootup/core/graph/ImmutableBlockControlFlowGraphTest.java +++ b/sootup.core/src/test/java/sootup/core/graph/ImmutableBlockControlFlowGraphTest.java @@ -11,6 +11,7 @@ import sootup.core.jimple.common.ref.JCaughtExceptionRef; import sootup.core.jimple.common.stmt.*; import sootup.core.signatures.PackageName; +import sootup.core.signatures.SignatureInterner; import sootup.core.types.ClassType; import sootup.core.types.PrimitiveType; @@ -32,7 +33,7 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName("java.lang"); + return SignatureInterner.getPackageName("java.lang"); } }; diff --git a/sootup.core/src/test/java/sootup/core/graph/MutableBlockControlFlowGraphTest.java b/sootup.core/src/test/java/sootup/core/graph/MutableBlockControlFlowGraphTest.java index 889293921fc..cee73dbb8d8 100644 --- a/sootup.core/src/test/java/sootup/core/graph/MutableBlockControlFlowGraphTest.java +++ b/sootup.core/src/test/java/sootup/core/graph/MutableBlockControlFlowGraphTest.java @@ -15,6 +15,7 @@ import sootup.core.jimple.common.ref.JCaughtExceptionRef; import sootup.core.jimple.common.stmt.*; import sootup.core.signatures.PackageName; +import sootup.core.signatures.SignatureInterner; import sootup.core.types.*; import sootup.core.util.printer.BriefStmtPrinter; @@ -47,7 +48,7 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName("java.lang"); + return SignatureInterner.getPackageName("java.lang"); } }; @@ -66,7 +67,7 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName("java.io"); + return SignatureInterner.getPackageName("java.io"); } }; @@ -568,7 +569,7 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName("some.object"); + return SignatureInterner.getPackageName("some.object"); } }; @@ -587,7 +588,7 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName("some.object"); + return SignatureInterner.getPackageName("some.object"); } }; diff --git a/sootup.core/src/test/java/sootup/core/jimple/basic/LocalGeneratorTest.java b/sootup.core/src/test/java/sootup/core/jimple/basic/LocalGeneratorTest.java index 149e6f67e57..17915cb2830 100644 --- a/sootup.core/src/test/java/sootup/core/jimple/basic/LocalGeneratorTest.java +++ b/sootup.core/src/test/java/sootup/core/jimple/basic/LocalGeneratorTest.java @@ -7,6 +7,7 @@ import org.junit.jupiter.api.Test; import sootup.core.jimple.common.Local; import sootup.core.signatures.PackageName; +import sootup.core.signatures.SignatureInterner; import sootup.core.types.*; public class LocalGeneratorTest { @@ -33,7 +34,7 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName("Fruit"); + return SignatureInterner.getPackageName("Fruit"); } }); diff --git a/sootup.core/src/test/java/sootup/core/jimple/common/constant/MethodHandleTest.java b/sootup.core/src/test/java/sootup/core/jimple/common/constant/MethodHandleTest.java index bbae716ffd3..41ffd426066 100644 --- a/sootup.core/src/test/java/sootup/core/jimple/common/constant/MethodHandleTest.java +++ b/sootup.core/src/test/java/sootup/core/jimple/common/constant/MethodHandleTest.java @@ -8,6 +8,7 @@ import sootup.core.signatures.FieldSignature; import sootup.core.signatures.MethodSignature; import sootup.core.signatures.PackageName; +import sootup.core.signatures.SignatureInterner; import sootup.core.types.*; import sootup.core.types.PrimitiveType.IntType; @@ -62,12 +63,13 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName("test"); + return SignatureInterner.getPackageName("test"); } }; MethodSignature ms = - new MethodSignature(classType, "m1", Collections.emptyList(), VoidType.getInstance()); - FieldSignature fs = new FieldSignature(classType, "f", IntType.getInstance()); + SignatureInterner.getMethodSignature( + classType, "m1", Collections.emptyList(), VoidType.getInstance()); + FieldSignature fs = SignatureInterner.getFieldSignature(classType, "f", IntType.getInstance()); MethodHandle mhms = new MethodHandle(ms, Kind.REF_INVOKE_VIRTUAL.getValue(), classType); MethodHandle mhfs = new MethodHandle(fs, Kind.REF_GET_FIELD, classType); diff --git a/sootup.core/src/test/java/sootup/core/jimple/common/expr/JDynamicInvokeExprTest.java b/sootup.core/src/test/java/sootup/core/jimple/common/expr/JDynamicInvokeExprTest.java index 74f588aecd9..17e1e069dde 100644 --- a/sootup.core/src/test/java/sootup/core/jimple/common/expr/JDynamicInvokeExprTest.java +++ b/sootup.core/src/test/java/sootup/core/jimple/common/expr/JDynamicInvokeExprTest.java @@ -9,8 +9,8 @@ import sootup.core.jimple.common.Immediate; import sootup.core.jimple.common.constant.IntConstant; import sootup.core.signatures.MethodSignature; -import sootup.core.signatures.MethodSubSignature; import sootup.core.signatures.PackageName; +import sootup.core.signatures.SignatureInterner; import sootup.core.types.ClassType; import sootup.core.types.VoidType; @@ -30,7 +30,7 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName("sootup.dummy"); + return SignatureInterner.getPackageName("sootup.dummy"); } }; } @@ -51,16 +51,17 @@ public PackageName getPackageName() { @Test void testWithersPreserveTagAndMethodSignature() { MethodSignature dummyMethod = - new MethodSignature( + SignatureInterner.getMethodSignature( dummyClassType(), "call", Collections.emptyList(), VoidType.getInstance()); MethodSignature dummyMethod2 = - new MethodSignature( + SignatureInterner.getMethodSignature( dummyClassType(), "call2", Collections.emptyList(), VoidType.getInstance()); MethodSignature bootstrapSig = TestUtil.createDummyMethodSignature(); MethodSignature bootstrapSig2 = - new MethodSignature( + SignatureInterner.getMethodSignature( TestUtil.createDummyClassType(), - new MethodSubSignature("bootstrap2", Collections.emptyList(), VoidType.getInstance())); + SignatureInterner.getMethodSubSignature( + "bootstrap2", VoidType.getInstance(), Collections.emptyList())); int originalTag = 7; List bsmArgs = Collections.singletonList(IntConstant.getInstance(1)); diff --git a/sootup.core/src/test/java/sootup/core/util/printer/AbstractStmtJimplePrinterTest.java b/sootup.core/src/test/java/sootup/core/util/printer/AbstractStmtJimplePrinterTest.java index 9706f3ff1af..c1adf94866e 100644 --- a/sootup.core/src/test/java/sootup/core/util/printer/AbstractStmtJimplePrinterTest.java +++ b/sootup.core/src/test/java/sootup/core/util/printer/AbstractStmtJimplePrinterTest.java @@ -7,8 +7,8 @@ import org.junit.jupiter.api.Test; import sootup.core.model.Body; import sootup.core.signatures.MethodSignature; -import sootup.core.signatures.MethodSubSignature; import sootup.core.signatures.PackageName; +import sootup.core.signatures.SignatureInterner; import sootup.core.types.*; public class AbstractStmtJimplePrinterTest { @@ -16,9 +16,9 @@ public class AbstractStmtJimplePrinterTest { @Test public void addImportTest() { - PackageName abc = new PackageName("a.b.c"); - PackageName def = new PackageName("d.e.f"); - PackageName anotherAbc = new PackageName("a.b.c"); + PackageName abc = SignatureInterner.getPackageName("a.b.c"); + PackageName def = SignatureInterner.getPackageName("d.e.f"); + PackageName anotherAbc = SignatureInterner.getPackageName("a.b.c"); ClassType classOneFromAbc = generateClass("ClassOne", abc); ClassType classOneFromDef = generateClass("ClassOne", def); @@ -26,9 +26,10 @@ public void addImportTest() { ClassType classTwoFromAbc = generateClass("ClassTwo", abc); MethodSignature ms = - new MethodSignature( + SignatureInterner.getMethodSignature( classOneFromAbc, - new MethodSubSignature("banana", Collections.emptyList(), VoidType.getInstance())); + SignatureInterner.getMethodSubSignature( + "banana", VoidType.getInstance(), Collections.emptyList())); final Body body = Body.builder().setModifiers(Collections.emptySet()).setMethodSignature(ms).build(); NormalStmtPrinter p = new NormalStmtPrinter(); diff --git a/sootup.examples/src/test/java/sootup/examples/basicSetup/BasicSetupTest.java b/sootup.examples/src/test/java/sootup/examples/basicSetup/BasicSetupTest.java index 51453ee6b7d..995c2954145 100644 --- a/sootup.examples/src/test/java/sootup/examples/basicSetup/BasicSetupTest.java +++ b/sootup.examples/src/test/java/sootup/examples/basicSetup/BasicSetupTest.java @@ -67,6 +67,7 @@ public void createByteCodeProject() { .getInvokeExpr() .get() .getArg(0) - .equivTo(JavaJimple.newStringConstant("Hello World!")))); + .equivTo( + JavaJimple.newStringConstant("Hello World!", identifierFactory)))); } } diff --git a/sootup.examples/src/test/java/sootup/examples/mutatingSootClass/MutatingTest.java b/sootup.examples/src/test/java/sootup/examples/mutatingSootClass/MutatingTest.java index 6757fe29251..5f57b1f570b 100644 --- a/sootup.examples/src/test/java/sootup/examples/mutatingSootClass/MutatingTest.java +++ b/sootup.examples/src/test/java/sootup/examples/mutatingSootClass/MutatingTest.java @@ -16,13 +16,12 @@ import sootup.core.model.SootMethod; import sootup.core.model.SourceType; import sootup.core.signatures.MethodSignature; -import sootup.core.signatures.MethodSubSignature; -import sootup.core.signatures.PackageName; import sootup.core.types.ArrayType; import sootup.core.types.PrimitiveType.IntType; import sootup.core.types.VoidType; import sootup.java.bytecode.frontend.inputlocation.PathBasedAnalysisInputLocation; import sootup.java.core.*; +import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.language.JavaJimple; import sootup.java.core.types.JavaClassType; import sootup.java.core.views.JavaView; @@ -95,12 +94,13 @@ public void testMutation() { SootMethod main = newClass .getMethod( - new MethodSubSignature( - "main", - Collections.singletonList( - new ArrayType( - new JavaClassType("String", new PackageName("java.lang")), 1)), - VoidType.getInstance())) + new JavaIdentifierFactory() + .getMethodSubSignature( + "main", + VoidType.getInstance(), + Collections.singletonList( + new ArrayType( + identifierFactory.getClassType("String", "java.lang"), 1)))) .orElse(null); assertNotNull(main); @@ -110,19 +110,22 @@ public void testMutation() { SootMethod constructor = sootClass .getMethod( - new MethodSubSignature("", Collections.emptyList(), VoidType.getInstance())) + new JavaIdentifierFactory() + .getMethodSubSignature( + "", VoidType.getInstance(), Collections.emptyList())) .orElse(null); assertNotNull(constructor); assertFalse(constructor.getBody().getLocals().isEmpty()); SootMethod olderMain = sootClass .getMethod( - new MethodSubSignature( - "main", - Collections.singletonList( - new ArrayType( - new JavaClassType("String", new PackageName("java.lang")), 1)), - VoidType.getInstance())) + new JavaIdentifierFactory() + .getMethodSubSignature( + "main", + VoidType.getInstance(), + Collections.singletonList( + new ArrayType( + identifierFactory.getClassType("String", "java.lang"), 1)))) .orElse(null); assertNotNull(olderMain); assertTrue(olderMain.getBody().getLocals().stream().noneMatch(local -> local.equals(newLocal))); diff --git a/sootup.interceptors/src/test/java/sootup/interceptors/LocalLivenessAnalyserTest.java b/sootup.interceptors/src/test/java/sootup/interceptors/LocalLivenessAnalyserTest.java index cb3fccc789f..089a344d917 100644 --- a/sootup.interceptors/src/test/java/sootup/interceptors/LocalLivenessAnalyserTest.java +++ b/sootup.interceptors/src/test/java/sootup/interceptors/LocalLivenessAnalyserTest.java @@ -67,7 +67,7 @@ void testCyclicControlFlowWithMergeDoesNotMutatePredecessorInPlace() { @Test void propagatesLivenessAcrossExceptionalEdges() { - JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory factory = new JavaIdentifierFactory(); var objectType = factory.getClassType("java.lang.Object"); var throwableType = factory.getClassType("java.lang.Throwable"); var sourceType = factory.getClassType("example.Source"); @@ -75,12 +75,12 @@ void propagatesLivenessAcrossExceptionalEdges() { Local caught = JavaJimple.newLocal("caught", throwableType); MethodSignature read = - new MethodSignature(sourceType, "read", Collections.emptyList(), objectType); + factory.getMethodSignature(sourceType, "read", objectType, Collections.emptyList()); var throwingStmt = JavaJimple.newAssignStmt(value, Jimple.newStaticInvokeExpr(read), NO_POSITION); var normalReturn = JavaJimple.newReturnVoidStmt(NO_POSITION); var handler = - JavaJimple.newIdentityStmt(caught, JavaJimple.newCaughtExceptionRef(), NO_POSITION); + JavaJimple.newIdentityStmt(caught, JavaJimple.newCaughtExceptionRef(factory), NO_POSITION); var exceptionalReturn = JavaJimple.newReturnStmt(value, NO_POSITION); MutableBlockControlFlowGraph graph = new MutableBlockControlFlowGraph(); diff --git a/sootup.interceptors/src/test/java/sootup/interceptors/UnusedLocalEliminatorTest.java b/sootup.interceptors/src/test/java/sootup/interceptors/UnusedLocalEliminatorTest.java index b7342a45888..901d61a50b8 100644 --- a/sootup.interceptors/src/test/java/sootup/interceptors/UnusedLocalEliminatorTest.java +++ b/sootup.interceptors/src/test/java/sootup/interceptors/UnusedLocalEliminatorTest.java @@ -57,7 +57,7 @@ public void testPreserveLocalChainOrder() { Body.BodyBuilder builder = Body.builder(); builder.setMethodSignature( - JavaIdentifierFactory.getInstance() + new JavaIdentifierFactory() .getMethodSignature("com.example.Test", "foo", "void", Collections.emptyList())); builder.setLocals(initialLocals); @@ -92,7 +92,7 @@ public void testUndeclaredReferencedLocalAppendedAtEnd() { Body.BodyBuilder builder = Body.builder(); builder.setMethodSignature( - JavaIdentifierFactory.getInstance() + new JavaIdentifierFactory() .getMethodSignature("com.example.Test", "bar", "void", Collections.emptyList())); builder.setLocals(initialLocals); diff --git a/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmAnnotationClassSource.java b/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmAnnotationClassSource.java index fadd0ac859e..3b61f436685 100644 --- a/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmAnnotationClassSource.java +++ b/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmAnnotationClassSource.java @@ -50,7 +50,6 @@ import sootup.core.types.Type; import sootup.core.util.Modifiers; import sootup.java.core.AnnotationUsage; -import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.JavaSootClassSource; import sootup.java.core.JavaSootField; import sootup.java.core.JavaSootMethod; @@ -58,14 +57,17 @@ public class AsmAnnotationClassSource extends JavaSootClassSource { @NonNull protected final ClassNode classNode; + @NonNull protected final IdentifierFactory identifierFactory; public AsmAnnotationClassSource( AnalysisInputLocation inputLocation, Path sourcePath, ClassType classType, - @NonNull ClassNode classNode) { + @NonNull ClassNode classNode, + @NonNull IdentifierFactory identifierFactory) { super(inputLocation, classType, sourcePath); this.classNode = classNode; + this.identifierFactory = identifierFactory; } private static Set resolveFields( @@ -74,7 +76,7 @@ private static Set resolveFields( .map( fieldNode -> { String fieldName = fieldNode.name; - Type fieldType = AsmUtil.toJimpleType(fieldNode.desc); + Type fieldType = AsmUtil.toJimpleType(fieldNode.desc, signatureFactory); FieldSignature fieldSignature = signatureFactory.getFieldSignature(fieldName, classSignature, fieldType); EnumSet modifiers = AsmUtil.getFieldModifiers(fieldNode.access); @@ -83,7 +85,7 @@ private static Set resolveFields( return new JavaSootField( fieldSignature, modifiers, - convertAnnotation(fieldNode.invisibleAnnotations), + convertAnnotation(fieldNode.invisibleAnnotations, signatureFactory), NoPositionInformation.getInstance()); }) .collect(Collectors.toSet()); @@ -91,7 +93,6 @@ private static Set resolveFields( @NonNull public Collection resolveMethods() throws ResolveException { - IdentifierFactory identifierFactory = JavaIdentifierFactory.getInstance(); return resolveMethods(classNode.methods, identifierFactory, classSignature) .collect(Collectors.toSet()); } @@ -105,11 +106,13 @@ private static Stream resolveMethods( asmClassClassSourceContent.setDeclaringClass(cs); List exceptions = - new ArrayList<>(AsmUtil.asmIdToSignatures(methodSource.exceptions)); + new ArrayList<>( + AsmUtil.asmIdToSignatures(methodSource.exceptions, signatureFactory)); String methodName = methodSource.name; EnumSet modifiers = Modifiers.getMethodModifiers(methodSource.access); - List sigTypes = AsmUtil.toJimpleSignatureDesc(methodSource.desc); + List sigTypes = + AsmUtil.toJimpleSignatureDesc(methodSource.desc, signatureFactory); Type retType = sigTypes.remove(sigTypes.size() - 1); MethodSignature methodSignature = @@ -130,16 +133,18 @@ private static Stream resolveMethods( methodSignature, modifiers, exceptions, - convertAnnotation(annotations), + convertAnnotation(annotations, signatureFactory), NoPositionInformation.getInstance()); }); } - protected static List convertAnnotation(List nodes) { + protected static List convertAnnotation( + List nodes, @NonNull IdentifierFactory identifierFactory) { if (nodes == null) { return Collections.emptyList(); } - return StreamSupport.stream(AsmUtil.createAnnotationUsage(nodes).spliterator(), false) + return StreamSupport.stream( + AsmUtil.createAnnotationUsage(nodes, identifierFactory).spliterator(), false) .collect(Collectors.toList()); } @@ -164,13 +169,12 @@ protected Iterable resolveAnnotations() { ? classNode.invisibleTypeAnnotations : Collections.emptyList()); - return convertAnnotation(annotationNodes); + return convertAnnotation(annotationNodes, identifierFactory); } @Override @NonNull public Collection resolveFields() throws ResolveException { - IdentifierFactory identifierFactory = JavaIdentifierFactory.getInstance(); return resolveFields(classNode.fields, identifierFactory, classSignature); } @@ -181,7 +185,7 @@ public EnumSet resolveModifiers() { @NonNull public Set resolveInterfaces() { - return new HashSet<>(AsmUtil.asmIdToSignatures(classNode.interfaces)); + return new HashSet<>(AsmUtil.asmIdToSignatures(classNode.interfaces, identifierFactory)); } @NonNull @@ -189,7 +193,7 @@ public Optional resolveSuperclass() { if (classNode.superName == null) { return Optional.empty(); } - return Optional.of(AsmUtil.toJimpleClassType(classNode.superName)); + return Optional.of(AsmUtil.toJimpleClassType(classNode.superName, identifierFactory)); } @NonNull @@ -197,7 +201,7 @@ public Optional resolveOuterClass() { if (classNode.outerClass == null) { return Optional.empty(); } - return Optional.of(AsmUtil.toJimpleClassType(classNode.outerClass)); + return Optional.of(AsmUtil.toJimpleClassType(classNode.outerClass, identifierFactory)); } @NonNull diff --git a/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmClassSource.java b/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmClassSource.java index 79e753d6e13..c8d9c431352 100644 --- a/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmClassSource.java +++ b/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmClassSource.java @@ -47,14 +47,17 @@ class AsmClassSource extends JavaSootClassSource { @NonNull private final ClassNode classNode; + @NonNull protected final IdentifierFactory identifierFactory; public AsmClassSource( @NonNull final AnalysisInputLocation inputLocation, @NonNull final Path sourcePath, @NonNull final ClassType javaClassType, - @NonNull final ClassNode classNode) { + @NonNull final ClassNode classNode, + @NonNull final IdentifierFactory identifierFactory) { super(inputLocation, javaClassType, sourcePath); this.classNode = classNode; + this.identifierFactory = identifierFactory; } private static Set resolveFields( @@ -63,7 +66,7 @@ private static Set resolveFields( .map( fieldNode -> { String fieldName = fieldNode.name; - Type fieldType = AsmUtil.toJimpleType(fieldNode.desc); + Type fieldType = AsmUtil.toJimpleType(fieldNode.desc, signatureFactory); FieldSignature fieldSignature = signatureFactory.getFieldSignature(fieldName, classSignature, fieldType); EnumSet modifiers = AsmUtil.getFieldModifiers(fieldNode.access); @@ -73,43 +76,44 @@ private static Set resolveFields( fieldSignature, modifiers, Streams.concat( - convertAnnotation(fieldNode.visibleAnnotations), - convertAnnotation(fieldNode.invisibleAnnotations)) + convertAnnotation(fieldNode.visibleAnnotations, signatureFactory), + convertAnnotation(fieldNode.invisibleAnnotations, signatureFactory)) .collect(Collectors.toList()), NoPositionInformation.getInstance()); }) .collect(Collectors.toSet()); } - protected static Stream convertAnnotation(List nodes) { + protected static Stream convertAnnotation( + List nodes, @NonNull IdentifierFactory identifierFactory) { if (nodes == null) { return Stream.empty(); } - return StreamSupport.stream(AsmUtil.createAnnotationUsage(nodes).spliterator(), false); + return StreamSupport.stream( + AsmUtil.createAnnotationUsage(nodes, identifierFactory).spliterator(), false); } /** Appends the converted annotations of {@code nodes} to {@code out} (no intermediate Stream). */ protected static void convertAnnotation( - @NonNull List out, List nodes) { - AsmUtil.createAnnotationUsage(nodes).forEach(out::add); + @NonNull List out, + List nodes, + @NonNull IdentifierFactory identifierFactory) { + AsmUtil.createAnnotationUsage(nodes, identifierFactory).forEach(out::add); } @Override protected Iterable resolveAnnotations() { Stream annotations = Streams.concat( - convertAnnotation(classNode.visibleAnnotations), - convertAnnotation(classNode.invisibleAnnotations), - convertAnnotation(classNode.visibleTypeAnnotations), - convertAnnotation(classNode.invisibleTypeAnnotations)); + convertAnnotation(classNode.visibleAnnotations, identifierFactory), + convertAnnotation(classNode.invisibleAnnotations, identifierFactory), + convertAnnotation(classNode.visibleTypeAnnotations, identifierFactory), + convertAnnotation(classNode.invisibleTypeAnnotations, identifierFactory)); return annotations.collect(Collectors.toList()); } @NonNull public Set resolveMethods() throws ResolveException { - // FIXME: [ms] don't create a new instance of the identifierfactory! - IdentifierFactory identifierFactory = JavaIdentifierFactory.getInstance(); - return classNode.methods.stream() .map( methodSource -> { @@ -117,11 +121,13 @@ public Set resolveMethods() throws ResolveException { asmClassClassSourceContent.setDeclaringClass(classSignature); List exceptions = - new ArrayList<>(AsmUtil.asmIdToSignatures(methodSource.exceptions)); + new ArrayList<>( + AsmUtil.asmIdToSignatures(methodSource.exceptions, identifierFactory)); String methodName = methodSource.name; EnumSet modifiers = Modifiers.getMethodModifiers(methodSource.access); - List sigTypes = AsmUtil.toJimpleSignatureDesc(methodSource.desc); + List sigTypes = + AsmUtil.toJimpleSignatureDesc(methodSource.desc, identifierFactory); Type retType = sigTypes.remove(sigTypes.size() - 1); MethodSignature methodSignature = @@ -136,8 +142,8 @@ public Set resolveMethods() throws ResolveException { // Locals instead (see AsmMethodSource#buildPreambleLocals). Collected into one list // (in/out) to avoid intermediate Stream allocation. List annotations = new ArrayList<>(); - convertAnnotation(annotations, methodSource.visibleAnnotations); - convertAnnotation(annotations, methodSource.invisibleAnnotations); + convertAnnotation(annotations, methodSource.visibleAnnotations, identifierFactory); + convertAnnotation(annotations, methodSource.invisibleAnnotations, identifierFactory); asmClassClassSourceContent.collectReturnTypeAnnotations(annotations); return new JavaSootMethod( @@ -154,7 +160,6 @@ public Set resolveMethods() throws ResolveException { @Override @NonNull public Set resolveFields() throws ResolveException { - IdentifierFactory identifierFactory = JavaIdentifierFactory.getInstance(); return resolveFields(classNode.fields, identifierFactory, classSignature); } @@ -165,7 +170,7 @@ public EnumSet resolveModifiers() { @NonNull public Set resolveInterfaces() { - return new HashSet<>(AsmUtil.asmIdToSignatures(classNode.interfaces)); + return new HashSet<>(AsmUtil.asmIdToSignatures(classNode.interfaces, identifierFactory)); } @NonNull @@ -173,7 +178,7 @@ public Optional resolveSuperclass() { if (classNode.superName == null) { return Optional.empty(); } - return Optional.of(AsmUtil.toJimpleClassType(classNode.superName)); + return Optional.of(AsmUtil.toJimpleClassType(classNode.superName, identifierFactory)); } @NonNull @@ -181,7 +186,7 @@ public Optional resolveOuterClass() { if (classNode.outerClass == null) { return Optional.empty(); } - return Optional.of(AsmUtil.toJimpleClassType(classNode.outerClass)); + return Optional.of(AsmUtil.toJimpleClassType(classNode.outerClass, identifierFactory)); } @NonNull diff --git a/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmJavaClassProvider.java b/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmJavaClassProvider.java index b1c863bcf81..c33a35f03a7 100644 --- a/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmJavaClassProvider.java +++ b/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmJavaClassProvider.java @@ -35,6 +35,7 @@ import org.objectweb.asm.tree.ClassNode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import sootup.core.IdentifierFactory; import sootup.core.frontend.PathbasedClassProvider; import sootup.core.frontend.SootClassSource; import sootup.core.inputlocation.AnalysisInputLocation; @@ -55,17 +56,20 @@ public AsmJavaClassProvider(@NonNull View view) { this.view = view; } - public static SootClassSource createClassSource( + public SootClassSource createClassSource( @NonNull final AnalysisInputLocation analysisInputLocation, @NonNull final Path sourcePath, @NonNull final ClassType classType, @NonNull final ClassNode classNode) { + final IdentifierFactory identifierFactory = view.getIdentifierFactory(); if ((classNode.access & Opcodes.ACC_ANNOTATION) == Opcodes.ACC_ANNOTATION) { - return new AsmAnnotationClassSource(analysisInputLocation, sourcePath, classType, classNode); + return new AsmAnnotationClassSource( + analysisInputLocation, sourcePath, classType, classNode, identifierFactory); } AsmClassSource asmClassSource = - new AsmClassSource(analysisInputLocation, sourcePath, classType, classNode); + new AsmClassSource( + analysisInputLocation, sourcePath, classType, classNode, identifierFactory); // copy and load the complete class at once into memory so the newly created asmClassSource can // release the memory and structures from the asm library diff --git a/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmMethodSource.java b/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmMethodSource.java index c6688589167..4975fa7f80f 100644 --- a/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmMethodSource.java +++ b/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmMethodSource.java @@ -157,7 +157,7 @@ private record ScopedTypeAnnotation(int startIdx, int endIdx, AnnotationUsage an lazyMethodSignature = Suppliers.memoize( () -> { - List sigTypes = AsmUtil.toJimpleSignatureDesc(desc); + List sigTypes = AsmUtil.toJimpleSignatureDesc(desc, identifierFactory); Type retType = sigTypes.remove(sigTypes.size() - 1); return identifierFactory.getMethodSignature(declaringClass, name, retType, sigTypes); @@ -279,7 +279,7 @@ public Object resolveAnnotationsDefaultValue() { private Object resolveAnnotationsInDefaultValue(Object a) { if (a instanceof AnnotationNode) { - return AsmUtil.createAnnotationUsage((AnnotationNode) a); + return AsmUtil.createAnnotationUsage((AnnotationNode) a, identifierFactory); } if (a instanceof ArrayList) { @@ -287,7 +287,7 @@ private Object resolveAnnotationsInDefaultValue(Object a) { ((ArrayList) a).forEach(e -> list.add(resolveAnnotationsInDefaultValue(e))); return list; } - return AsmUtil.convertAnnotationValue(a); + return AsmUtil.convertAnnotationValue(a, identifierFactory); } @NonNull @@ -421,7 +421,7 @@ private void indexLocalVariableAnnotations(@Nullable List paramTypes = - AsmUtil.toJimpleSignatureDesc(((org.objectweb.asm.Type) val).getDescriptor()); + AsmUtil.toJimpleSignatureDesc( + ((org.objectweb.asm.Type) val).getDescriptor(), identifierFactory); Type returnType = paramTypes.remove(paramTypes.size() - 1); - v = JavaJimple.newMethodType(paramTypes, returnType); + v = JavaJimple.newMethodType(paramTypes, returnType, identifierFactory); } else { - v = JavaJimple.newClassConstant(((org.objectweb.asm.Type) val).getDescriptor()); + v = + JavaJimple.newClassConstant( + ((org.objectweb.asm.Type) val).getDescriptor(), identifierFactory); } } else if (val instanceof Handle) { Handle h = (Handle) val; if (MethodHandle.isMethodRef(h.getTag())) { - v = JavaJimple.newMethodHandle(toMethodSignature((Handle) val), ((Handle) val).getTag()); + v = + JavaJimple.newMethodHandle( + toMethodSignature((Handle) val), ((Handle) val).getTag(), identifierFactory); } else { - v = JavaJimple.newMethodHandle(toSootFieldSignature((Handle) val), ((Handle) val).getTag()); + v = + JavaJimple.newMethodHandle( + toSootFieldSignature((Handle) val), ((Handle) val).getTag(), identifierFactory); } } else if (val instanceof ConstantDynamic) { ConstantDynamic cd = (ConstantDynamic) val; if (MethodHandle.isMethodRef(cd.getBootstrapMethod().getTag())) { v = JavaJimple.newMethodHandle( - toMethodSignature(cd.getBootstrapMethod()), cd.getBootstrapMethod().getTag()); + toMethodSignature(cd.getBootstrapMethod()), + cd.getBootstrapMethod().getTag(), + identifierFactory); } else { v = JavaJimple.newMethodHandle( - toSootFieldSignature(cd.getBootstrapMethod()), cd.getBootstrapMethod().getTag()); + toSootFieldSignature(cd.getBootstrapMethod()), + cd.getBootstrapMethod().getTag(), + identifierFactory); } } else { throw new UnsupportedOperationException("Unknown constant type: " + val.getClass()); @@ -1128,7 +1139,7 @@ private Immediate toSootValue(@NonNull Object val) throws UnsupportedOperationEx private JFieldRef toSootFieldRef(Handle methodHandle) { String bsmClsName = AsmUtil.toQualifiedName(methodHandle.getOwner()); JavaClassType bsmCls = identifierFactory.getClassType(bsmClsName); - Type t = AsmUtil.toJimpleSignatureDesc(methodHandle.getDesc()).get(0); + Type t = AsmUtil.toJimpleSignatureDesc(methodHandle.getDesc(), identifierFactory).get(0); int kind = methodHandle.getTag(); FieldSignature fieldSignature = identifierFactory.getFieldSignature(methodHandle.getName(), bsmCls, t); @@ -1144,14 +1155,15 @@ private JFieldRef toSootFieldRef(Handle methodHandle) { private FieldSignature toSootFieldSignature(Handle methodHandle) { String bsmClsName = AsmUtil.toQualifiedName(methodHandle.getOwner()); JavaClassType bsmCls = identifierFactory.getClassType(bsmClsName); - Type t = AsmUtil.toJimpleSignatureDesc(methodHandle.getDesc()).get(0); + Type t = AsmUtil.toJimpleSignatureDesc(methodHandle.getDesc(), identifierFactory).get(0); return identifierFactory.getFieldSignature(methodHandle.getName(), bsmCls, t); } private MethodSignature toMethodSignature(Handle methodHandle) { String bsmClsName = AsmUtil.toQualifiedName(methodHandle.getOwner()); JavaClassType bsmCls = identifierFactory.getClassType(bsmClsName); - List bsmSigTypes = AsmUtil.toJimpleSignatureDesc(methodHandle.getDesc()); + List bsmSigTypes = + AsmUtil.toJimpleSignatureDesc(methodHandle.getDesc(), identifierFactory); Type returnType = bsmSigTypes.remove(bsmSigTypes.size() - 1); return identifierFactory.getMethodSignature( bsmCls, methodHandle.getName(), returnType, bsmSigTypes); @@ -1189,7 +1201,7 @@ private void convertMethodInsn(@NonNull MethodInsnNode insn) { clsName = "java.lang.Object"; } JavaClassType cls = identifierFactory.getClassType(AsmUtil.toQualifiedName(clsName)); - List sigTypes = AsmUtil.toJimpleSignatureDesc(insn.desc); + List sigTypes = AsmUtil.toJimpleSignatureDesc(insn.desc, identifierFactory); Type returnType = sigTypes.remove((sigTypes.size() - 1)); MethodSignature methodSignature = identifierFactory.getMethodSignature(cls, insn.name, returnType, sigTypes); @@ -1269,7 +1281,7 @@ private void convertInvokeDynamicInsn(@NonNull InvokeDynamicInsnNode insn) { identifierFactory.getClassType(JDynamicInvokeExpr.INVOKEDYNAMIC_DUMMY_CLASS_NAME); // Generate parameters & returnType & parameterTypes - List types = AsmUtil.toJimpleSignatureDesc(insn.desc); + List types = AsmUtil.toJimpleSignatureDesc(insn.desc, identifierFactory); int nrArgs = types.size() - 1; Type[] parameterTypes = new Type[nrArgs]; Immediate[] methodArgs = new Immediate[nrArgs]; @@ -1317,7 +1329,7 @@ private void convertInvokeDynamicInsn(@NonNull InvokeDynamicInsnNode insn) { private void convertMultiANewArrayInsn(@NonNull MultiANewArrayInsnNode insn) { OperandMerging merging = operandStack.getOrCreateMerging(insn); - ArrayType t = (ArrayType) AsmUtil.toJimpleType(insn.desc); + ArrayType t = (ArrayType) AsmUtil.toJimpleType(insn.desc, identifierFactory); int dims = insn.dims; Operand[] sizes = new Operand[dims]; Immediate[] sizeVals = new Immediate[dims]; @@ -1358,7 +1370,7 @@ private void convertTypeInsn(@NonNull TypeInsnNode insn) { OperandMerging merging = operandStack.getOrCreateMerging(insn); Expr val; if (op == NEW) { - val = Jimple.newNewExpr(AsmUtil.toJimpleClassType(insn.desc)); + val = Jimple.newNewExpr(AsmUtil.toJimpleClassType(insn.desc, identifierFactory)); } else { Operand op1 = operandStack.pop(); merging.mergeInputs(op1); @@ -1367,14 +1379,16 @@ private void convertTypeInsn(@NonNull TypeInsnNode insn) { { val = JavaJimple.newNewArrayExpr( - AsmUtil.arrayTypetoJimpleType(insn.desc), + AsmUtil.arrayTypetoJimpleType(insn.desc, identifierFactory), op1.toImmediate(), - JavaIdentifierFactory.getInstance()); + identifierFactory); break; } case CHECKCAST: { - val = Jimple.newCastExpr(op1.toImmediate(), AsmUtil.arrayTypetoJimpleType(insn.desc)); + val = + Jimple.newCastExpr( + op1.toImmediate(), AsmUtil.arrayTypetoJimpleType(insn.desc, identifierFactory)); break; } case INSTANCEOF: @@ -1386,7 +1400,7 @@ private void convertTypeInsn(@NonNull TypeInsnNode insn) { // corrupting the Jimple AST type hierarchy and subsequent analyses. val = Jimple.newInstanceOfExpr( - op1.toImmediate(), AsmUtil.arrayTypetoJimpleType(insn.desc)); + op1.toImmediate(), AsmUtil.arrayTypetoJimpleType(insn.desc, identifierFactory)); break; } default: @@ -1485,7 +1499,7 @@ private void convertLabel(@NonNull LabelNode ln) { } OperandMerging merging = operandStack.getOrCreateMerging(ln); - JCaughtExceptionRef ref = JavaJimple.newCaughtExceptionRef(); + JCaughtExceptionRef ref = JavaJimple.newCaughtExceptionRef(identifierFactory); Operand opr = new Operand(ln, ref, this); merging.mergeOutput(opr); if (opr.stackLocal == null) { @@ -1563,7 +1577,7 @@ private void convert() { for (LabelNode handlerNode : trapHandler.keySet()) { if (inlineExceptionLabels.contains(handlerNode)) { // Catch the exception - JCaughtExceptionRef ref = JavaJimple.newCaughtExceptionRef(); + JCaughtExceptionRef ref = JavaJimple.newCaughtExceptionRef(identifierFactory); Local local = newStackLocal(); JIdentityStmt as = Jimple.newIdentityStmt(local, ref, getStmtPositionInfo()); @@ -1770,7 +1784,7 @@ private void collectFormalParameterTypeAnnotations( out, invisibleTypeAnnotations, TypeReference.METHOD_FORMAL_PARAMETER, formalParameterIndex); } - private static void collectMethodTypeAnnotations( + private void collectMethodTypeAnnotations( @NonNull List out, List nodes, int sort, @@ -1789,7 +1803,7 @@ private static void collectMethodTypeAnnotations( } // Note: node.typePath (the position of the annotation within a nested/generic type) is not // represented by AnnotationUsage; the annotation is attached to the parameter/return target. - out.add(AsmUtil.createAnnotationUsage(node)); + out.add(AsmUtil.createAnnotationUsage(node, identifierFactory)); } } @@ -1821,11 +1835,11 @@ private List buildPreambleLocals(Body.BodyBuilder bodyBuilder) { // METHOD_FORMAL_PARAMETER target) and attach all of them to the parameter Local. List parameterAnnotations = new ArrayList<>(); if (visibleParameterAnnotations != null) { - AsmUtil.createAnnotationUsage(visibleParameterAnnotations[i]) + AsmUtil.createAnnotationUsage(visibleParameterAnnotations[i], identifierFactory) .forEach(parameterAnnotations::add); } if (invisibleParameterAnnotations != null) { - AsmUtil.createAnnotationUsage(invisibleParameterAnnotations[i]) + AsmUtil.createAnnotationUsage(invisibleParameterAnnotations[i], identifierFactory) .forEach(parameterAnnotations::add); } collectFormalParameterTypeAnnotations(parameterAnnotations, i); diff --git a/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmModuleSource.java b/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmModuleSource.java index e5584e765c1..6dec2742fde 100644 --- a/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmModuleSource.java +++ b/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmModuleSource.java @@ -31,23 +31,27 @@ import org.jspecify.annotations.NonNull; import org.objectweb.asm.ClassReader; import org.objectweb.asm.tree.*; +import sootup.core.IdentifierFactory; import sootup.core.frontend.ResolveException; import sootup.java.core.JavaModuleIdentifierFactory; import sootup.java.core.JavaModuleInfo; import sootup.java.core.ModuleModifier; +import sootup.java.core.signatures.ModulePackageName; import sootup.java.core.signatures.ModuleSignature; import sootup.java.core.types.JavaClassType; public class AsmModuleSource extends JavaModuleInfo { @NonNull private final Path sourcePath; + @NonNull private final IdentifierFactory identifierFactory; @NonNull private final Supplier _lazyModule = Suppliers.memoize(this::_lazyModule); - public AsmModuleSource(@NonNull Path sourcePath) { + public AsmModuleSource(@NonNull Path sourcePath, @NonNull IdentifierFactory identifierFactory) { // if it would be an automatic module there would be no module-info.class super(); this.sourcePath = sourcePath; + this.identifierFactory = identifierFactory; } public static EnumSet getModuleModifiers(int access) { @@ -121,7 +125,6 @@ public Collection exports() { return Collections.emptyList(); } ArrayList exports = new ArrayList<>(module.exports.size()); - JavaModuleIdentifierFactory identifierFactory = JavaModuleIdentifierFactory.getInstance(); for (ModuleExportNode exportNode : module.exports) { ArrayList modules = new ArrayList<>(exportNode.modules.size()); for (String moduleName : exportNode.modules) { @@ -129,8 +132,7 @@ public Collection exports() { } JavaModuleInfo.PackageReference reference = new JavaModuleInfo.PackageReference( - identifierFactory.getPackageName( - exportNode.packaze.replace('/', '.'), getModuleSignature().toString()), + ModulePackageName.of(exportNode.packaze.replace('/', '.'), getModuleSignature()), getModuleModifiers(exportNode.access), modules); exports.add(reference); @@ -145,7 +147,6 @@ public Collection opens() { return Collections.emptyList(); } ArrayList opens = new ArrayList<>(module.opens.size()); - JavaModuleIdentifierFactory identifierFactory = JavaModuleIdentifierFactory.getInstance(); for (ModuleOpenNode openNode : module.opens) { ArrayList modules = new ArrayList<>(openNode.modules.size()); for (String moduleName : openNode.modules) { @@ -153,8 +154,7 @@ public Collection opens() { } JavaModuleInfo.PackageReference reference = new JavaModuleInfo.PackageReference( - identifierFactory.getPackageName( - openNode.packaze.replace('/', '.'), getModuleSignature().toString()), + ModulePackageName.of(openNode.packaze.replace('/', '.'), getModuleSignature()), getModuleModifiers(openNode.access), modules); opens.add(reference); @@ -171,12 +171,13 @@ public Collection provides() { ArrayList providers = new ArrayList<>(module.provides.size()); // add provides for (ModuleProvideNode moduleProvideNode : module.provides) { - JavaClassType serviceSignature = AsmUtil.toJimpleClassType(moduleProvideNode.service); + JavaClassType serviceSignature = + AsmUtil.toJimpleClassType(moduleProvideNode.service, identifierFactory); if (serviceSignature == null) { throw new IllegalStateException("provides entry without 'with' ."); } Iterable providersSignatures = - AsmUtil.asmIdToSignatures(moduleProvideNode.providers); + AsmUtil.asmIdToSignatures(moduleProvideNode.providers, identifierFactory); for (JavaClassType sootClassSignature : providersSignatures) { providers.add(new InterfaceReference(sootClassSignature, serviceSignature)); } @@ -194,7 +195,7 @@ public Collection uses() { ArrayList uses = new ArrayList<>(module.uses.size()); // add uses for (String usedService : module.uses) { - JavaClassType serviceSignature = AsmUtil.toJimpleClassType(usedService); + JavaClassType serviceSignature = AsmUtil.toJimpleClassType(usedService, identifierFactory); uses.add(serviceSignature); } diff --git a/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmUtil.java b/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmUtil.java index 05d30f75d35..509a5e77456 100644 --- a/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmUtil.java +++ b/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/conversion/AsmUtil.java @@ -44,6 +44,7 @@ import org.objectweb.asm.util.TraceMethodVisitor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import sootup.core.IdentifierFactory; import sootup.core.frontend.ResolveException; import sootup.core.jimple.common.constant.ClassConstant; import sootup.core.model.FieldModifier; @@ -101,23 +102,25 @@ public static EnumSet getFieldModifiers(int access) { @NonNull public static Collection asmIdToSignatures( - @Nullable Iterable asmClassNames) { + @Nullable Iterable asmClassNames, @NonNull IdentifierFactory identifierFactory) { if (asmClassNames == null) { return Collections.emptyList(); } return StreamSupport.stream(asmClassNames.spliterator(), false) - .map(AsmUtil::toJimpleClassType) + .map(name -> toJimpleClassType(name, identifierFactory)) .collect(Collectors.toList()); } @NonNull - public static JavaClassType toJimpleClassType(@NonNull String asmClassName) { - return JavaIdentifierFactory.getInstance().getClassType(toQualifiedName(asmClassName)); + public static JavaClassType toJimpleClassType( + @NonNull String asmClassName, @NonNull IdentifierFactory identifierFactory) { + return (JavaClassType) identifierFactory.getClassType(toQualifiedName(asmClassName)); } @NonNull - public static Type toJimpleType(@NonNull String desc) { + public static Type toJimpleType( + @NonNull String desc, @NonNull IdentifierFactory identifierFactory) { int nrDims = countArrayDim(desc); if (nrDims > 0) { desc = desc.substring(nrDims); @@ -132,22 +135,21 @@ public static Type toJimpleType(@NonNull String desc) { throw new AssertionError("Invalid reference descriptor: " + desc); } String name = desc.substring(1, desc.length() - 1); - baseType = JavaIdentifierFactory.getInstance().getType(toQualifiedName(name)); + baseType = identifierFactory.getType(toQualifiedName(name)); } if ((baseType instanceof PrimitiveType || baseType instanceof VoidType) && desc.length() > 1) { throw new AssertionError("Invalid primitive type descriptor: " + desc); } - return nrDims > 0 - ? JavaIdentifierFactory.getInstance().getArrayType(baseType, nrDims) - : baseType; + return nrDims > 0 ? identifierFactory.getArrayType(baseType, nrDims) : baseType; } @NonNull - public static Type arrayTypetoJimpleType(@NonNull String desc) { + public static Type arrayTypetoJimpleType( + @NonNull String desc, @NonNull IdentifierFactory identifierFactory) { if (desc.charAt(0) == '[') { - return toJimpleType(desc); + return toJimpleType(desc, identifierFactory); } - return toJimpleClassType(desc); + return toJimpleClassType(desc, identifierFactory); } /** returns the amount of dimensions of a description. */ @@ -183,9 +185,16 @@ private static Optional toPrimitiveOrVoidType(@NonNull String desc) { }; } - /** Converts n types contained in desc to a list of Jimple Types */ + /** + * Converts n types contained in desc to a list of Jimple Types + * + * @param desc the bytecode descriptor holding the types + * @param identifierFactory the factory that creates the reference types + * @return the converted types in the order they occur in {@code desc} + */ @NonNull - public static List toJimpleSignatureDesc(@NonNull String desc) { + public static List toJimpleSignatureDesc( + @NonNull String desc, @NonNull IdentifierFactory identifierFactory) { // [ms] more types are possibly needed for method type which is ( arg-type* ) ret-type List types = new ArrayList<>(1); int len = desc.length(); @@ -235,7 +244,7 @@ public static List toJimpleSignatureDesc(@NonNull String desc) { int begin = idx; idx = desc.indexOf(';', begin); String cls = desc.substring(begin, idx++); - baseType = JavaIdentifierFactory.getInstance().getType(toQualifiedName(cls)); + baseType = identifierFactory.getType(toQualifiedName(cls)); break this_type; default: throw new AssertionError("Unknown type: '" + c + "' in '" + desc + "'."); @@ -243,7 +252,7 @@ public static List toJimpleSignatureDesc(@NonNull String desc) { } if (baseType != null && nrDims > 0) { - types.add(JavaIdentifierFactory.getInstance().getArrayType(baseType, nrDims)); + types.add(identifierFactory.getArrayType(baseType, nrDims)); } else { types.add(baseType); } @@ -263,13 +272,17 @@ public static String toString(AbstractInsnNode insn) { .reduce("", String::concat); } - public static AnnotationUsage createAnnotationUsage(AnnotationNode annotationNode) { + public static AnnotationUsage createAnnotationUsage( + AnnotationNode annotationNode, @NonNull IdentifierFactory identifierFactory) { /* actually, we could move the inner loop's code (see below) here */ - return createAnnotationUsage(Collections.singletonList(annotationNode)).iterator().next(); + return createAnnotationUsage(Collections.singletonList(annotationNode), identifierFactory) + .iterator() + .next(); } public static Iterable createAnnotationUsage( - List invisibleParameterAnnotation) { + List invisibleParameterAnnotation, + @NonNull IdentifierFactory identifierFactory) { if (invisibleParameterAnnotation == null) { return Collections.emptyList(); } @@ -291,31 +304,38 @@ public static Iterable createAnnotationUsage( final List annotationValueList = ((ArrayList) annotationValue).stream().map(av -> (AnnotationNode) av).toList(); - paramMap.put(annotationName, createAnnotationUsage(annotationValueList)); + paramMap.put( + annotationName, createAnnotationUsage(annotationValueList, identifierFactory)); } else if (annotationValue instanceof AnnotationNode) { - paramMap.put(annotationName, createAnnotationUsage((AnnotationNode) annotationValue)); + paramMap.put( + annotationName, + createAnnotationUsage((AnnotationNode) annotationValue, identifierFactory)); } else { if (annotationValue instanceof ArrayList) { paramMap.put( annotationName, ((ArrayList) annotationValue) - .stream().map(AsmUtil::convertAnnotationValue).collect(Collectors.toList())); + .stream() + .map(av -> convertAnnotationValue(av, identifierFactory)) + .collect(Collectors.toList())); } else { - paramMap.put(annotationName, convertAnnotationValue(annotationValue)); + paramMap.put( + annotationName, convertAnnotationValue(annotationValue, identifierFactory)); } } } } JavaClassType at = - JavaIdentifierFactory.getInstance().getClassType(AsmUtil.toQualifiedName(e.desc)); + (JavaClassType) identifierFactory.getClassType(AsmUtil.toQualifiedName(e.desc)); annotationUsages.add(new AnnotationUsage(at, paramMap)); } return annotationUsages; } - public static Object convertAnnotationValue(Object annotationValue) { + public static Object convertAnnotationValue( + Object annotationValue, @NonNull IdentifierFactory identifierFactory) { if (annotationValue instanceof String[]) { // is an enum // [0] is the type of the enum @@ -323,17 +343,18 @@ public static Object convertAnnotationValue(Object annotationValue) { // transform the enum type to a fully qualified name String[] enumData = (String[]) annotationValue; enumData[0] = AsmUtil.toQualifiedName(enumData[0]); - return ConstantUtil.fromObject(enumData); + return ConstantUtil.fromObject(enumData, identifierFactory); } else { if (annotationValue instanceof org.objectweb.asm.Type) { // is a class constant // transform asm Type to ClassConstant ClassConstant classConstant = - JavaJimple.newClassConstant(((org.objectweb.asm.Type) annotationValue).toString()); - return ConstantUtil.fromObject(classConstant); + JavaJimple.newClassConstant( + ((org.objectweb.asm.Type) annotationValue).toString(), identifierFactory); + return ConstantUtil.fromObject(classConstant, identifierFactory); } } - return ConstantUtil.fromObject(annotationValue); + return ConstantUtil.fromObject(annotationValue, identifierFactory); } @NonNull diff --git a/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/inputlocation/JavaModulePathAnalysisInputLocation.java b/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/inputlocation/JavaModulePathAnalysisInputLocation.java index 0dbd6b18764..10abb7e1299 100644 --- a/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/inputlocation/JavaModulePathAnalysisInputLocation.java +++ b/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/inputlocation/JavaModulePathAnalysisInputLocation.java @@ -99,23 +99,32 @@ public JavaModulePathAnalysisInputLocation( @NonNull public Optional getModuleInfo(ModuleSignature sig, View view) { - return moduleFinder.getModuleInfo(sig); + return moduleFinder.getModuleInfo(sig, view.getIdentifierFactory()); } @NonNull public Set getModules(View view) { - return moduleFinder.getModules(); + return moduleFinder.getModules(view.getIdentifierFactory()); } - @Override + /** + * Returns the view's {@link IdentifierFactory}, which has to be module aware to name the classes + * found on a module path. + */ @NonNull - public Stream getClassSources(@NonNull View view) { + private static IdentifierFactory moduleIdentifierFactory(@NonNull View view) { IdentifierFactory identifierFactory = view.getIdentifierFactory(); Preconditions.checkArgument( identifierFactory instanceof JavaModuleIdentifierFactory, "Factory must be a JavaModuleSignatureFactory"); + return identifierFactory; + } - Collection allModules = moduleFinder.getAllModules(); + @Override + @NonNull + public Stream getClassSources(@NonNull View view) { + Collection allModules = + moduleFinder.getAllModules(moduleIdentifierFactory(view)); return allModules.stream().flatMap(sig -> getClassSourcesInternal(sig, view)); } @@ -135,17 +144,15 @@ public List getBodyInterceptors() { @NonNull public Stream getModulesClassSources( @NonNull ModuleSignature moduleSignature, @NonNull View view) { - IdentifierFactory identifierFactory = view.getIdentifierFactory(); - Preconditions.checkArgument( - identifierFactory instanceof JavaModuleIdentifierFactory, - "Factory must be a JavaModuleSignatureFactory"); + moduleIdentifierFactory(view); // checks that the view can name module classes return getClassSourcesInternal(moduleSignature, view); } protected Stream getClassSourcesInternal( @NonNull ModuleSignature moduleSignature, @NonNull View view) { - AnalysisInputLocation inputLocation = moduleFinder.getModule(moduleSignature); + AnalysisInputLocation inputLocation = + moduleFinder.getModule(moduleSignature, moduleIdentifierFactory(view)); if (inputLocation == null) { return Stream.empty(); } @@ -162,7 +169,8 @@ public Optional getClassSource( ModuleSignature modulename = ((ModulePackageName) klassType.getPackageName()).getModuleSignature(); // get inputlocation from cache - AnalysisInputLocation inputLocation = moduleFinder.getModule(modulename); + AnalysisInputLocation inputLocation = + moduleFinder.getModule(modulename, moduleIdentifierFactory(view)); if (inputLocation == null) { return Optional.empty(); diff --git a/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/inputlocation/JrtFileSystemAnalysisInputLocation.java b/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/inputlocation/JrtFileSystemAnalysisInputLocation.java index b71d3699fe1..6a7cacb62b4 100644 --- a/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/inputlocation/JrtFileSystemAnalysisInputLocation.java +++ b/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/inputlocation/JrtFileSystemAnalysisInputLocation.java @@ -184,7 +184,7 @@ protected PathbasedClassProvider getClassProvider(@NonNull View view) { @Override public @NonNull Stream getClassSources(@NonNull View view) { - Collection moduleSignatures = discoverModules(); + Collection moduleSignatures = discoverModules(view.getIdentifierFactory()); return moduleSignatures.stream() .flatMap(sig -> getClassSourcesInternal(sig, view.getIdentifierFactory(), view)); } @@ -192,10 +192,11 @@ protected PathbasedClassProvider getClassProvider(@NonNull View view) { /** * Discover and return all modules contained in the jrt filesystem. * + * @param identifierFactory the factory that creates the module signatures * @return Collection of found module names. */ @NonNull - public Collection discoverModules() { + public Collection discoverModules(@NonNull IdentifierFactory identifierFactory) { if (!isResolved) { final Path moduleRoot = theFileSystem.getPath("modules"); final String moduleInfoFilename = JavaModuleIdentifierFactory.MODULE_INFO_FILE + ".class"; @@ -207,7 +208,8 @@ public Collection discoverModules() { JavaModuleIdentifierFactory.getModuleSignature(entry.subpath(1, 2).toString()); Path moduleInfo = entry.resolve(moduleInfoFilename); if (Files.exists(moduleInfo)) { - moduleInfoMap.put(moduleSignature, new AsmModuleSource(moduleInfo)); + moduleInfoMap.put( + moduleSignature, new AsmModuleSource(moduleInfo, identifierFactory)); } else { moduleInfoMap.put( moduleSignature, JavaModuleInfo.createAutomaticModuleInfo(moduleSignature)); @@ -250,7 +252,7 @@ private JavaClassType fromPath( @Override public Optional getModuleInfo(ModuleSignature sig, View view) { if (!isResolved) { - discoverModules(); + discoverModules(view.getIdentifierFactory()); } return Optional.ofNullable(moduleInfoMap.get(sig)); } @@ -259,7 +261,7 @@ public Optional getModuleInfo(ModuleSignature sig, View view) { @Override public Set getModules(View view) { if (!isResolved) { - discoverModules(); + discoverModules(view.getIdentifierFactory()); } return Collections.unmodifiableSet(moduleInfoMap.keySet()); } diff --git a/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/inputlocation/ModuleFinder.java b/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/inputlocation/ModuleFinder.java index 145d5f980c0..660e02be749 100644 --- a/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/inputlocation/ModuleFinder.java +++ b/sootup.java.bytecode.frontend/src/main/java/sootup/java/bytecode/frontend/inputlocation/ModuleFinder.java @@ -36,6 +36,7 @@ import java.util.stream.Collectors; import org.jspecify.annotations.NonNull; import org.jspecify.annotations.Nullable; +import sootup.core.IdentifierFactory; import sootup.core.frontend.ResolveException; import sootup.core.inputlocation.AnalysisInputLocation; import sootup.core.interceptor.BodyInterceptor; @@ -118,17 +119,18 @@ public ModuleFinder(@NonNull Path modulePath) { } @NonNull - public Optional getModuleInfo(ModuleSignature sig) { + public Optional getModuleInfo( + ModuleSignature sig, @NonNull IdentifierFactory identifierFactory) { if (hasMoreToResolve()) { - getAllModules(); + getAllModules(identifierFactory); } return Optional.ofNullable(moduleInfoMap.get(sig)); } @NonNull - public Set getModules() { + public Set getModules(@NonNull IdentifierFactory identifierFactory) { if (hasMoreToResolve()) { - getAllModules(); + getAllModules(identifierFactory); } return Collections.unmodifiableSet(moduleInfoMap.keySet()); } @@ -137,10 +139,12 @@ public Set getModules() { * Returns the input location that manages the module. * * @param moduleName the module name + * @param identifierFactory the factory that creates the signatures of discovered modules * @return the input location that resolves classes contained in the module */ @Nullable - public AnalysisInputLocation getModule(@NonNull ModuleSignature moduleName) { + public AnalysisInputLocation getModule( + @NonNull ModuleSignature moduleName, @NonNull IdentifierFactory identifierFactory) { // check if module is cached AnalysisInputLocation inputLocationForModule = moduleInputLocation.get(moduleName); @@ -150,7 +154,7 @@ public AnalysisInputLocation getModule(@NonNull ModuleSignature moduleName) { // search iterative on the remaining entries of the modulePath for the module while (hasMoreToResolve()) { - discoverModulesIn(modulePathEntries.get(next++)); + discoverModulesIn(modulePathEntries.get(next++), identifierFactory); inputLocationForModule = moduleInputLocation.get(moduleName); if (inputLocationForModule != null) { return inputLocationForModule; @@ -162,13 +166,14 @@ public AnalysisInputLocation getModule(@NonNull ModuleSignature moduleName) { /** * Discover all modules in the module path. * + * @param identifierFactory the factory that creates the signatures of discovered modules * @return the names of all modules found */ @NonNull - public Collection getAllModules() { + public Collection getAllModules(@NonNull IdentifierFactory identifierFactory) { while (hasMoreToResolve()) { - discoverModulesIn(modulePathEntries.get(next++)); + discoverModulesIn(modulePathEntries.get(next++), identifierFactory); } return Collections.unmodifiableCollection(moduleInputLocation.keySet()); } @@ -182,7 +187,7 @@ public Collection getAllModules() { * * @param path the directory */ - private void discoverModulesIn(@NonNull Path path) { + private void discoverModulesIn(@NonNull Path path, @NonNull IdentifierFactory identifierFactory) { BasicFileAttributes attrs; try { attrs = Files.readAttributes(path, BasicFileAttributes.class); @@ -191,11 +196,11 @@ private void discoverModulesIn(@NonNull Path path) { } if (PathUtils.isArchive(path)) { - buildModuleForJar(path); + buildModuleForJar(path, identifierFactory); } else if (attrs.isDirectory()) { Path mi = path.resolve(JavaModuleIdentifierFactory.MODULE_INFO_FILE + ".class"); if (Files.exists(mi)) { - buildModuleForExplodedModule(path); + buildModuleForExplodedModule(path, identifierFactory); } try (DirectoryStream stream = Files.newDirectoryStream(path)) { @@ -209,10 +214,10 @@ private void discoverModulesIn(@NonNull Path path) { if (attrs.isDirectory()) { mi = entry.resolve(JavaModuleIdentifierFactory.MODULE_INFO_FILE + ".class"); if (Files.exists(mi)) { - buildModuleForExplodedModule(entry); + buildModuleForExplodedModule(entry, identifierFactory); } } else if (PathUtils.isArchive(entry)) { - buildModuleForJar(entry); + buildModuleForJar(entry, identifierFactory); } } } catch (Exception e) { @@ -221,7 +226,8 @@ private void discoverModulesIn(@NonNull Path path) { } } - private void buildModuleForExplodedModule(@NonNull Path dir) throws ResolveException { + private void buildModuleForExplodedModule( + @NonNull Path dir, @NonNull IdentifierFactory identifierFactory) throws ResolveException { // create the input location for this module dir PathBasedAnalysisInputLocation inputLocation = PathBasedAnalysisInputLocation.create(dir, sourceType, bodyInterceptors); @@ -231,7 +237,7 @@ private void buildModuleForExplodedModule(@NonNull Path dir) throws ResolveExcep return; } - JavaModuleInfo moduleInfo = new AsmModuleSource(moduleInfoFile); + JavaModuleInfo moduleInfo = new AsmModuleSource(moduleInfoFile, identifierFactory); JavaModuleInfo oldValue = moduleInfoMap.put(moduleInfo.getModuleSignature(), moduleInfo); moduleInputLocation.put(moduleInfo.getModuleSignature(), inputLocation); if (oldValue != null) { @@ -245,7 +251,7 @@ private void buildModuleForExplodedModule(@NonNull Path dir) throws ResolveExcep * * @param jar the jar file */ - private void buildModuleForJar(@NonNull Path jar) { + private void buildModuleForJar(@NonNull Path jar, @NonNull IdentifierFactory identifierFactory) { PathBasedAnalysisInputLocation inputLocation = PathBasedAnalysisInputLocation.create(jar, sourceType, bodyInterceptors); Path mi; @@ -254,7 +260,7 @@ private void buildModuleForJar(@NonNull Path jar) { mi = archiveRoot.resolve(JavaModuleIdentifierFactory.MODULE_INFO_FILE + ".class"); if (Files.exists(mi)) { - JavaModuleInfo moduleInfo = new AsmModuleSource(mi); + JavaModuleInfo moduleInfo = new AsmModuleSource(mi, identifierFactory); moduleInfoMap.put(moduleInfo.getModuleSignature(), moduleInfo); moduleInputLocation.put(moduleInfo.getModuleSignature(), inputLocation); } else { diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/conversion/AsmAnnotationElementOrderTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/conversion/AsmAnnotationElementOrderTest.java index 6f77ba09dbc..7de492e9dc8 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/conversion/AsmAnnotationElementOrderTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/conversion/AsmAnnotationElementOrderTest.java @@ -18,6 +18,7 @@ import sootup.core.model.SourceType; import sootup.java.bytecode.frontend.inputlocation.JavaClassPathAnalysisInputLocation; import sootup.java.core.AnnotationUsage; +import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.JavaSootClass; import sootup.java.core.JavaSootMethod; import sootup.java.core.views.JavaView; @@ -49,7 +50,7 @@ void directAsmUtilPreservesElementOrder() { "optional", Boolean.FALSE, "dereference", Boolean.TRUE); - AnnotationUsage usage = AsmUtil.createAnnotationUsage(node); + AnnotationUsage usage = AsmUtil.createAnnotationUsage(node, new JavaIdentifierFactory()); List keys = new ArrayList<>(usage.getValues().keySet()); assertEquals(Arrays.asList("selector", "strongRef", "optional", "dereference"), keys); } diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/conversion/AsmUtilTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/conversion/AsmUtilTest.java index dfd7f8bdd6f..ffa8293b576 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/conversion/AsmUtilTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/conversion/AsmUtilTest.java @@ -11,9 +11,12 @@ import sootup.core.types.PrimitiveType; import sootup.core.types.Type; import sootup.core.types.VoidType; +import sootup.java.core.JavaIdentifierFactory; public class AsmUtilTest { + private final JavaIdentifierFactory identifierFactory = new JavaIdentifierFactory(); + @Test public void testIsDword() { assertTrue(AsmUtil.isDWord(PrimitiveType.getLong())); @@ -30,86 +33,86 @@ public void testToQualifiedName() { @Test public void testToJimpleClassType() { - ClassType classType = AsmUtil.toJimpleClassType("Ljava/lang/Object;"); + ClassType classType = AsmUtil.toJimpleClassType("Ljava/lang/Object;", identifierFactory); assertEquals("java.lang.Object", classType.getFullyQualifiedName()); - ClassType classType2 = AsmUtil.toJimpleClassType("java/lang/Object"); + ClassType classType2 = AsmUtil.toJimpleClassType("java/lang/Object", identifierFactory); assertEquals("java.lang.Object", classType2.getFullyQualifiedName()); - ClassType classType3 = AsmUtil.toJimpleClassType("java.lang.Object"); + ClassType classType3 = AsmUtil.toJimpleClassType("java.lang.Object", identifierFactory); assertEquals("java.lang.Object", classType3.getFullyQualifiedName()); } @Test public void testToJimpleTypePrimitives() { - Type primitiveTypeZ = AsmUtil.toJimpleType("Z"); + Type primitiveTypeZ = AsmUtil.toJimpleType("Z", identifierFactory); assertTrue(primitiveTypeZ instanceof PrimitiveType.BooleanType); - Type primitiveTypeB = AsmUtil.toJimpleType("B"); + Type primitiveTypeB = AsmUtil.toJimpleType("B", identifierFactory); assertTrue(primitiveTypeB instanceof PrimitiveType.ByteType); - Type primitiveTypeC = AsmUtil.toJimpleType("C"); + Type primitiveTypeC = AsmUtil.toJimpleType("C", identifierFactory); assertTrue(primitiveTypeC instanceof PrimitiveType.CharType); - Type primitiveTypeS = AsmUtil.toJimpleType("S"); + Type primitiveTypeS = AsmUtil.toJimpleType("S", identifierFactory); assertTrue(primitiveTypeS instanceof PrimitiveType.ShortType); - Type primitiveTypeI = AsmUtil.toJimpleType("I"); + Type primitiveTypeI = AsmUtil.toJimpleType("I", identifierFactory); assertTrue(primitiveTypeI instanceof PrimitiveType.IntType); - Type primitiveTypeF = AsmUtil.toJimpleType("F"); + Type primitiveTypeF = AsmUtil.toJimpleType("F", identifierFactory); assertTrue(primitiveTypeF instanceof PrimitiveType.FloatType); - Type primitiveTypeJ = AsmUtil.toJimpleType("J"); + Type primitiveTypeJ = AsmUtil.toJimpleType("J", identifierFactory); assertTrue(primitiveTypeJ instanceof PrimitiveType.LongType); - Type primitiveTypeD = AsmUtil.toJimpleType("D"); + Type primitiveTypeD = AsmUtil.toJimpleType("D", identifierFactory); assertTrue(primitiveTypeD instanceof PrimitiveType.DoubleType); - Type primitiveTypeV = AsmUtil.toJimpleType("V"); + Type primitiveTypeV = AsmUtil.toJimpleType("V", identifierFactory); assertTrue(primitiveTypeV instanceof VoidType); } @Test public void testToJimpleTypeClass() { - Type classType = AsmUtil.toJimpleType("Ljava/lang/Object;"); + Type classType = AsmUtil.toJimpleType("Ljava/lang/Object;", identifierFactory); assertTrue(classType instanceof ClassType); assertEquals("java.lang.Object", ((ClassType) classType).getFullyQualifiedName()); } @Test public void testToJimpleTypeArrays() { - Type primitiveTypeZ = AsmUtil.toJimpleType("[Z"); + Type primitiveTypeZ = AsmUtil.toJimpleType("[Z", identifierFactory); assertTrue(primitiveTypeZ instanceof ArrayType); assertTrue(((ArrayType) primitiveTypeZ).getBaseType() instanceof PrimitiveType.BooleanType); assertEquals(((ArrayType) primitiveTypeZ).getDimension(), 1); - Type primitiveTypeB = AsmUtil.toJimpleType("[[B"); + Type primitiveTypeB = AsmUtil.toJimpleType("[[B", identifierFactory); assertTrue(primitiveTypeB instanceof ArrayType); assertTrue(((ArrayType) primitiveTypeB).getBaseType() instanceof PrimitiveType.ByteType); assertEquals(((ArrayType) primitiveTypeB).getDimension(), 2); - Type primitiveTypeC = AsmUtil.toJimpleType("[[[C"); + Type primitiveTypeC = AsmUtil.toJimpleType("[[[C", identifierFactory); assertTrue(primitiveTypeC instanceof ArrayType); assertTrue(((ArrayType) primitiveTypeC).getBaseType() instanceof PrimitiveType.CharType); assertEquals(((ArrayType) primitiveTypeC).getDimension(), 3); - Type primitiveTypeS = AsmUtil.toJimpleType("[[[[S"); + Type primitiveTypeS = AsmUtil.toJimpleType("[[[[S", identifierFactory); assertTrue(primitiveTypeS instanceof ArrayType); assertTrue(((ArrayType) primitiveTypeS).getBaseType() instanceof PrimitiveType.ShortType); assertEquals(((ArrayType) primitiveTypeS).getDimension(), 4); - Type primitiveTypeI = AsmUtil.toJimpleType("[[[[[I"); + Type primitiveTypeI = AsmUtil.toJimpleType("[[[[[I", identifierFactory); assertTrue(primitiveTypeI instanceof ArrayType); assertTrue(((ArrayType) primitiveTypeI).getBaseType() instanceof PrimitiveType.IntType); assertEquals(((ArrayType) primitiveTypeI).getDimension(), 5); - Type primitiveTypeF = AsmUtil.toJimpleType("[[[[F"); + Type primitiveTypeF = AsmUtil.toJimpleType("[[[[F", identifierFactory); assertTrue(primitiveTypeF instanceof ArrayType); assertTrue(((ArrayType) primitiveTypeF).getBaseType() instanceof PrimitiveType.FloatType); assertEquals(((ArrayType) primitiveTypeF).getDimension(), 4); - Type primitiveTypeJ = AsmUtil.toJimpleType("[[[J"); + Type primitiveTypeJ = AsmUtil.toJimpleType("[[[J", identifierFactory); assertTrue(primitiveTypeJ instanceof ArrayType); assertTrue(((ArrayType) primitiveTypeJ).getBaseType() instanceof PrimitiveType.LongType); assertEquals(((ArrayType) primitiveTypeJ).getDimension(), 3); - Type primitiveTypeD = AsmUtil.toJimpleType("[[D"); + Type primitiveTypeD = AsmUtil.toJimpleType("[[D", identifierFactory); assertTrue(primitiveTypeD instanceof ArrayType); assertTrue(((ArrayType) primitiveTypeD).getBaseType() instanceof PrimitiveType.DoubleType); assertEquals(((ArrayType) primitiveTypeD).getDimension(), 2); - Type classType = AsmUtil.toJimpleType("[Ljava.lang.Object;"); + Type classType = AsmUtil.toJimpleType("[Ljava.lang.Object;", identifierFactory); assertTrue(classType instanceof ArrayType); assertTrue(((ArrayType) classType).getBaseType() instanceof ClassType); assertEquals(((ArrayType) classType).getDimension(), 1); @@ -117,96 +120,101 @@ public void testToJimpleTypeArrays() { @Test public void testToJimpleTypeUnknownDescriptor() { - Assertions.assertThrows(AssertionError.class, () -> AsmUtil.toJimpleType("P")); + Assertions.assertThrows( + AssertionError.class, () -> AsmUtil.toJimpleType("P", identifierFactory)); } @Test() public void testToJimpleTypeInvalidRefDescriptor() { - Assertions.assertThrows(AssertionError.class, () -> AsmUtil.toJimpleType("L")); + Assertions.assertThrows( + AssertionError.class, () -> AsmUtil.toJimpleType("L", identifierFactory)); } @Test() public void testToJimpleTypeInvalidPrimitiveDescriptor() { - Assertions.assertThrows(AssertionError.class, () -> AsmUtil.toJimpleType("II")); + Assertions.assertThrows( + AssertionError.class, () -> AsmUtil.toJimpleType("II", identifierFactory)); } @Test() public void testToJimpleTypeInvalidVoidDescriptor() { - Assertions.assertThrows(AssertionError.class, () -> AsmUtil.toJimpleType("VI")); + Assertions.assertThrows( + AssertionError.class, () -> AsmUtil.toJimpleType("VI", identifierFactory)); } @Test() public void testToJimpleTypeIncorrectArray() { - Assertions.assertThrows(AssertionError.class, () -> AsmUtil.toJimpleType("[I[I")); + Assertions.assertThrows( + AssertionError.class, () -> AsmUtil.toJimpleType("[I[I", identifierFactory)); } @Test public void testArrayTypeToJimpleType() { - Type primitiveTypeZ = AsmUtil.arrayTypetoJimpleType("Z"); + Type primitiveTypeZ = AsmUtil.arrayTypetoJimpleType("Z", identifierFactory); assertTrue(primitiveTypeZ instanceof ClassType); - Type primitiveTypeB = AsmUtil.arrayTypetoJimpleType("B"); + Type primitiveTypeB = AsmUtil.arrayTypetoJimpleType("B", identifierFactory); assertTrue(primitiveTypeB instanceof ClassType); - Type primitiveTypeC = AsmUtil.arrayTypetoJimpleType("C"); + Type primitiveTypeC = AsmUtil.arrayTypetoJimpleType("C", identifierFactory); assertTrue(primitiveTypeC instanceof ClassType); - Type primitiveTypeS = AsmUtil.arrayTypetoJimpleType("S"); + Type primitiveTypeS = AsmUtil.arrayTypetoJimpleType("S", identifierFactory); assertTrue(primitiveTypeS instanceof ClassType); - Type primitiveTypeI = AsmUtil.arrayTypetoJimpleType("I"); + Type primitiveTypeI = AsmUtil.arrayTypetoJimpleType("I", identifierFactory); assertTrue(primitiveTypeI instanceof ClassType); - Type primitiveTypeF = AsmUtil.arrayTypetoJimpleType("F"); + Type primitiveTypeF = AsmUtil.arrayTypetoJimpleType("F", identifierFactory); assertTrue(primitiveTypeF instanceof ClassType); - Type primitiveTypeJ = AsmUtil.arrayTypetoJimpleType("J"); + Type primitiveTypeJ = AsmUtil.arrayTypetoJimpleType("J", identifierFactory); assertTrue(primitiveTypeJ instanceof ClassType); - Type primitiveTypeD = AsmUtil.arrayTypetoJimpleType("D"); + Type primitiveTypeD = AsmUtil.arrayTypetoJimpleType("D", identifierFactory); assertTrue(primitiveTypeD instanceof ClassType); - Type primitiveTypeV = AsmUtil.arrayTypetoJimpleType("V"); + Type primitiveTypeV = AsmUtil.arrayTypetoJimpleType("V", identifierFactory); assertTrue(primitiveTypeV instanceof ClassType); - Type classType = AsmUtil.arrayTypetoJimpleType("java/lang/Object"); + Type classType = AsmUtil.arrayTypetoJimpleType("java/lang/Object", identifierFactory); assertTrue(classType instanceof ClassType); assertEquals("java.lang.Object", ((ClassType) classType).getFullyQualifiedName()); - Type primitiveTypeZArray = AsmUtil.arrayTypetoJimpleType("[Z"); + Type primitiveTypeZArray = AsmUtil.arrayTypetoJimpleType("[Z", identifierFactory); assertTrue(primitiveTypeZArray instanceof ArrayType); assertTrue( ((ArrayType) primitiveTypeZArray).getBaseType() instanceof PrimitiveType.BooleanType); assertEquals(((ArrayType) primitiveTypeZArray).getDimension(), 1); - Type primitiveTypeBArray = AsmUtil.arrayTypetoJimpleType("[[B"); + Type primitiveTypeBArray = AsmUtil.arrayTypetoJimpleType("[[B", identifierFactory); assertTrue(primitiveTypeBArray instanceof ArrayType); assertTrue(((ArrayType) primitiveTypeBArray).getBaseType() instanceof PrimitiveType.ByteType); assertEquals(((ArrayType) primitiveTypeBArray).getDimension(), 2); - Type primitiveTypeCArray = AsmUtil.arrayTypetoJimpleType("[[[C"); + Type primitiveTypeCArray = AsmUtil.arrayTypetoJimpleType("[[[C", identifierFactory); assertTrue(primitiveTypeCArray instanceof ArrayType); assertTrue(((ArrayType) primitiveTypeCArray).getBaseType() instanceof PrimitiveType.CharType); assertEquals(((ArrayType) primitiveTypeCArray).getDimension(), 3); - Type primitiveTypeSArray = AsmUtil.arrayTypetoJimpleType("[[[[S"); + Type primitiveTypeSArray = AsmUtil.arrayTypetoJimpleType("[[[[S", identifierFactory); assertTrue(primitiveTypeSArray instanceof ArrayType); assertTrue(((ArrayType) primitiveTypeSArray).getBaseType() instanceof PrimitiveType.ShortType); assertEquals(((ArrayType) primitiveTypeSArray).getDimension(), 4); - Type primitiveTypeIArray = AsmUtil.arrayTypetoJimpleType("[[[[[I"); + Type primitiveTypeIArray = AsmUtil.arrayTypetoJimpleType("[[[[[I", identifierFactory); assertTrue(primitiveTypeIArray instanceof ArrayType); assertTrue(((ArrayType) primitiveTypeIArray).getBaseType() instanceof PrimitiveType.IntType); assertEquals(((ArrayType) primitiveTypeIArray).getDimension(), 5); - Type primitiveTypeFArray = AsmUtil.arrayTypetoJimpleType("[[[[F"); + Type primitiveTypeFArray = AsmUtil.arrayTypetoJimpleType("[[[[F", identifierFactory); assertTrue(primitiveTypeFArray instanceof ArrayType); assertTrue(((ArrayType) primitiveTypeFArray).getBaseType() instanceof PrimitiveType.FloatType); assertEquals(((ArrayType) primitiveTypeFArray).getDimension(), 4); - Type primitiveTypeJArray = AsmUtil.arrayTypetoJimpleType("[[[J"); + Type primitiveTypeJArray = AsmUtil.arrayTypetoJimpleType("[[[J", identifierFactory); assertTrue(primitiveTypeJArray instanceof ArrayType); assertTrue(((ArrayType) primitiveTypeJArray).getBaseType() instanceof PrimitiveType.LongType); assertEquals(((ArrayType) primitiveTypeJArray).getDimension(), 3); - Type primitiveTypeDArray = AsmUtil.arrayTypetoJimpleType("[[D"); + Type primitiveTypeDArray = AsmUtil.arrayTypetoJimpleType("[[D", identifierFactory); assertTrue(primitiveTypeDArray instanceof ArrayType); assertTrue(((ArrayType) primitiveTypeDArray).getBaseType() instanceof PrimitiveType.DoubleType); assertEquals(((ArrayType) primitiveTypeDArray).getDimension(), 2); - Type classTypeArray = AsmUtil.arrayTypetoJimpleType("[Ljava.lang.Object;"); + Type classTypeArray = AsmUtil.arrayTypetoJimpleType("[Ljava.lang.Object;", identifierFactory); assertTrue(classTypeArray instanceof ArrayType); assertTrue(((ArrayType) classTypeArray).getBaseType() instanceof ClassType); assertEquals(((ArrayType) classTypeArray).getDimension(), 1); diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/conversion/JavaModuleViewTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/conversion/JavaModuleViewTest.java index 85c591f9259..146384ce77d 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/conversion/JavaModuleViewTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/conversion/JavaModuleViewTest.java @@ -33,21 +33,20 @@ public void testGeneralClassReceivalFromModule() { JavaModuleView view = new JavaModuleView(analysisInputLocations, moduleInfoAnalysisInputLocations); ModuleJavaClassType targetClass = - JavaModuleIdentifierFactory.getInstance().getClassType("String", "java.lang", "java.base"); + new JavaModuleIdentifierFactory().getClassType("String", "java.lang", "java.base"); Optional aClass = view.getClass(targetClass); assertTrue(aClass.isPresent()); ModuleJavaClassType notExistingClass = - JavaModuleIdentifierFactory.getInstance().getClassType("Panty", "java.lang", "java.base"); + new JavaModuleIdentifierFactory().getClassType("Panty", "java.lang", "java.base"); assertFalse(view.getClass(notExistingClass).isPresent()); ModuleJavaClassType notExistingPackage = - JavaModuleIdentifierFactory.getInstance().getClassType("String", "java.kurz", "java.base"); + new JavaModuleIdentifierFactory().getClassType("String", "java.kurz", "java.base"); assertFalse(view.getClass(notExistingPackage).isPresent()); ModuleJavaClassType notExistingModule = - JavaModuleIdentifierFactory.getInstance() - .getClassType("String", "java.lang", "non.existent"); + new JavaModuleIdentifierFactory().getClassType("String", "java.lang", "non.existent"); assertFalse(view.getClass(notExistingModule).isPresent()); Optional moduleDescriptor = @@ -73,8 +72,7 @@ public void testUnnamedModule() { Collections.emptyList(); JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); - ModuleJavaClassType targetClass = - JavaModuleIdentifierFactory.getInstance().getClassType("A", "", ""); + ModuleJavaClassType targetClass = new JavaModuleIdentifierFactory().getClassType("A", "", ""); Optional aClass = view.getClass(targetClass); assertTrue(aClass.isPresent()); assertSame( @@ -82,7 +80,7 @@ public void testUnnamedModule() { JavaModuleInfo.getUnnamedModuleInfo().getModuleSignature()); ModuleJavaClassType targetClassWOModuleSig = - JavaModuleIdentifierFactory.getInstance().getClassType("A", ""); + new JavaModuleIdentifierFactory().getClassType("A", ""); Optional bClass = view.getClass(targetClassWOModuleSig); assertTrue(bClass.isPresent()); assertSame( @@ -109,25 +107,23 @@ public void testAnnotation() { JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); ModulePackageName modMain = - JavaModuleIdentifierFactory.getInstance().getPackageName("pkgmain", "modmain"); - ModulePackageName modB = - JavaModuleIdentifierFactory.getInstance().getPackageName("pkgb", "modb"); + new JavaModuleIdentifierFactory().getPackageName("pkgmain", "modmain"); + ModulePackageName modB = new JavaModuleIdentifierFactory().getPackageName("pkgb", "modb"); ModulePackageName modAnnotations = - JavaModuleIdentifierFactory.getInstance() - .getPackageName("pkgannotations", "mod.annotations"); + new JavaModuleIdentifierFactory().getPackageName("pkgannotations", "mod.annotations"); assertTrue(view.getModuleInfo(modMain.getModuleSignature()).isPresent()); assertTrue(view.getModuleInfo(modB.getModuleSignature()).isPresent()); assertTrue(view.getModuleInfo(modAnnotations.getModuleSignature()).isPresent()); ModuleJavaClassType ctAnno = - JavaModuleIdentifierFactory.getInstance() + new JavaModuleIdentifierFactory() .getClassType("CompileTimeAnnotation", "pkgannotations", "mod.annotations"); ModuleJavaClassType customAnno = - JavaModuleIdentifierFactory.getInstance() + new JavaModuleIdentifierFactory() .getClassType("ReallyCoolModule", "pkgannotations", "mod.annotations"); ModuleJavaClassType rtAnno = - JavaModuleIdentifierFactory.getInstance() + new JavaModuleIdentifierFactory() .getClassType("RunTimeAnnotation", "pkgannotations", "mod.annotations"); assertTrue(view.getClass(ctAnno).isPresent()); @@ -152,18 +148,16 @@ public void testRequiresStatic() { JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); ModulePackageName modMain = - JavaModuleIdentifierFactory.getInstance().getPackageName("pkgmain", "modmain"); - ModulePackageName modB = - JavaModuleIdentifierFactory.getInstance().getPackageName("pkgb", "modb"); - ModulePackageName modC = - JavaModuleIdentifierFactory.getInstance().getPackageName("pkgc", "modc"); + new JavaModuleIdentifierFactory().getPackageName("pkgmain", "modmain"); + ModulePackageName modB = new JavaModuleIdentifierFactory().getPackageName("pkgb", "modb"); + ModulePackageName modC = new JavaModuleIdentifierFactory().getPackageName("pkgc", "modc"); assertTrue(view.getModuleInfo(modMain.getModuleSignature()).isPresent()); assertTrue(view.getModuleInfo(modB.getModuleSignature()).isPresent()); assertTrue(view.getModuleInfo(modC.getModuleSignature()).isPresent()); ModuleJavaClassType cClass = - JavaModuleIdentifierFactory.getInstance().getClassType("C", "pkgc", "modc"); + new JavaModuleIdentifierFactory().getClassType("C", "pkgc", "modc"); assertTrue(view.getClass(cClass).isPresent()); @@ -172,7 +166,7 @@ public void testRequiresStatic() { assertTrue(view.getClass(modMain, cClass).isPresent()); ModuleJavaClassType targetClass = - JavaModuleIdentifierFactory.getInstance().getClassType("String", "java.lang", "java.base"); + new JavaModuleIdentifierFactory().getClassType("String", "java.lang", "java.base"); assertTrue(view.getClass(targetClass).isPresent()); assertTrue(view.getClass(modMain, targetClass).isPresent()); assertTrue(view.getClass(modB, targetClass).isPresent()); @@ -191,11 +185,9 @@ public void testRequiresExport() { JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); ModulePackageName modMain = - JavaModuleIdentifierFactory.getInstance().getPackageName("pkgmain", "modmain"); - ModulePackageName modB = - JavaModuleIdentifierFactory.getInstance().getPackageName("pkgb", "modb"); - ModulePackageName modC = - JavaModuleIdentifierFactory.getInstance().getPackageName("pkgc", "modc"); + new JavaModuleIdentifierFactory().getPackageName("pkgmain", "modmain"); + ModulePackageName modB = new JavaModuleIdentifierFactory().getPackageName("pkgb", "modb"); + ModulePackageName modC = new JavaModuleIdentifierFactory().getPackageName("pkgc", "modc"); Optional moduleInfoMain = view.getModuleInfo(modMain.getModuleSignature()); assertTrue(moduleInfoMain.isPresent()); @@ -209,7 +201,7 @@ public void testRequiresExport() { .anyMatch(reqs -> reqs.getModuleSignature().equals(modB.getModuleSignature()))); ModuleJavaClassType targetClassMain = - JavaModuleIdentifierFactory.getInstance().getClassType("Main", "pkgmain", "modmain"); + new JavaModuleIdentifierFactory().getClassType("Main", "pkgmain", "modmain"); assertTrue(view.getClass(modMain, targetClassMain).isPresent()); // ModB @@ -219,13 +211,13 @@ public void testRequiresExport() { .anyMatch(reqs -> reqs.getModuleSignature().equals(modC.getModuleSignature()))); ModuleJavaClassType targetClassB = - JavaModuleIdentifierFactory.getInstance().getClassType("B", "pkgb", "modb"); + new JavaModuleIdentifierFactory().getClassType("B", "pkgb", "modb"); assertTrue(view.getClass(modB, targetClassB).isPresent()); assertTrue(view.getClass(modMain, targetClassB).isPresent()); // ModC ModuleJavaClassType targetClassC = - JavaModuleIdentifierFactory.getInstance().getClassType("C", "pkgc", "modc"); + new JavaModuleIdentifierFactory().getClassType("C", "pkgc", "modc"); assertTrue(view.getClass(modC, targetClassC).isPresent()); } @@ -242,11 +234,9 @@ public void testRequiresTransitiveExport() { JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); ModulePackageName modMain = - JavaModuleIdentifierFactory.getInstance().getPackageName("pkgmain", "modmain"); - ModulePackageName modA = - JavaModuleIdentifierFactory.getInstance().getPackageName("pkga", "moda"); - ModulePackageName modC = - JavaModuleIdentifierFactory.getInstance().getPackageName("pkgc", "modc"); + new JavaModuleIdentifierFactory().getPackageName("pkgmain", "modmain"); + ModulePackageName modA = new JavaModuleIdentifierFactory().getPackageName("pkga", "moda"); + ModulePackageName modC = new JavaModuleIdentifierFactory().getPackageName("pkgc", "modc"); // ModMain Optional moduleInfoMain = view.getModuleInfo(modMain.getModuleSignature()); @@ -257,19 +247,19 @@ public void testRequiresTransitiveExport() { .anyMatch(reqs -> reqs.getModuleSignature().equals(modA.getModuleSignature()))); ModuleJavaClassType targetClassMain = - JavaModuleIdentifierFactory.getInstance().getClassType("Main", "pkgmain", "modmain"); + new JavaModuleIdentifierFactory().getClassType("Main", "pkgmain", "modmain"); assertTrue(view.getClass(modMain, targetClassMain).isPresent()); // ModC ModuleJavaClassType targetClassC = - JavaModuleIdentifierFactory.getInstance().getClassType("C", "pkgc", "modc"); + new JavaModuleIdentifierFactory().getClassType("C", "pkgc", "modc"); assertTrue(view.getClass(modC, targetClassC).isPresent()); // A -> C assertTrue(view.getClass(modA, targetClassC).isPresent()); // modmain -> moda ModuleJavaClassType mainToAClass = - JavaModuleIdentifierFactory.getInstance().getClassType("A1", "pkga1", "moda"); + new JavaModuleIdentifierFactory().getClassType("A1", "pkga1", "moda"); assertTrue(view.getClass(modA, mainToAClass).isPresent()); assertTrue(view.getClass(modMain, mainToAClass).isPresent()); @@ -277,7 +267,7 @@ public void testRequiresTransitiveExport() { assertTrue(view.getClass(modMain, targetClassC).isPresent()); ModuleJavaClassType targetClassFromJavaBase = - JavaModuleIdentifierFactory.getInstance().getClassType("String", "java.lang", "java.base"); + new JavaModuleIdentifierFactory().getClassType("String", "java.lang", "java.base"); assertTrue(view.getClass(modMain, targetClassFromJavaBase).isPresent()); } @@ -290,22 +280,22 @@ public void testReflection() { JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); ModuleJavaClassType mainClass = - JavaModuleIdentifierFactory.getInstance().getClassType("Main", "pkgmain", "modmain"); + new JavaModuleIdentifierFactory().getClassType("Main", "pkgmain", "modmain"); Optional mainClassOpt = view.getClass(mainClass); assertTrue(mainClassOpt.isPresent()); ModuleJavaClassType bClass = - JavaModuleIdentifierFactory.getInstance().getClassType("B", "pkgb", "modb"); + new JavaModuleIdentifierFactory().getClassType("B", "pkgb", "modb"); Optional bClassOpt = view.getClass(bClass); assertTrue(bClassOpt.isPresent()); ModuleJavaClassType b1Class = - JavaModuleIdentifierFactory.getInstance().getClassType("B1", "pkgb1", "modb"); + new JavaModuleIdentifierFactory().getClassType("B1", "pkgb1", "modb"); Optional b1ClassOpt = view.getClass(b1Class); assertTrue(b1ClassOpt.isPresent()); ModuleJavaClassType biClass = - JavaModuleIdentifierFactory.getInstance().getClassType("InternalB", "pkgbinternal", "modb"); + new JavaModuleIdentifierFactory().getClassType("InternalB", "pkgbinternal", "modb"); Optional biClassOpt = view.getClass(biClass); assertTrue(biClassOpt.isPresent()); @@ -343,12 +333,12 @@ public void testUsesProvide() { JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); ModuleJavaClassType mainModmainSig = - JavaModuleIdentifierFactory.getInstance().getClassType("Main", "pkgmain", "modmain"); + new JavaModuleIdentifierFactory().getClassType("Main", "pkgmain", "modmain"); Optional mainModmainClass = view.getClass(mainModmainSig); assertTrue(mainModmainClass.isPresent()); ModuleJavaClassType serviceDefSig = - JavaModuleIdentifierFactory.getInstance() + new JavaModuleIdentifierFactory() .getClassType("IService", "myservice", "modservicedefinition"); Optional serviceDefClass = view.getClass(serviceDefSig); assertTrue(serviceDefClass.isPresent()); @@ -358,7 +348,7 @@ public void testUsesProvide() { assertEquals(serviceDefAccessClass.get(), serviceDefClass.get()); ModuleJavaClassType serviceImplSig = - JavaModuleIdentifierFactory.getInstance() + new JavaModuleIdentifierFactory() .getClassType("ServiceImpl", "com.service.impl", "modservice.impl.com"); Optional serviceImplClass = view.getClass(serviceImplSig); assertTrue(serviceImplClass.isPresent()); @@ -368,7 +358,7 @@ public void testUsesProvide() { assertEquals(serviceImplAccessClass.get(), serviceImplClass.get()); ModuleJavaClassType serviceImplNetSig = - JavaModuleIdentifierFactory.getInstance() + new JavaModuleIdentifierFactory() .getClassType("ServiceImpl", "net.service.impl", "modservice.impl.net"); Optional serviceImplNetClass = view.getClass(serviceImplNetSig); assertTrue(serviceImplNetClass.isPresent()); @@ -391,12 +381,12 @@ public void testUsesProvideInClient() { JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); ModuleJavaClassType mainModmainSig = - JavaModuleIdentifierFactory.getInstance().getClassType("Main", "pkgmain", "modmain"); + new JavaModuleIdentifierFactory().getClassType("Main", "pkgmain", "modmain"); Optional mainModmainClass = view.getClass(mainModmainSig); assertTrue(mainModmainClass.isPresent()); ModuleJavaClassType serviceDefSig = - JavaModuleIdentifierFactory.getInstance() + new JavaModuleIdentifierFactory() .getClassType("IService", "myservice", "modservicedefinition"); Optional serviceDefClass = view.getClass(serviceDefSig); assertTrue(serviceDefClass.isPresent()); @@ -406,7 +396,7 @@ public void testUsesProvideInClient() { assertEquals(serviceDefAccessClass.get(), serviceDefClass.get()); ModuleJavaClassType serviceImplSig = - JavaModuleIdentifierFactory.getInstance() + new JavaModuleIdentifierFactory() .getClassType("ServiceImpl", "com.service.impl", "modservice.impl.com"); Optional serviceImplClass = view.getClass(serviceImplSig); assertTrue(serviceImplClass.isPresent()); @@ -416,7 +406,7 @@ public void testUsesProvideInClient() { assertEquals(serviceImplAccessClass.get(), serviceImplClass.get()); ModuleJavaClassType serviceImplNetSig = - JavaModuleIdentifierFactory.getInstance() + new JavaModuleIdentifierFactory() .getClassType("ServiceImpl", "net.service.impl", "modservice.impl.net"); Optional serviceImplNetClass = view.getClass(serviceImplNetSig); assertTrue(serviceImplNetClass.isPresent()); @@ -441,39 +431,37 @@ public void testDerivedPrivatePackageProtected() { JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); ModuleJavaClassType mainClass = - JavaModuleIdentifierFactory.getInstance().getClassType("Main", "pkgmain", "modmain"); + new JavaModuleIdentifierFactory().getClassType("Main", "pkgmain", "modmain"); assertTrue(view.getClass(mainClass).isPresent()); ModuleJavaClassType bClass = - JavaModuleIdentifierFactory.getInstance().getClassType("B", "pkgb", "modb"); + new JavaModuleIdentifierFactory().getClassType("B", "pkgb", "modb"); assertTrue(view.getClass(bClass).isPresent()); assertTrue(view.getClass(mainClass.getPackageName(), bClass).isPresent()); ModuleJavaClassType dataClass = - JavaModuleIdentifierFactory.getInstance().getClassType("Data", "pkgb", "modb"); + new JavaModuleIdentifierFactory().getClassType("Data", "pkgb", "modb"); assertTrue(view.getClass(dataClass).isPresent()); assertTrue(view.getClass(mainClass.getPackageName(), dataClass).isPresent()); ModuleJavaClassType dataFactoryClass = - JavaModuleIdentifierFactory.getInstance().getClassType("DataFactory", "pkgb", "modb"); + new JavaModuleIdentifierFactory().getClassType("DataFactory", "pkgb", "modb"); assertTrue(view.getClass(dataFactoryClass).isPresent()); assertTrue(view.getClass(mainClass.getPackageName(), dataFactoryClass).isPresent()); ModuleJavaClassType internalBHelperClass = - JavaModuleIdentifierFactory.getInstance() - .getClassType("InternalBHelper", "pkgbinternal", "modb"); + new JavaModuleIdentifierFactory().getClassType("InternalBHelper", "pkgbinternal", "modb"); assertTrue(view.getClass(internalBHelperClass).isPresent()); assertFalse(view.getClass(mainClass.getPackageName(), internalBHelperClass).isPresent()); ModuleJavaClassType internalBSuperClass = - JavaModuleIdentifierFactory.getInstance() + new JavaModuleIdentifierFactory() .getClassType("InternalBSuperClass", "pkgbinternal", "modb"); assertTrue(view.getClass(internalBSuperClass).isPresent()); assertFalse(view.getClass(mainClass.getPackageName(), internalBSuperClass).isPresent()); ModuleJavaClassType InternalDataClass = - JavaModuleIdentifierFactory.getInstance() - .getClassType("InternalData", "pkgbinternal", "modb"); + new JavaModuleIdentifierFactory().getClassType("InternalData", "pkgbinternal", "modb"); assertTrue(view.getClass(InternalDataClass).isPresent()); assertFalse(view.getClass(mainClass.getPackageName(), InternalDataClass).isPresent()); } @@ -488,28 +476,27 @@ public void testExceptions() { JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); ModuleJavaClassType mainClass = - JavaModuleIdentifierFactory.getInstance().getClassType("Main", "pkgmain", "modmain"); + new JavaModuleIdentifierFactory().getClassType("Main", "pkgmain", "modmain"); assertTrue(view.getClass(mainClass).isPresent()); ModuleJavaClassType exceptionClass = - JavaModuleIdentifierFactory.getInstance().getClassType("MyException", "pkgb", "modb"); + new JavaModuleIdentifierFactory().getClassType("MyException", "pkgb", "modb"); assertTrue(view.getClass(exceptionClass).isPresent()); assertTrue(view.getClass(mainClass.getPackageName(), exceptionClass).isPresent()); ModuleJavaClassType internalExceptionClass = - JavaModuleIdentifierFactory.getInstance() + new JavaModuleIdentifierFactory() .getClassType("MyInternalException", "pkgbinternal", "modb"); assertTrue(view.getClass(internalExceptionClass).isPresent()); assertFalse(view.getClass(mainClass.getPackageName(), internalExceptionClass).isPresent()); ModuleJavaClassType runtimeExceptionClass = - JavaModuleIdentifierFactory.getInstance() - .getClassType("MyRuntimeException", "pkgb", "modb"); + new JavaModuleIdentifierFactory().getClassType("MyRuntimeException", "pkgb", "modb"); assertTrue(view.getClass(runtimeExceptionClass).isPresent()); assertTrue(view.getClass(mainClass.getPackageName(), runtimeExceptionClass).isPresent()); ModuleJavaClassType internalRuntimeExceptionClass = - JavaModuleIdentifierFactory.getInstance() + new JavaModuleIdentifierFactory() .getClassType("MyInternalRuntimeException", "pkgbinternal", "modb"); assertTrue(view.getClass(internalRuntimeExceptionClass).isPresent()); assertFalse( @@ -526,23 +513,22 @@ public void testInterfaceCallback() { JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); ModuleJavaClassType mainClass = - JavaModuleIdentifierFactory.getInstance().getClassType("Main", "pkgmain", "modmain"); + new JavaModuleIdentifierFactory().getClassType("Main", "pkgmain", "modmain"); assertTrue(view.getClass(mainClass).isPresent()); ModuleJavaClassType calleeClass = - JavaModuleIdentifierFactory.getInstance().getClassType("Callee", "pkgcallee", "modcallee"); + new JavaModuleIdentifierFactory().getClassType("Callee", "pkgcallee", "modcallee"); assertTrue(view.getClass(calleeClass).isPresent()); assertTrue(view.getClass(mainClass.getPackageName(), calleeClass).isPresent()); ModuleJavaClassType iCallbackClass = - JavaModuleIdentifierFactory.getInstance() - .getClassType("ICallback", "pkgcallee", "modcallee"); + new JavaModuleIdentifierFactory().getClassType("ICallback", "pkgcallee", "modcallee"); assertTrue(view.getClass(iCallbackClass).isPresent()); assertTrue(view.getClass(mainClass.getPackageName(), iCallbackClass).isPresent()); assertTrue(view.getClass(calleeClass.getPackageName(), iCallbackClass).isPresent()); ModuleJavaClassType handlerClass = - JavaModuleIdentifierFactory.getInstance() + new JavaModuleIdentifierFactory() .getClassType("MyCallbackImpl", "pkgcallbackhandler", "modcallbackhandler"); assertTrue(view.getClass(handlerClass).isPresent()); assertTrue(view.getClass(mainClass.getPackageName(), handlerClass).isPresent()); @@ -566,19 +552,17 @@ public void testSplitpackageAutomaticModules() { assertEquals(3, view.getNamedModules().size()); ModulePackageName modmain = - JavaModuleIdentifierFactory.getInstance().getPackageName("pkgmain", "modmain"); + new JavaModuleIdentifierFactory().getPackageName("pkgmain", "modmain"); ModuleJavaClassType mainClass = - JavaModuleIdentifierFactory.getInstance().getClassType("Main", "pkgmain", "modmain"); + new JavaModuleIdentifierFactory().getClassType("Main", "pkgmain", "modmain"); assertTrue(view.getClass(mainClass).isPresent()); ModuleJavaClassType v1Class = - JavaModuleIdentifierFactory.getInstance() - .getClassType("Version1", "pkgsplitted", "modauto1"); + new JavaModuleIdentifierFactory().getClassType("Version1", "pkgsplitted", "modauto1"); assertTrue(view.getClass(v1Class).isPresent()); ModuleJavaClassType v2Class = - JavaModuleIdentifierFactory.getInstance() - .getClassType("Version2", "pkgsplitted", "modauto2"); + new JavaModuleIdentifierFactory().getClassType("Version2", "pkgsplitted", "modauto2"); assertTrue(view.getClass(v2Class).isPresent()); assertTrue(view.getClass(modmain, v1Class).isPresent()); @@ -600,7 +584,7 @@ public void testSplitpackage() { JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); ModuleJavaClassType targetClass = - JavaModuleIdentifierFactory.getInstance().getClassType("String", "java.lang", "java.base"); + new JavaModuleIdentifierFactory().getClassType("String", "java.lang", "java.base"); Optional aClass = view.getClass(targetClass); assertTrue(aClass.isPresent()); fail("test module descriptor/rights"); @@ -617,12 +601,11 @@ public void testHiddenMain() { JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); ModuleJavaClassType targetClass = - JavaModuleIdentifierFactory.getInstance().getClassType("Main", "pkgmain", "modmain"); + new JavaModuleIdentifierFactory().getClassType("Main", "pkgmain", "modmain"); assertTrue(view.getClass(targetClass).isPresent()); ModuleJavaClassType hiddenMain = - JavaModuleIdentifierFactory.getInstance() - .getClassType("HiddenMain", "pkgmainhidden", "modmain"); + new JavaModuleIdentifierFactory().getClassType("HiddenMain", "pkgmainhidden", "modmain"); assertTrue(view.getClass(hiddenMain).isPresent()); } @@ -642,11 +625,10 @@ public void testAccessUnnamedModuleFromAutomaticModule() { JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); ModuleJavaClassType mainClass = - JavaModuleIdentifierFactory.getInstance().getClassType("Main", "pkgmain", "modmain.auto"); + new JavaModuleIdentifierFactory().getClassType("Main", "pkgmain", "modmain.auto"); assertTrue(view.getClass(mainClass).isPresent()); - ModuleJavaClassType aClass = - JavaModuleIdentifierFactory.getInstance().getClassType("A", "pkga", ""); + ModuleJavaClassType aClass = new JavaModuleIdentifierFactory().getClassType("A", "pkga", ""); assertTrue(view.getClass(aClass).isPresent()); assertTrue(view.getClass(mainClass.getPackageName(), aClass).isPresent()); @@ -672,23 +654,22 @@ public void testAccessUnnamedModuleFromModule() { Paths.get(testPath + "unnamed-module_access-from-explicit-module/jar/modmain.jar"))); JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); - ModulePackageName cpb = JavaModuleIdentifierFactory.getInstance().getPackageName("pkgb", ""); + ModulePackageName cpb = new JavaModuleIdentifierFactory().getPackageName("pkgb", ""); JavaModuleInfo moduleInfo_cpb = view.getModuleInfo(cpb.getModuleSignature()).get(); assertTrue(moduleInfo_cpb.isUnnamedModule()); - ModulePackageName pkgbModb = - JavaModuleIdentifierFactory.getInstance().getPackageName("pkgb", "modb"); + ModulePackageName pkgbModb = new JavaModuleIdentifierFactory().getPackageName("pkgb", "modb"); JavaModuleInfo moduleInfo_pkgbModb = view.getModuleInfo(pkgbModb.getModuleSignature()).get(); assertFalse(moduleInfo_pkgbModb.isUnnamedModule()); ModulePackageName modmain = - JavaModuleIdentifierFactory.getInstance().getPackageName("pkgcpmain", "modmain"); + new JavaModuleIdentifierFactory().getPackageName("pkgcpmain", "modmain"); JavaModuleInfo moduleInfo_cpmain = view.getModuleInfo(modmain.getModuleSignature()).get(); assertFalse(moduleInfo_cpmain.isUnnamedModule()); ModuleJavaClassType BFromClasspath = - JavaModuleIdentifierFactory.getInstance().getClassType("BFromClasspath", "pkgb", ""); + new JavaModuleIdentifierFactory().getClassType("BFromClasspath", "pkgb", ""); assertTrue(view.getClass(BFromClasspath).isPresent()); assertFalse(view.getClass(modmain, BFromClasspath).isPresent()); @@ -720,58 +701,56 @@ public void testAccessModuleFromUnnamedModule() { JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); - ModulePackageName pkgbModb = - JavaModuleIdentifierFactory.getInstance().getPackageName("pkgb", "modb"); + ModulePackageName pkgbModb = new JavaModuleIdentifierFactory().getPackageName("pkgb", "modb"); JavaModuleInfo moduleInfo_pkgbModb = view.getModuleInfo(pkgbModb.getModuleSignature()).get(); assertFalse(moduleInfo_pkgbModb.isUnnamedModule()); - ModulePackageName cpb = JavaModuleIdentifierFactory.getInstance().getPackageName("pkgb", ""); + ModulePackageName cpb = new JavaModuleIdentifierFactory().getPackageName("pkgb", ""); JavaModuleInfo moduleInfo_cpb = view.getModuleInfo(cpb.getModuleSignature()).get(); assertTrue(moduleInfo_cpb.isUnnamedModule()); - ModulePackageName cpmain = - JavaModuleIdentifierFactory.getInstance().getPackageName("pkgcpmain", ""); + ModulePackageName cpmain = new JavaModuleIdentifierFactory().getPackageName("pkgcpmain", ""); JavaModuleInfo moduleInfo_cpmain = view.getModuleInfo(cpmain.getModuleSignature()).get(); assertTrue(moduleInfo_cpmain.isUnnamedModule()); ModuleJavaClassType main = - JavaModuleIdentifierFactory.getInstance().getClassType("Main", "pkgcpmain", ""); + new JavaModuleIdentifierFactory().getClassType("Main", "pkgcpmain", ""); assertTrue(view.getClass(main).isPresent()); assertTrue(view.getClass(cpmain, main).isPresent()); assertTrue(view.getClass(cpb, main).isPresent()); assertFalse(view.getClass(pkgbModb, main).isPresent()); ModuleJavaClassType BOnClasspath = - JavaModuleIdentifierFactory.getInstance().getClassType("BFromClasspath", "pkgboncp", ""); + new JavaModuleIdentifierFactory().getClassType("BFromClasspath", "pkgboncp", ""); assertTrue(view.getClass(BOnClasspath).isPresent()); assertTrue(view.getClass(cpb, BOnClasspath).isPresent()); assertTrue(view.getClass(cpmain, BOnClasspath).isPresent()); assertFalse(view.getClass(pkgbModb, BOnClasspath).isPresent()); ModuleJavaClassType BFromClasspath = - JavaModuleIdentifierFactory.getInstance().getClassType("BFromClasspath", "pkgb", ""); + new JavaModuleIdentifierFactory().getClassType("BFromClasspath", "pkgb", ""); assertTrue(view.getClass(BFromClasspath).isPresent()); assertTrue(view.getClass(cpb, BFromClasspath).isPresent()); assertTrue(view.getClass(cpmain, BFromClasspath).isPresent()); assertFalse(view.getClass(pkgbModb, BFromClasspath).isPresent()); ModuleJavaClassType BFromModule = - JavaModuleIdentifierFactory.getInstance().getClassType("BFromModule", "pkgb", "modb"); + new JavaModuleIdentifierFactory().getClassType("BFromModule", "pkgb", "modb"); assertTrue(view.getClass(BFromModule).isPresent()); assertTrue(view.getClass(pkgbModb, BFromModule).isPresent()); assertTrue(view.getClass(cpb, BFromModule).isPresent()); assertTrue(view.getClass(cpmain, BFromModule).isPresent()); ModuleJavaClassType BModuleB = - JavaModuleIdentifierFactory.getInstance().getClassType("B", "pkgb", "modb"); + new JavaModuleIdentifierFactory().getClassType("B", "pkgb", "modb"); assertTrue(view.getClass(BModuleB).isPresent()); assertTrue(view.getClass(pkgbModb, BModuleB).isPresent()); assertTrue(view.getClass(cpb, BModuleB).isPresent()); assertTrue(view.getClass(cpmain, BModuleB).isPresent()); ModuleJavaClassType BFromModuleButInternal = - JavaModuleIdentifierFactory.getInstance() + new JavaModuleIdentifierFactory() .getClassType("BFromModuleButInternal", "pkgbinternal", "modb"); assertTrue(view.getClass(BFromModuleButInternal).isPresent()); assertTrue(view.getClass(pkgbModb, BFromModuleButInternal).isPresent()); @@ -811,9 +790,9 @@ public void testEqualModulePath() { JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); ModulePackageName modMain = - JavaModuleIdentifierFactory.getInstance().getPackageName("pkgmain", "modmain"); + new JavaModuleIdentifierFactory().getPackageName("pkgmain", "modmain"); ModuleJavaClassType targetClassMain = - JavaModuleIdentifierFactory.getInstance().getClassType("Main", "pkgmain", "modmain"); + new JavaModuleIdentifierFactory().getClassType("Main", "pkgmain", "modmain"); assertTrue(view.getClass(modMain, targetClassMain).isPresent()); // should we detect that in general? it doenst lead to errors.. just unnecessary overhead while // resolving.. @@ -830,12 +809,12 @@ public void testMainMethod() { JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); ModuleJavaClassType targetClass = - JavaModuleIdentifierFactory.getInstance().getClassType("Main", "pkgmain", "modmain"); + new JavaModuleIdentifierFactory().getClassType("Main", "pkgmain", "modmain"); assertTrue(view.getClass(targetClass).isPresent()); long mainMethodCount = view.getClass(targetClass).get().getMethods().stream() - .filter(m -> m.isMain(JavaModuleIdentifierFactory.getInstance())) + .filter(m -> m.isMain(new JavaModuleIdentifierFactory())) .count(); assertEquals(1, mainMethodCount); } diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/inputlocation/AnalysisInputLocationTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/inputlocation/AnalysisInputLocationTest.java index b6f83443740..1c7627cee2b 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/inputlocation/AnalysisInputLocationTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/inputlocation/AnalysisInputLocationTest.java @@ -47,7 +47,7 @@ public abstract class AnalysisInputLocationTest { final Path cls = Paths.get("src/test/resources/miniTestSuite/java6/binary/Employee.class"); protected IdentifierFactory getIdentifierFactory() { - return JavaIdentifierFactory.getInstance(); + return new JavaIdentifierFactory(); } protected void testClassReceival( diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/inputlocation/JavaModulePathAnalysisInputLocationTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/inputlocation/JavaModulePathAnalysisInputLocationTest.java index da0fc4888e8..90b8c225c15 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/inputlocation/JavaModulePathAnalysisInputLocationTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/inputlocation/JavaModulePathAnalysisInputLocationTest.java @@ -35,7 +35,7 @@ public void testJarModule() { JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); JavaClassType targetClass = - JavaModuleIdentifierFactory.getInstance().getClassType("Main", "pkgmain", "modmain"); + new JavaModuleIdentifierFactory().getClassType("Main", "pkgmain", "modmain"); Optional aClass = view.getClass(targetClass); assertTrue(aClass.isPresent()); @@ -55,7 +55,7 @@ public void testExplodedModule() { JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); JavaClassType targetClass = - JavaModuleIdentifierFactory.getInstance().getClassType("Main", "pkgmain", "modmain"); + new JavaModuleIdentifierFactory().getClassType("Main", "pkgmain", "modmain"); Optional aClass = view.getClass(targetClass); assertTrue(aClass.isPresent()); @@ -75,11 +75,9 @@ public void testGetModuleInfo() { JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); ModulePackageName modMain = - JavaModuleIdentifierFactory.getInstance().getPackageName("pkgmain", "modmain"); - ModulePackageName modB = - JavaModuleIdentifierFactory.getInstance().getPackageName("pkgb", "modb"); - ModulePackageName modC = - JavaModuleIdentifierFactory.getInstance().getPackageName("pkgc", "modc"); + new JavaModuleIdentifierFactory().getPackageName("pkgmain", "modmain"); + ModulePackageName modB = new JavaModuleIdentifierFactory().getPackageName("pkgb", "modb"); + ModulePackageName modC = new JavaModuleIdentifierFactory().getPackageName("pkgc", "modc"); Optional moduleInfoMain = view.getModuleInfo(modMain.getModuleSignature()); assertTrue(moduleInfoMain.isPresent()); @@ -101,7 +99,7 @@ public void testGetClassSource() { JavaModuleView view = new JavaModuleView(inputLocations, moduleInfoAnalysisInputLocations); final ClassType sig = - JavaModuleIdentifierFactory.getInstance().getClassType("String", "java.lang", "java.base"); + new JavaModuleIdentifierFactory().getClassType("String", "java.lang", "java.base"); final Optional clazzOpt = inputLocation.getClassSource(sig, view); assertTrue(clazzOpt.isPresent()); diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/inputlocation/JrtFileSystemAnalysisInputLocationTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/inputlocation/JrtFileSystemAnalysisInputLocationTest.java index 5b2da3c294c..7ffb73dbfc8 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/inputlocation/JrtFileSystemAnalysisInputLocationTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/inputlocation/JrtFileSystemAnalysisInputLocationTest.java @@ -27,7 +27,7 @@ public void getClassSource() { new JavaModuleView(Collections.emptyList(), Collections.singletonList(inputLocation)); final ClassType sig = - JavaModuleIdentifierFactory.getInstance().getClassType("String", "java.lang", "java.base"); + new JavaModuleIdentifierFactory().getClassType("String", "java.lang", "java.base"); final Optional clazz = inputLocation.getClassSource(sig, view); assertTrue(clazz.isPresent()); @@ -42,9 +42,9 @@ public void getClassSources() { new JavaModuleView(Collections.emptyList(), Collections.singletonList(inputLocation)); final ClassType sig1 = - JavaModuleIdentifierFactory.getInstance().getClassType("String", "java.lang", "java.base"); + new JavaModuleIdentifierFactory().getClassType("String", "java.lang", "java.base"); final ClassType sig2 = - JavaModuleIdentifierFactory.getInstance().getClassType("System", "java.lang", "java.base"); + new JavaModuleIdentifierFactory().getClassType("System", "java.lang", "java.base"); final Collection classSources = inputLocation.getClassSources(view).collect(Collectors.toList()); @@ -63,7 +63,8 @@ public void getClassSources() { @Test public void discoverModules() { JrtFileSystemAnalysisInputLocation inputLocation = new JrtFileSystemAnalysisInputLocation(); - Collection modules = inputLocation.discoverModules(); + Collection modules = + inputLocation.discoverModules(new JavaModuleIdentifierFactory()); assertTrue(modules.size() > 65); System.out.println(modules); assertTrue(modules.contains(JavaModuleIdentifierFactory.getModuleSignature("java.base"))); diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/inputlocation/ModuleFinderTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/inputlocation/ModuleFinderTest.java index 8c0bd8d4633..db172daf8e8 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/inputlocation/ModuleFinderTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/inputlocation/ModuleFinderTest.java @@ -17,18 +17,21 @@ */ public class ModuleFinderTest extends AnalysisInputLocationTest { + private final JavaModuleIdentifierFactory identifierFactory = new JavaModuleIdentifierFactory(); + @Test public void discoverJarModuleByName() { ModuleFinder moduleFinder = new ModuleFinder(jar); AnalysisInputLocation inputLocation = - moduleFinder.getModule(JavaModuleIdentifierFactory.getModuleSignature("MiniApp")); + moduleFinder.getModule( + JavaModuleIdentifierFactory.getModuleSignature("MiniApp"), identifierFactory); assertTrue(inputLocation instanceof PathBasedAnalysisInputLocation); } @Test public void discoverJarModuleInAllModules() { ModuleFinder moduleFinder = new ModuleFinder(jar); - Collection modules = moduleFinder.getAllModules(); + Collection modules = moduleFinder.getAllModules(identifierFactory); assertTrue(modules.contains(JavaModuleIdentifierFactory.getModuleSignature("MiniApp"))); } @@ -36,14 +39,15 @@ public void discoverJarModuleInAllModules() { public void discoverWarModuleByName() { ModuleFinder moduleFinder = new ModuleFinder(war); AnalysisInputLocation inputLocation = - moduleFinder.getModule(JavaModuleIdentifierFactory.getModuleSignature("dummyWarApp")); + moduleFinder.getModule( + JavaModuleIdentifierFactory.getModuleSignature("dummyWarApp"), identifierFactory); assertTrue(inputLocation instanceof PathBasedAnalysisInputLocation); } @Test public void discoverWarModuleInAllModules() { ModuleFinder moduleFinder = new ModuleFinder(war); - Collection modules = moduleFinder.getAllModules(); + Collection modules = moduleFinder.getAllModules(identifierFactory); assertTrue(modules.contains(JavaModuleIdentifierFactory.getModuleSignature("dummyWarApp"))); } @@ -52,7 +56,7 @@ public void testModuleJar() { ModuleFinder moduleFinder = new ModuleFinder( Paths.get("src/test/resources/java9-target/de/upb/soot/namespaces/modules/")); - Collection discoveredModules = moduleFinder.getAllModules(); + Collection discoveredModules = moduleFinder.getAllModules(identifierFactory); assertTrue( discoveredModules.contains(JavaModuleIdentifierFactory.getModuleSignature("de.upb.mod"))); } @@ -62,7 +66,7 @@ public void testModuleExploded() { ModuleFinder moduleFinder = new ModuleFinder( Paths.get("src/test/resources/java9-target/de/upb/soot/namespaces/modules/")); - Collection discoveredModules = moduleFinder.getAllModules(); + Collection discoveredModules = moduleFinder.getAllModules(identifierFactory); assertTrue( discoveredModules.contains(JavaModuleIdentifierFactory.getModuleSignature("fancyMod"))); } @@ -87,10 +91,12 @@ public void testAutomaticModuleNamingViaManifest() { assertNotNull( moduleFinder.getModule( JavaModuleIdentifierFactory.getModuleSignature( - "automaticmoduleWithNamingViaManifestModuleName"))); + "automaticmoduleWithNamingViaManifestModuleName"), + identifierFactory)); assertNull( moduleFinder.getModule( - JavaModuleIdentifierFactory.getModuleSignature("AutomaticmoduleWithManifest"))); + JavaModuleIdentifierFactory.getModuleSignature("AutomaticmoduleWithManifest"), + identifierFactory)); } } diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/inputlocation/ModuleMultiReleaseJarAnalysisInputLocationTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/inputlocation/ModuleMultiReleaseJarAnalysisInputLocationTest.java index 1469ba21884..3a74883980c 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/inputlocation/ModuleMultiReleaseJarAnalysisInputLocationTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/inputlocation/ModuleMultiReleaseJarAnalysisInputLocationTest.java @@ -59,7 +59,7 @@ public void modularMultiReleaseJar() { getIdentifierFactory().getClassType("de.upb.swt.multirelease.Utility"); final ModuleJavaClassType utilityModule = - JavaModuleIdentifierFactory.getInstance() + new JavaModuleIdentifierFactory() .getClassType("de.upb.swt.multirelease/de.upb.swt.multirelease.Utility"); final ClassType classType2 = diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/AggregatorTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/AggregatorTest.java index c07acc10e50..4664b0aae29 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/AggregatorTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/AggregatorTest.java @@ -84,7 +84,7 @@ public void noAggregationWithUse() { StmtPositionInfo noPositionInfo = StmtPositionInfo.getNoStmtPositionInfo(); - JavaIdentifierFactory identifierFactory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory identifierFactory = new JavaIdentifierFactory(); JavaClassType fileType = identifierFactory.getClassType("File"); Local a = JavaJimple.newLocal("a", fileType); @@ -137,7 +137,7 @@ private static Body.BodyBuilder createBodyBuilder(boolean withAggregation) { Body.BodyBuilder builder = Body.builder(); builder.setMethodSignature( - JavaIdentifierFactory.getInstance() + new JavaIdentifierFactory() .getMethodSignature("ab.c", "test", "void", Collections.emptyList())); final MutableControlFlowGraph controlFlowGraph = builder.getControlFlowGraph(); controlFlowGraph.setStartingStmt(intToA); diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/CastAndReturnInlinerTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/CastAndReturnInlinerTest.java index b7450275d6f..109169c9a2e 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/CastAndReturnInlinerTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/CastAndReturnInlinerTest.java @@ -47,7 +47,7 @@ public class CastAndReturnInlinerTest { */ @Test public void testModification() { - JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory factory = new JavaIdentifierFactory(); StmtPositionInfo noPositionInfo = StmtPositionInfo.getNoStmtPositionInfo(); JavaClassType objectType = factory.getClassType("java.lang.Object"); @@ -56,7 +56,7 @@ public void testModification() { Local b = JavaJimple.newLocal("b", stringType); FallsThroughStmt strToA = - JavaJimple.newAssignStmt(a, JavaJimple.newStringConstant("str"), noPositionInfo); + JavaJimple.newAssignStmt(a, JavaJimple.newStringConstant("str", factory), noPositionInfo); FallsThroughStmt bToA = JavaJimple.newAssignStmt(b, JavaJimple.newCastExpr(a, stringType), noPositionInfo); Stmt ret = JavaJimple.newReturnStmt(b, noPositionInfo); @@ -74,7 +74,7 @@ public void testModification() { controlFlowGraph.putEdge(bToA, ret); bodyBuilder.setMethodSignature( - JavaIdentifierFactory.getInstance() + new JavaIdentifierFactory() .getMethodSignature("ab.c", "test", "void", Collections.emptyList())); new CastAndReturnInliner().interceptBody(bodyBuilder, new JavaView(Collections.emptyList())); @@ -106,7 +106,7 @@ public void testModification() { */ @Test public void testNoModification() { - JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory factory = new JavaIdentifierFactory(); StmtPositionInfo noPositionInfo = StmtPositionInfo.getNoStmtPositionInfo(); JavaClassType objectType = factory.getClassType("java.lang.Object"); @@ -116,9 +116,9 @@ public void testNoModification() { Local c = JavaJimple.newLocal("c", stringType); FallsThroughStmt strToA = - JavaJimple.newAssignStmt(a, JavaJimple.newStringConstant("str"), noPositionInfo); + JavaJimple.newAssignStmt(a, JavaJimple.newStringConstant("str", factory), noPositionInfo); FallsThroughStmt strToC = - JavaJimple.newAssignStmt(c, JavaJimple.newStringConstant("str2"), noPositionInfo); + JavaJimple.newAssignStmt(c, JavaJimple.newStringConstant("str2", factory), noPositionInfo); FallsThroughStmt bToA = JavaJimple.newAssignStmt(b, JavaJimple.newCastExpr(a, stringType), noPositionInfo); // Note this returns c, not b, hence the cast and return must not be inlined @@ -136,7 +136,7 @@ public void testNoModification() { controlFlowGraph.putEdge(jump, JGotoStmt.BRANCH_IDX, bToA); controlFlowGraph.putEdge(bToA, ret); bodyBuilder.setMethodSignature( - JavaIdentifierFactory.getInstance() + new JavaIdentifierFactory() .getMethodSignature("ab.c", "test", "void", Collections.emptyList())); Body testBody = bodyBuilder.build(); diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/ConditionalBranchFolderTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/ConditionalBranchFolderTest.java index 7abf18c5e11..35860ecc0a8 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/ConditionalBranchFolderTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/ConditionalBranchFolderTest.java @@ -18,7 +18,6 @@ import sootup.core.jimple.common.stmt.Stmt; import sootup.core.model.Body; import sootup.core.signatures.MethodSignature; -import sootup.core.signatures.PackageName; import sootup.core.util.ImmutableUtils; import sootup.core.util.Utils; import sootup.interceptors.ConditionalBranchFolder; @@ -85,14 +84,14 @@ public void testConditionalBranchingWithNoConclusiveIfCondition() { * @return the generated {@link Body} */ private static Body.BodyBuilder createBodyBuilder(int constantCondition) { - JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory factory = new JavaIdentifierFactory(); StmtPositionInfo noPositionInfo = StmtPositionInfo.getNoStmtPositionInfo(); JavaClassType stringType = factory.getClassType("java.lang.String"); Local a = JavaJimple.newLocal("a", stringType); Local b = JavaJimple.newLocal("b", stringType); - StringConstant stringConstant = JavaJimple.newStringConstant("str"); + StringConstant stringConstant = JavaJimple.newStringConstant("str", factory); FallsThroughStmt strToA = JavaJimple.newAssignStmt(a, stringConstant, noPositionInfo); FallsThroughStmt strToB; @@ -100,26 +99,26 @@ private static Body.BodyBuilder createBodyBuilder(int constantCondition) { JEqExpr jEqExpr; switch (constantCondition) { case 0: - anotherStringConstant = JavaJimple.newStringConstant("str"); + anotherStringConstant = JavaJimple.newStringConstant("str", factory); strToB = JavaJimple.newAssignStmt(b, anotherStringConstant, noPositionInfo); jEqExpr = new JEqExpr(stringConstant, anotherStringConstant); break; case 1: - anotherStringConstant = JavaJimple.newStringConstant("different string"); + anotherStringConstant = JavaJimple.newStringConstant("different string", factory); strToB = JavaJimple.newAssignStmt(b, anotherStringConstant, noPositionInfo); jEqExpr = new JEqExpr(stringConstant, anotherStringConstant); break; case 2: final MethodSignature methodSignature = - JavaIdentifierFactory.getInstance() + new JavaIdentifierFactory() .getMethodSignature( "java.lang.Object", "toString", "String", Collections.emptyList()); Local base = new Local( "someObjectThatHasSomethingToString", - new JavaClassType("StringBuilder", new PackageName("java.lang"))); + factory.getClassType("StringBuilder", "java.lang")); strToB = JavaJimple.newAssignStmt( b, Jimple.newVirtualInvokeExpr(base, methodSignature), noPositionInfo); @@ -144,7 +143,7 @@ private static Body.BodyBuilder createBodyBuilder(int constantCondition) { controlFlowGraph.putEdge(ifStmt, JIfStmt.FALSE_BRANCH_IDX, retb); controlFlowGraph.setStartingStmt(strToA); bodyBuilder.setMethodSignature( - JavaIdentifierFactory.getInstance() + new JavaIdentifierFactory() .getMethodSignature("ab.c", "test", "void", Collections.emptyList())); return bodyBuilder; } diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/ConstantPropagatorAndFolderTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/ConstantPropagatorAndFolderTest.java index b02d2a0c9bd..701bb6edbdf 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/ConstantPropagatorAndFolderTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/ConstantPropagatorAndFolderTest.java @@ -106,7 +106,7 @@ private static Body.BodyBuilder createBody(boolean constantFolding) { Body.BodyBuilder builder = Body.builder(); builder.setMethodSignature( - JavaIdentifierFactory.getInstance() + new JavaIdentifierFactory() .getMethodSignature("ab.c", "test", "void", Collections.emptyList())); final MutableControlFlowGraph controlFlowGraph = builder.getControlFlowGraph(); diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/CopyPropagatorTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/CopyPropagatorTest.java index 09c7b5eea36..4a46024b451 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/CopyPropagatorTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/CopyPropagatorTest.java @@ -44,13 +44,14 @@ public class CopyPropagatorTest { // Preparation - JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory factory = new JavaIdentifierFactory(); StmtPositionInfo noStmtPositionInfo = StmtPositionInfo.getNoStmtPositionInfo(); JavaClassType intType = factory.getClassType("int"); JavaClassType refType = factory.getClassType("ref"); JavaClassType classType = factory.getClassType("Test"); MethodSignature methodSignature = - new MethodSignature(classType, "test", Collections.emptyList(), VoidType.getInstance()); + new JavaIdentifierFactory() + .getMethodSignature(classType, "test", VoidType.getInstance(), Collections.emptyList()); IdentityRef identityRef = JavaJimple.newThisRef(classType); // build locals diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/DeadAssignmentEliminatorTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/DeadAssignmentEliminatorTest.java index 3149341922a..328f4a73e50 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/DeadAssignmentEliminatorTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/DeadAssignmentEliminatorTest.java @@ -34,6 +34,8 @@ public class DeadAssignmentEliminatorTest { + private static final JavaIdentifierFactory identifierFactory = new JavaIdentifierFactory(); + Path classFilePath = Paths.get("src/test/resources/bugfixes/DeadAssignmentEliminator.class"); /** @@ -78,8 +80,7 @@ public void conditionalToRemovedBlock() { Body.BodyBuilder builder = Body.builder(); builder.setMethodSignature( - JavaIdentifierFactory.getInstance() - .getMethodSignature("test", "ab.c", "void", Collections.emptyList())); + identifierFactory.getMethodSignature("test", "ab.c", "void", Collections.emptyList())); builder.setLocals(locals); final MutableControlFlowGraph controlFlowGraph = builder.getControlFlowGraph(); @@ -130,14 +131,15 @@ public void testNoModification() { private static Body.BodyBuilder createBody(boolean essentialOption) { StmtPositionInfo noPositionInfo = StmtPositionInfo.getNoStmtPositionInfo(); - JavaClassType objectType = JavaIdentifierFactory.getInstance().getClassType("java.lang.Object"); + JavaClassType objectType = identifierFactory.getClassType("java.lang.Object"); Local a = JavaJimple.newLocal("a", objectType); Local b = JavaJimple.newLocal("b", objectType); Local c = JavaJimple.newLocal("c", PrimitiveType.getInt()); FallsThroughStmt strToA = - JavaJimple.newAssignStmt(a, JavaJimple.newStringConstant("str"), noPositionInfo); + JavaJimple.newAssignStmt( + a, JavaJimple.newStringConstant("str", identifierFactory), noPositionInfo); Stmt ret = JavaJimple.newReturnStmt(a, noPositionInfo); Set locals = new LinkedHashSet<>(Arrays.asList(a, b, c)); @@ -146,8 +148,7 @@ private static Body.BodyBuilder createBody(boolean essentialOption) { final MutableControlFlowGraph controlFlowGraph = builder.getControlFlowGraph(); controlFlowGraph.setStartingStmt(strToA); builder.setMethodSignature( - JavaIdentifierFactory.getInstance() - .getMethodSignature("ab.c", "test", "void", Collections.emptyList())); + identifierFactory.getMethodSignature("ab.c", "test", "void", Collections.emptyList())); if (essentialOption) { FallsThroughStmt newToB = diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/EmptySwitchEliminatorTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/EmptySwitchEliminatorTest.java index c89485389e4..d2e9c137526 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/EmptySwitchEliminatorTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/EmptySwitchEliminatorTest.java @@ -28,12 +28,13 @@ public class EmptySwitchEliminatorTest { // Preparation - JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory factory = new JavaIdentifierFactory(); StmtPositionInfo noStmtPositionInfo = StmtPositionInfo.getNoStmtPositionInfo(); JavaClassType intType = factory.getClassType("int"); JavaClassType classType = factory.getClassType("Test"); MethodSignature methodSignature = - new MethodSignature(classType, "test", Collections.emptyList(), VoidType.getInstance()); + new JavaIdentifierFactory() + .getMethodSignature(classType, "test", VoidType.getInstance(), Collections.emptyList()); IdentityRef identityRef = JavaJimple.newThisRef(classType); // build locals diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/LocalLivenessAnalyserTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/LocalLivenessAnalyserTest.java index f7d33971f24..10dbd2435c3 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/LocalLivenessAnalyserTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/LocalLivenessAnalyserTest.java @@ -28,13 +28,14 @@ public class LocalLivenessAnalyserTest { // Preparation - JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory factory = new JavaIdentifierFactory(); StmtPositionInfo noStmtPositionInfo = StmtPositionInfo.getNoStmtPositionInfo(); JavaClassType intType = factory.getClassType("int"); JavaClassType classType = factory.getClassType("Test"); MethodSignature methodSignature = - new MethodSignature(classType, "test", Collections.emptyList(), VoidType.getInstance()); + new JavaIdentifierFactory() + .getMethodSignature(classType, "test", VoidType.getInstance(), Collections.emptyList()); // build locals Local a = JavaJimple.newLocal("a", intType); diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/LocalNameStandardizerTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/LocalNameStandardizerTest.java index 590f2c072bb..b0e4896c372 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/LocalNameStandardizerTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/LocalNameStandardizerTest.java @@ -30,11 +30,12 @@ */ public class LocalNameStandardizerTest { - JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory factory = new JavaIdentifierFactory(); StmtPositionInfo noStmtPositionInfo = StmtPositionInfo.getNoStmtPositionInfo(); JavaClassType classType = factory.getClassType("Test"); MethodSignature methodSignature = - new MethodSignature(classType, "test", Collections.emptyList(), VoidType.getInstance()); + new JavaIdentifierFactory() + .getMethodSignature(classType, "test", VoidType.getInstance(), Collections.emptyList()); JavaClassType refType = factory.getClassType("ref"); JavaClassType otherRefType = factory.getClassType("otherRef"); diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/LocalPackerTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/LocalPackerTest.java index f6cfa2d80f1..dae989a904d 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/LocalPackerTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/LocalPackerTest.java @@ -31,7 +31,7 @@ */ public class LocalPackerTest { // Preparation - JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory factory = new JavaIdentifierFactory(); StmtPositionInfo noStmtPositionInfo = StmtPositionInfo.getNoStmtPositionInfo(); JavaClassType classType = factory.getClassType("Test"); @@ -42,7 +42,7 @@ public class LocalPackerTest { IdentityRef identityRef0 = JavaJimple.newParameterRef(intType, 0); IdentityRef identityRef1 = JavaJimple.newParameterRef(intType, 1); - IdentityRef caughtExceptionRef = JavaJimple.newCaughtExceptionRef(); + IdentityRef caughtExceptionRef = JavaJimple.newCaughtExceptionRef(factory); // build locals Local l0 = JavaJimple.newLocal("l0", classType); @@ -240,7 +240,8 @@ private Body.BodyBuilder createBodyBuilder() { parameters.add(intType); // parameters.add(doubleType); MethodSignature methodSignature = - new MethodSignature(classType, "test", parameters, VoidType.getInstance()); + new JavaIdentifierFactory() + .getMethodSignature(classType, "test", VoidType.getInstance(), parameters); builder.setMethodSignature(methodSignature); // build set locals @@ -275,7 +276,8 @@ private Body createExpectedBody() { parameters.add(intType); // parameters.add(doubleType); MethodSignature methodSignature = - new MethodSignature(classType, "test", parameters, VoidType.getInstance()); + new JavaIdentifierFactory() + .getMethodSignature(classType, "test", VoidType.getInstance(), parameters); builder.setMethodSignature(methodSignature); // build set locals @@ -313,7 +315,8 @@ private Body.BodyBuilder createTrapBody() { parameters.add(intType); // parameters.add(doubleType); MethodSignature methodSignature = - new MethodSignature(classType, "test", parameters, VoidType.getInstance()); + new JavaIdentifierFactory() + .getMethodSignature(classType, "test", VoidType.getInstance(), parameters); builder.setMethodSignature(methodSignature); // build set locals @@ -352,7 +355,8 @@ private Body.BodyBuilder createExpectedTrapBody() { parameters.add(intType); MethodSignature methodSignature = - new MethodSignature(classType, "test", parameters, VoidType.getInstance()); + new JavaIdentifierFactory() + .getMethodSignature(classType, "test", VoidType.getInstance(), parameters); builder.setMethodSignature(methodSignature); // build set locals diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/LocalSplitterTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/LocalSplitterTest.java index f1770ef9b72..63d2b35ae98 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/LocalSplitterTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/LocalSplitterTest.java @@ -11,12 +11,10 @@ import sootup.core.model.Body; import sootup.core.model.SootMethod; import sootup.core.model.SourceType; -import sootup.core.signatures.PackageName; import sootup.core.types.ClassType; import sootup.interceptors.LocalSplitter; import sootup.java.bytecode.frontend.inputlocation.JavaClassPathAnalysisInputLocation; import sootup.java.bytecode.frontend.inputlocation.JrtFileSystemAnalysisInputLocation; -import sootup.java.core.types.JavaClassType; import sootup.java.core.views.JavaView; public class LocalSplitterTest { @@ -33,7 +31,7 @@ public void setup() { } private Body getBody(String methodName) { - ClassType type = new JavaClassType("LocalSplitterTarget", PackageName.DEFAULT_PACKAGE); + ClassType type = view.getIdentifierFactory().getClassType("LocalSplitterTarget"); SootMethod sootMethod = view.getClass(type).get().getMethods().stream() .filter(method -> method.getName().equals(methodName)) diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/NopEliminatorTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/NopEliminatorTest.java index 11958477f6e..4dba2625608 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/NopEliminatorTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/NopEliminatorTest.java @@ -73,7 +73,7 @@ public void testNoJNops() { * @return the generated {@link Body} */ private static Body.BodyBuilder createBody(boolean withNop) { - JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory factory = new JavaIdentifierFactory(); StmtPositionInfo noPositionInfo = StmtPositionInfo.getNoStmtPositionInfo(); JavaClassType objectType = factory.getClassType("java.lang.Object"); @@ -83,7 +83,7 @@ private static Body.BodyBuilder createBody(boolean withNop) { Local b = JavaJimple.newLocal("b", stringType); FallsThroughStmt strToA = - JavaJimple.newAssignStmt(a, JavaJimple.newStringConstant("str"), noPositionInfo); + JavaJimple.newAssignStmt(a, JavaJimple.newStringConstant("str", factory), noPositionInfo); FallsThroughStmt bToA = JavaJimple.newAssignStmt(b, JavaJimple.newCastExpr(a, stringType), noPositionInfo); Stmt ret = JavaJimple.newReturnStmt(b, noPositionInfo); @@ -93,7 +93,7 @@ private static Body.BodyBuilder createBody(boolean withNop) { Body.BodyBuilder builder = Body.builder(); builder.setMethodSignature( - JavaIdentifierFactory.getInstance() + new JavaIdentifierFactory() .getMethodSignature("ab.c", "test", "void", Collections.emptyList())); final MutableControlFlowGraph controlFlowGraph = builder.getControlFlowGraph(); diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/StaticSingleAssignmentFormerTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/StaticSingleAssignmentFormerTest.java index 03776c77a76..943b6a63284 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/StaticSingleAssignmentFormerTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/StaticSingleAssignmentFormerTest.java @@ -33,7 +33,7 @@ public class StaticSingleAssignmentFormerTest { // Preparation - JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory factory = new JavaIdentifierFactory(); StmtPositionInfo noStmtPositionInfo = StmtPositionInfo.getNoStmtPositionInfo(); final String location = "src/test/resources/bugfixes/"; @@ -41,10 +41,11 @@ public class StaticSingleAssignmentFormerTest { JavaClassType classType = factory.getClassType("Test"); JavaClassType refType = factory.getClassType("ref"); MethodSignature methodSignature = - new MethodSignature(classType, "test", Collections.emptyList(), VoidType.getInstance()); + new JavaIdentifierFactory() + .getMethodSignature(classType, "test", VoidType.getInstance(), Collections.emptyList()); IdentityRef identityRef = JavaJimple.newThisRef(classType); ClassType exceptionType = factory.getClassType("Exception"); - IdentityRef caughtExceptionRef = JavaJimple.newCaughtExceptionRef(); + IdentityRef caughtExceptionRef = JavaJimple.newCaughtExceptionRef(factory); // build locals Local l0 = JavaJimple.newLocal("l0", classType); diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/TrapTightenerTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/TrapTightenerTest.java index 36e83f3fd37..511afd79b73 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/TrapTightenerTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/TrapTightenerTest.java @@ -35,13 +35,14 @@ public class TrapTightenerTest { public final BriefStmtPrinter briefStmtPrinter = new BriefStmtPrinter(); - JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory factory = new JavaIdentifierFactory(); StmtPositionInfo noStmtPositionInfo = StmtPositionInfo.getNoStmtPositionInfo(); JavaClassType intType = factory.getClassType("int"); JavaClassType classType = factory.getClassType("Test"); MethodSignature methodSignature = - new MethodSignature(classType, "test", Collections.emptyList(), VoidType.getInstance()); + new JavaIdentifierFactory() + .getMethodSignature(classType, "test", VoidType.getInstance(), Collections.emptyList()); IdentityRef identityRef = JavaJimple.newThisRef(classType); // build locals @@ -51,7 +52,7 @@ public class TrapTightenerTest { Local l3 = JavaJimple.newLocal("l3", intType); ClassType exception = factory.getClassType("java.lang.Throwable"); - IdentityRef caughtExceptionRef = JavaJimple.newCaughtExceptionRef(); + IdentityRef caughtExceptionRef = JavaJimple.newCaughtExceptionRef(factory); FallsThroughStmt startingStmt = JavaJimple.newIdentityStmt(l0, identityRef, noStmtPositionInfo); Stmt ret = JavaJimple.newReturnVoidStmt(noStmtPositionInfo); diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnreachableCodeEliminatorTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnreachableCodeEliminatorTest.java index 491c00d8d47..59e10e11291 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnreachableCodeEliminatorTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnreachableCodeEliminatorTest.java @@ -33,11 +33,12 @@ public class UnreachableCodeEliminatorTest { public final BriefStmtPrinter briefStmtPrinter = new BriefStmtPrinter(); - JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory factory = new JavaIdentifierFactory(); StmtPositionInfo noStmtPositionInfo = StmtPositionInfo.getNoStmtPositionInfo(); JavaClassType classType = factory.getClassType("Test"); MethodSignature methodSignature = - new MethodSignature(classType, "test", Collections.emptyList(), VoidType.getInstance()); + new JavaIdentifierFactory() + .getMethodSignature(classType, "test", VoidType.getInstance(), Collections.emptyList()); JavaClassType refType = factory.getClassType("ref"); ClassType exception = factory.getClassType("RuntimeException"); @@ -50,7 +51,7 @@ public class UnreachableCodeEliminatorTest { Local l3 = JavaJimple.newLocal("l3", PrimitiveType.getInt()); Local l4 = JavaJimple.newLocal("l3", PrimitiveType.getInt()); Local stack0 = JavaJimple.newLocal("stack0", refType); - IdentityRef idRef = JavaJimple.newCaughtExceptionRef(); + IdentityRef idRef = JavaJimple.newCaughtExceptionRef(factory); // build stmts FallsThroughStmt startingStmt = JavaJimple.newIdentityStmt(l0, identityRef, noStmtPositionInfo); diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnusedLocalEliminatorTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnusedLocalEliminatorTest.java index 1a038d8fb55..8bbcc6ee05b 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnusedLocalEliminatorTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnusedLocalEliminatorTest.java @@ -37,8 +37,8 @@ public void testRemoveUnusedDefsAndUses() { Set originalLocals = originalBody.getLocals(); Set processedLocals = processedBody.getLocals(); - JavaClassType objectType = JavaIdentifierFactory.getInstance().getClassType("java.lang.Object"); - JavaClassType stringType = JavaIdentifierFactory.getInstance().getClassType("java.lang.String"); + JavaClassType objectType = new JavaIdentifierFactory().getClassType("java.lang.Object"); + JavaClassType stringType = new JavaIdentifierFactory().getClassType("java.lang.String"); assertEquals(4, originalLocals.size()); assertEquals(2, processedLocals.size()); @@ -58,7 +58,7 @@ public void testRemoveNothing() { } private static Body.BodyBuilder createBody(boolean unusedLocals) { - JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory factory = new JavaIdentifierFactory(); StmtPositionInfo noPositionInfo = StmtPositionInfo.getNoStmtPositionInfo(); JavaClassType objectType = factory.getClassType("java.lang.Object"); @@ -75,7 +75,7 @@ private static Body.BodyBuilder createBody(boolean unusedLocals) { } FallsThroughStmt strToA = - JavaJimple.newAssignStmt(a, JavaJimple.newStringConstant("str"), noPositionInfo); + JavaJimple.newAssignStmt(a, JavaJimple.newStringConstant("str", factory), noPositionInfo); FallsThroughStmt bToA = JavaJimple.newAssignStmt(b, JavaJimple.newCastExpr(a, stringType), noPositionInfo); Stmt ret = JavaJimple.newReturnStmt(b, noPositionInfo); @@ -90,7 +90,7 @@ private static Body.BodyBuilder createBody(boolean unusedLocals) { controlFlowGraph.putEdge(bToA, ret); builder.setMethodSignature( - JavaIdentifierFactory.getInstance() + new JavaIdentifierFactory() .getMethodSignature("a.b.c", "test", "void", Collections.emptyList())); return builder; } diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/typeresolving/TypeResolverTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/typeresolving/TypeResolverTest.java index 85f8d2e66ce..5fa8e8d2414 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/typeresolving/TypeResolverTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/typeresolving/TypeResolverTest.java @@ -23,24 +23,24 @@ import sootup.interceptors.typeresolving.TypeResolver; import sootup.interceptors.typeresolving.types.TopType; import sootup.java.bytecode.frontend.inputlocation.JavaClassPathAnalysisInputLocation; -import sootup.java.core.JavaPackageName; +import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.JavaSootMethod; -import sootup.java.core.types.JavaClassType; import sootup.java.core.views.JavaView; public class TypeResolverTest extends TypeAssignerTestSuite { + JavaIdentifierFactory identifierFactory = new JavaIdentifierFactory(); String baseDir = "src/test/resources/TypeResolverTestSuite/"; - Type objectType = new JavaClassType("Object", new JavaPackageName("java.lang")); - Type stringType = new JavaClassType("String", new JavaPackageName("java.lang")); - Type charSequenceType = new JavaClassType("CharSequence", new JavaPackageName("java.lang")); - Type numberType = new JavaClassType("Number", new JavaPackageName("java.lang")); - Type dateType = new JavaClassType("Date", new JavaPackageName("java.util")); - Type miscType = new JavaClassType("Misc", new JavaPackageName("")); - Type sysoutType = new JavaClassType("PrintStream", new JavaPackageName("java.io")); - Type throwableType = new JavaClassType("Throwable", new JavaPackageName("java.lang")); + Type objectType = identifierFactory.getClassType("Object", "java.lang"); + Type stringType = identifierFactory.getClassType("String", "java.lang"); + Type charSequenceType = identifierFactory.getClassType("CharSequence", "java.lang"); + Type numberType = identifierFactory.getClassType("Number", "java.lang"); + Type dateType = identifierFactory.getClassType("Date", "java.util"); + Type miscType = identifierFactory.getClassType("Misc", ""); + Type sysoutType = identifierFactory.getClassType("PrintStream", "java.io"); + Type throwableType = identifierFactory.getClassType("Throwable", "java.lang"); Type illegalArgumentType = - new JavaClassType("IllegalArgumentException", new JavaPackageName("java.lang")); + identifierFactory.getClassType("IllegalArgumentException", "java.lang"); @BeforeEach public void setup() { @@ -195,8 +195,7 @@ public void testArrayAssignBeforeInit() { body, new Local( "l0", - ArrayType.createArrayType( - new JavaClassType("String", new JavaPackageName("java.lang")), 1)), + ArrayType.createArrayType(identifierFactory.getClassType("String", "java.lang"), 1)), new Local("$stack1", PrimitiveType.getDouble()), new Local("$stack2", PrimitiveType.getByte())); } diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/minimaltestsuite/MinimalBytecodeTestSuiteBase.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/minimaltestsuite/MinimalBytecodeTestSuiteBase.java index 917f18783e4..d5a42ea52f2 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/minimaltestsuite/MinimalBytecodeTestSuiteBase.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/minimaltestsuite/MinimalBytecodeTestSuiteBase.java @@ -31,7 +31,7 @@ public abstract class MinimalBytecodeTestSuiteBase { static final String baseDir = "src/test/resources/miniTestSuite"; - protected JavaIdentifierFactory identifierFactory = JavaIdentifierFactory.getInstance(); + protected JavaIdentifierFactory identifierFactory = new JavaIdentifierFactory(); private static String testDir = ""; private static JavaView javaView; diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/minimaltestsuite/java14/RecordTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/minimaltestsuite/java14/RecordTest.java index 75a8836f93c..da522f5a052 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/minimaltestsuite/java14/RecordTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/minimaltestsuite/java14/RecordTest.java @@ -13,9 +13,7 @@ import sootup.core.jimple.common.expr.JDynamicInvokeExpr; import sootup.core.jimple.common.stmt.InvokableStmt; import sootup.core.model.SootMethod; -import sootup.core.signatures.FieldSignature; import sootup.core.signatures.MethodSignature; -import sootup.core.signatures.PackageName; import sootup.core.types.PrimitiveType; import sootup.java.bytecode.frontend.minimaltestsuite.MinimalBytecodeTestSuiteBase; import sootup.java.core.language.JavaJimple; @@ -70,21 +68,23 @@ public void test() { // test bootstrap args List bootTrapArgs = invoke.getBootstrapArgs(); - assertTrue(bootTrapArgs.contains(JavaJimple.newClassConstant("LRecord;"))); - assertTrue(bootTrapArgs.contains(JavaJimple.newStringConstant("a;b"))); + assertTrue(bootTrapArgs.contains(JavaJimple.newClassConstant("LRecord;", identifierFactory))); + assertTrue(bootTrapArgs.contains(JavaJimple.newStringConstant("a;b", identifierFactory))); assertTrue( bootTrapArgs.contains( JavaJimple.newMethodHandle( - new FieldSignature( - new JavaClassType("Record", new PackageName("")), "a", PrimitiveType.getInt()), - 1))); + identifierFactory.getFieldSignature( + "a", identifierFactory.getClassType("Record"), PrimitiveType.getInt()), + 1, + identifierFactory))); assertTrue( bootTrapArgs.contains( JavaJimple.newMethodHandle( - new FieldSignature( - new JavaClassType("Record", new PackageName("")), + identifierFactory.getFieldSignature( "b", - new JavaClassType("String", new PackageName("java.lang"))), - 1))); + identifierFactory.getClassType("Record"), + identifierFactory.getClassType("String", "java.lang")), + 1, + identifierFactory))); } } diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/minimaltestsuite/java6/AnnotationUsageInheritedTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/minimaltestsuite/java6/AnnotationUsageInheritedTest.java index 3d499d4e4b8..680714d2d56 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/minimaltestsuite/java6/AnnotationUsageInheritedTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/minimaltestsuite/java6/AnnotationUsageInheritedTest.java @@ -52,7 +52,7 @@ public void testInheritedAnnotationOnClass() { JavaClassType onClassType = identifierFactory.getClassType("OnClass"); Map elementValueMap = new HashMap<>(); elementValueMap.put("sthBlue", IntConstant.getInstance(42)); - elementValueMap.put("author", JavaJimple.newStringConstant("GeorgeLucas")); + elementValueMap.put("author", JavaJimple.newStringConstant("GeorgeLucas", identifierFactory)); assertEquals( Collections.singletonList(new AnnotationUsage(onClassType, elementValueMap)), actualInheritedAnnotationUsages); diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/minimaltestsuite/java6/AnnotationUsageTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/minimaltestsuite/java6/AnnotationUsageTest.java index 112a85ef43e..1470d7b0d0b 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/minimaltestsuite/java6/AnnotationUsageTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/minimaltestsuite/java6/AnnotationUsageTest.java @@ -50,7 +50,7 @@ public void testAnnotationOnClassOrAnnotation() { ClassType onClassType = identifierFactory.getClassType("OnClass"); Map elementValueMap = new HashMap<>(); elementValueMap.put("sthBlue", IntConstant.getInstance(42)); - elementValueMap.put("author", JavaJimple.newStringConstant("GeorgeLucas")); + elementValueMap.put("author", JavaJimple.newStringConstant("GeorgeLucas", identifierFactory)); assertEquals( Arrays.asList( @@ -70,7 +70,7 @@ public void testAnnotationOnField() { ClassType onFieldType = identifierFactory.getClassType("OnField"); Map annotationParamMap = new HashMap<>(); - annotationParamMap.put("isRipe", JavaJimple.newStringConstant("true")); + annotationParamMap.put("isRipe", JavaJimple.newStringConstant("true", identifierFactory)); assertEquals( Collections.singletonList(new AnnotationUsage(onFieldType, annotationParamMap)), @@ -108,7 +108,7 @@ public void testDefaultValues() { JavaSootClass annotationSootClass = classOptional.get(); Map elementValueMap = new HashMap<>(); - elementValueMap.put("isRipe", JavaJimple.newStringConstant("false")); + elementValueMap.put("isRipe", JavaJimple.newStringConstant("false", identifierFactory)); elementValueMap.put("sthNew", IntConstant.getInstance(789)); assertEquals(elementValueMap, annotationSootClass.getAnnotationDefaultValues()); @@ -134,7 +134,8 @@ public void testArrayDefaultValues() { elementValueMap.put( "value", Arrays.asList( - JavaJimple.newStringConstant("first"), JavaJimple.newStringConstant("second"))); + JavaJimple.newStringConstant("first", identifierFactory), + JavaJimple.newStringConstant("second", identifierFactory))); assertEquals(elementValueMap, annotationSootClass.getAnnotationDefaultValues()); } @@ -153,10 +154,14 @@ public void testEnumDefaultValues() { elementValueMap.put( "array", Arrays.asList( - JavaJimple.newEnumConstant("ENUM1", enumClass.getType().getFullyQualifiedName()), - JavaJimple.newEnumConstant("ENUM2", enumClass.getType().getFullyQualifiedName()))); + JavaJimple.newEnumConstant( + "ENUM1", enumClass.getType().getFullyQualifiedName(), identifierFactory), + JavaJimple.newEnumConstant( + "ENUM2", enumClass.getType().getFullyQualifiedName(), identifierFactory))); elementValueMap.put( - "single", JavaJimple.newEnumConstant("ENUM3", enumClass.getType().getFullyQualifiedName())); + "single", + JavaJimple.newEnumConstant( + "ENUM3", enumClass.getType().getFullyQualifiedName(), identifierFactory)); assertEquals(elementValueMap, annotationSootClass.getAnnotationDefaultValues()); } @@ -171,9 +176,10 @@ public void testClassDefaultValues() { elementValueMap.put( "array", Arrays.asList( - JavaJimple.newClassConstant("Ljava/lang/Boolean;"), - JavaJimple.newClassConstant("Ljava/lang/Double;"))); - elementValueMap.put("single", JavaJimple.newClassConstant("Ljava/lang/Integer;")); + JavaJimple.newClassConstant("Ljava/lang/Boolean;", identifierFactory), + JavaJimple.newClassConstant("Ljava/lang/Double;", identifierFactory))); + elementValueMap.put( + "single", JavaJimple.newClassConstant("Ljava/lang/Integer;", identifierFactory)); assertEquals(elementValueMap, annotationSootClass.getAnnotationDefaultValues()); } @@ -191,7 +197,8 @@ public void testContainerAnnotationDefaultValues() { Collections.singletonMap("countOnMe", IntConstant.getInstance(1337))); Map elementValueMap = new HashMap<>(); - elementValueMap.put("containerValue", JavaJimple.newStringConstant("defaultValue")); + elementValueMap.put( + "containerValue", JavaJimple.newStringConstant("defaultValue", identifierFactory)); elementValueMap.put("value", Collections.singletonList(baseAnnotationUsage)); assertEquals(elementValueMap, annotationSootClass.getAnnotationDefaultValues()); } @@ -281,7 +288,8 @@ public void testAnnotationOnMethod() { ClassType onMethodRepeatableType = identifierFactory.getClassType("OnMethodRepeatable"); Map elementValueMap = new HashMap<>(); - elementValueMap.put("containerValue", JavaJimple.newStringConstant("betterValue")); + elementValueMap.put( + "containerValue", JavaJimple.newStringConstant("betterValue", identifierFactory)); AnnotationUsage anno1 = new AnnotationUsage( @@ -308,7 +316,9 @@ public void testAnnotationWithArrayOnMethod() { Map elementValueMap = new HashMap<>(); elementValueMap.put( "value", - Arrays.asList(JavaJimple.newStringConstant("test"), JavaJimple.newStringConstant("test1"))); + Arrays.asList( + JavaJimple.newStringConstant("test", identifierFactory), + JavaJimple.newStringConstant("test1", identifierFactory))); assertEquals( Collections.singletonList(new AnnotationUsage(arrayConstantType, elementValueMap)), method.get().getAnnotations()); @@ -330,10 +340,14 @@ public void testAnnotationWithEnumOnMethod() { elementValueMap.put( "array", Arrays.asList( - JavaJimple.newEnumConstant("ENUM3", enumClass.getType().getFullyQualifiedName()), - JavaJimple.newEnumConstant("ENUM2", enumClass.getType().getFullyQualifiedName()))); + JavaJimple.newEnumConstant( + "ENUM3", enumClass.getType().getFullyQualifiedName(), identifierFactory), + JavaJimple.newEnumConstant( + "ENUM2", enumClass.getType().getFullyQualifiedName(), identifierFactory))); elementValueMap.put( - "single", JavaJimple.newEnumConstant("ENUM1", enumClass.getType().getFullyQualifiedName())); + "single", + JavaJimple.newEnumConstant( + "ENUM1", enumClass.getType().getFullyQualifiedName(), identifierFactory)); assertEquals( Collections.singletonList(new AnnotationUsage(enumAnnotationType, elementValueMap)), method.get().getAnnotations()); @@ -350,9 +364,10 @@ public void testAnnotationWithClassesOnMethod() { elementValueMap.put( "array", Arrays.asList( - JavaJimple.newClassConstant("Ljava/lang/Integer;"), - JavaJimple.newClassConstant("Ljava/lang/String;"))); - elementValueMap.put("single", JavaJimple.newClassConstant("Ljava/lang/Double;")); + JavaJimple.newClassConstant("Ljava/lang/Integer;", identifierFactory), + JavaJimple.newClassConstant("Ljava/lang/String;", identifierFactory))); + elementValueMap.put( + "single", JavaJimple.newClassConstant("Ljava/lang/Double;", identifierFactory)); assertEquals( Collections.singletonList(new AnnotationUsage(classAnnotationType, elementValueMap)), method.get().getAnnotations()); diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/minimaltestsuite/java6/NestedAnnotationTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/minimaltestsuite/java6/NestedAnnotationTest.java index bb9459182f0..8039b4c1702 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/minimaltestsuite/java6/NestedAnnotationTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/minimaltestsuite/java6/NestedAnnotationTest.java @@ -24,7 +24,8 @@ public void testNestedAnnotation() { ClassType mySecondInnerAnnotationType = identifierFactory.getClassType("MySecondInnerAnnotation"); Map secondInnerAnnotationParamMap = new LinkedHashMap<>(); - secondInnerAnnotationParamMap.put("value", JavaJimple.newStringConstant("second")); + secondInnerAnnotationParamMap.put( + "value", JavaJimple.newStringConstant("second", identifierFactory)); AnnotationUsage secondInnerAnnotation = new AnnotationUsage(mySecondInnerAnnotationType, secondInnerAnnotationParamMap); diff --git a/sootup.java.core/src/main/java/sootup/java/core/ConstantUtil.java b/sootup.java.core/src/main/java/sootup/java/core/ConstantUtil.java index 817a20db110..4935b462a1e 100644 --- a/sootup.java.core/src/main/java/sootup/java/core/ConstantUtil.java +++ b/sootup.java.core/src/main/java/sootup/java/core/ConstantUtil.java @@ -21,6 +21,8 @@ * . * #L% */ +import org.jspecify.annotations.NonNull; +import sootup.core.IdentifierFactory; import sootup.core.jimple.common.constant.BooleanConstant; import sootup.core.jimple.common.constant.ClassConstant; import sootup.core.jimple.common.constant.Constant; @@ -31,8 +33,19 @@ import sootup.core.jimple.common.constant.NullConstant; import sootup.java.core.language.JavaJimple; +/** Converts plain Java objects - e.g. annotation values - into their Jimple {@link Constant}. */ public class ConstantUtil { - public static Constant fromObject(Object obj) { + + private ConstantUtil() {} + + /** + * Converts the given object into the Jimple constant that represents it. + * + * @param obj the value to convert, may be {@code null} + * @param identifierFactory the factory that provides the types of reference constants + * @return the constant representing {@code obj} + */ + public static Constant fromObject(Object obj, @NonNull IdentifierFactory identifierFactory) { if (obj == null) { return NullConstant.getInstance(); } @@ -61,7 +74,7 @@ public static Constant fromObject(Object obj) { return LongConstant.getInstance((Long) obj); } if (obj instanceof String) { - return JavaJimple.newStringConstant((String) obj); + return JavaJimple.newStringConstant((String) obj, identifierFactory); } if (obj instanceof String[]) { @@ -69,11 +82,11 @@ public static Constant fromObject(Object obj) { // [0] is the fully qualified name of the enum // [1] is the value of the enum String[] enumData = (String[]) obj; - return JavaJimple.newEnumConstant(enumData[1], enumData[0]); + return JavaJimple.newEnumConstant(enumData[1], enumData[0], identifierFactory); } if (obj instanceof ClassConstant) { - return JavaJimple.newClassConstant(((ClassConstant) obj).getValue()); + return JavaJimple.newClassConstant(((ClassConstant) obj).getValue(), identifierFactory); } // TODO: [bh] implement MethodHandle, MethodType? diff --git a/sootup.java.core/src/main/java/sootup/java/core/JavaIdentifierFactory.java b/sootup.java.core/src/main/java/sootup/java/core/JavaIdentifierFactory.java index 33c4dbe1fa2..e95eca24679 100644 --- a/sootup.java.core/src/main/java/sootup/java/core/JavaIdentifierFactory.java +++ b/sootup.java.core/src/main/java/sootup/java/core/JavaIdentifierFactory.java @@ -22,8 +22,6 @@ * #L% */ -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; import com.google.common.collect.Maps; import java.util.*; import java.util.regex.Matcher; @@ -38,6 +36,7 @@ import sootup.core.signatures.MethodSignature; import sootup.core.signatures.MethodSubSignature; import sootup.core.signatures.PackageName; +import sootup.core.signatures.SignatureInterner; import sootup.core.types.ArrayType; import sootup.core.types.ClassType; import sootup.core.types.NullType; @@ -52,11 +51,10 @@ */ public class JavaIdentifierFactory implements IdentifierFactory { - @NonNull private static final JavaIdentifierFactory INSTANCE = new JavaIdentifierFactory(); - @NonNull public static final MethodSubSignature STATIC_INITIALIZER = - new MethodSubSignature("", Collections.emptyList(), VoidType.getInstance()); + SignatureInterner.getMethodSubSignature( + "", VoidType.getInstance(), Collections.emptyList()); @NonNull private static final Pattern SOOT_FIELD_SUB_SIGNATURE_PATTERN = @@ -66,27 +64,15 @@ public class JavaIdentifierFactory implements IdentifierFactory { private static final Pattern JAVADOCLIKE_FIELD_SUB_SIGNATURE_PATTERN = Pattern.compile("^#(?[^(]+):(?.+)$"); - /** Caches the created PackageNames for packages. */ - @NonNull - protected final Cache packageCache = - CacheBuilder.newBuilder().weakValues().build(); - - /** Caches class types */ - @NonNull - protected final Cache classTypeCache = - CacheBuilder.newBuilder().weakValues().build(); - @NonNull protected final Map primitiveTypeMap = Maps.newHashMapWithExpectedSize(8); - public static JavaIdentifierFactory getInstance() { - return INSTANCE; - } - - JavaIdentifierFactory() { - /* Represents the default package. */ - packageCache.put(PackageName.DEFAULT_PACKAGE.getName(), PackageName.DEFAULT_PACKAGE); - + /** + * Creates an identifier factory. Obtain one from {@link + * sootup.core.views.View#getIdentifierFactory()} rather than constructing it directly, so that + * the factory shares the lifetime of the {@link sootup.java.core.views.JavaView} that uses it. + */ + public JavaIdentifierFactory() { // initialize primitive map primitiveTypeMap.put( PrimitiveType.LongType.getInstance().getName(), PrimitiveType.LongType.getInstance()); @@ -107,8 +93,9 @@ public static JavaIdentifierFactory getInstance() { } /** - * Always creates a new ClassSignature. In opposite to PackageSignatures, ClassSignatures are not - * cached because the are unique per class, and thus reusing them does not make sense. + * Returns a unique ClassType. The method looks up a cache if it already contains a ClassType with + * the given name/package. If the cache lookup fails a new ClassType is created. This lets callers + * compare ClassTypes with {@code ==}. * * @param className the simple class name * @param packageName the Java package name; must not be null use empty string for the default @@ -119,11 +106,7 @@ public static JavaIdentifierFactory getInstance() { */ @Override public JavaClassType getClassType(final String className, final String packageName) { - PackageName packageIdentifier = getPackageName(packageName); - return classTypeCache - .asMap() - .computeIfAbsent( - className + packageName, (k) -> new JavaClassType(className, packageIdentifier)); + return JavaClassType.of(className, getPackageName(packageName)); } /** @@ -235,7 +218,7 @@ public ArrayType getArrayType(Type baseType, int dim) { */ @Override public PackageName getPackageName(@NonNull final String packageName) { - return packageCache.asMap().computeIfAbsent(packageName, PackageName::new); + return SignatureInterner.getPackageName(packageName); } /** @@ -260,7 +243,8 @@ public MethodSignature getMethodSignature( Type parameterSignature = getType(fqParameterName); parameterSignatures.add(parameterSignature); } - return new MethodSignature(declaringClass, methodName, parameterSignatures, returnType); + return getMethodSignature( + declaringClass, getMethodSubSignature(methodName, returnType, parameterSignatures)); } /** @@ -284,8 +268,9 @@ public MethodSignature getMethodSignature( Type parameterSignature = getType(fqParameterName); parameterSignatures.add(parameterSignature); } - return new MethodSignature( - declaringClassSignature, methodName, parameterSignatures, returnType); + return getMethodSignature( + declaringClassSignature, + getMethodSubSignature(methodName, returnType, parameterSignatures)); } @Override @@ -294,15 +279,19 @@ public MethodSignature getMethodSignature( final String methodName, final Type fqReturnType, final List parameters) { - - return new MethodSignature(declaringClassSignature, methodName, parameters, fqReturnType); + return getMethodSignature( + declaringClassSignature, getMethodSubSignature(methodName, fqReturnType, parameters)); } + /** + * Always returns the same, hash-consed MethodSignature for equal (declaringClass, subSignature) + * pairs, so callers may compare MethodSignatures with {@code ==}. + */ @Override @NonNull public MethodSignature getMethodSignature( @NonNull ClassType declaringClassSignature, @NonNull MethodSubSignature subSignature) { - return new MethodSignature(declaringClassSignature, subSignature); + return SignatureInterner.getMethodSignature(declaringClassSignature, subSignature); } private static final class MethodSignatureParserPatternHolder { @@ -411,7 +400,7 @@ public MethodSubSignature getMethodSubSignature( @NonNull String name, @NonNull Type returnType, @NonNull Iterable parameterSignatures) { - return new MethodSubSignature(name, parameterSignatures, returnType); + return SignatureInterner.getMethodSubSignature(name, returnType, parameterSignatures); } @NonNull @@ -568,26 +557,30 @@ public FieldSignature parseFieldSignature(@NonNull String fieldSignature) { public FieldSignature getFieldSignature( final String fieldName, final ClassType declaringClassSignature, final String fieldType) { Type type = getType(fieldType); - return new FieldSignature(declaringClassSignature, fieldName, type); + return getFieldSignature(declaringClassSignature, getFieldSubSignature(fieldName, type)); } @Override public FieldSignature getFieldSignature( final String fieldName, final ClassType declaringClassSignature, final Type fieldType) { - return new FieldSignature(declaringClassSignature, fieldName, fieldType); + return getFieldSignature(declaringClassSignature, getFieldSubSignature(fieldName, fieldType)); } + /** + * Always returns the same, hash-consed FieldSignature for equal (declaringClass, subSignature) + * pairs, so callers may compare FieldSignatures with {@code ==}. + */ @Override @NonNull public FieldSignature getFieldSignature( @NonNull ClassType declaringClassSignature, @NonNull FieldSubSignature subSignature) { - return new FieldSignature(declaringClassSignature, subSignature); + return SignatureInterner.getFieldSignature(declaringClassSignature, subSignature); } @NonNull @Override public FieldSubSignature getFieldSubSignature(@NonNull String name, @NonNull Type type) { - return new FieldSubSignature(name, type); + return SignatureInterner.getFieldSubSignature(name, type); } @NonNull diff --git a/sootup.java.core/src/main/java/sootup/java/core/JavaModuleIdentifierFactory.java b/sootup.java.core/src/main/java/sootup/java/core/JavaModuleIdentifierFactory.java index 3d55daf8457..fd6d0a4e542 100644 --- a/sootup.java.core/src/main/java/sootup/java/core/JavaModuleIdentifierFactory.java +++ b/sootup.java.core/src/main/java/sootup/java/core/JavaModuleIdentifierFactory.java @@ -38,39 +38,37 @@ public class JavaModuleIdentifierFactory extends JavaIdentifierFactory { public static final String MODULE_INFO_FILE = "module-info"; - private static final Cache modules = + @NonNull + private final Cache moduleIdentifierFactoryWrapper = CacheBuilder.newBuilder().weakValues().build(); - private static final JavaModuleIdentifierFactory INSTANCE = new JavaModuleIdentifierFactory(); - - public static JavaModuleIdentifierFactory getInstance() { - return INSTANCE; - } - - public static JavaModuleIdentifierFactory getInstance(@NonNull String module) { - return getInstance(getModuleSignature(module)); + /** + * Returns a view on this factory that resolves class names without an explicit module to {@code + * module}. The wrappers are cached per factory instance, so they are released together with the + * {@link sootup.java.core.views.JavaModuleView} this factory belongs to. + * + * @param module the name of the default module + * @return a factory that resolves module-less class names to {@code module} + */ + @NonNull + public JavaModuleIdentifierFactory forModule(@NonNull String module) { + return forModule(getModuleSignature(module)); } - private static final Cache - moduleIdentifierFactoryWrapper = CacheBuilder.newBuilder().weakValues().build(); - - public static JavaModuleIdentifierFactory getInstance(@NonNull ModuleSignature moduleSignature) { + /** + * Returns a view on this factory that resolves class names without an explicit module to {@code + * moduleSignature}. + * + * @param moduleSignature the signature of the default module + * @return a factory that resolves module-less class names to {@code moduleSignature} + */ + @NonNull + public JavaModuleIdentifierFactory forModule(@NonNull ModuleSignature moduleSignature) { return moduleIdentifierFactoryWrapper .asMap() .computeIfAbsent(moduleSignature, JavaModuleIdentifierFactoryWrapper::new); } - static { - /* - * Represents the unnamed module in Java's module system. Every type that is not defined in any known module but loaded - * from the classpath is associated with this unnamed module, so as to ensure that every type is associated with a - * module. - * - *

{@link ModuleSignature#UNNAMED_MODULE} - */ - modules.put(ModuleSignature.UNNAMED_MODULE.getModuleName(), ModuleSignature.UNNAMED_MODULE); - } - @Override public boolean isMainSubSignature(@NonNull MethodSubSignature methodSubSignature) { if (methodSubSignature.getName().equals("main")) { @@ -114,9 +112,9 @@ public ModuleJavaClassType getClassType(String fullyQualyfiedClassNameWithModule } /** - * Always creates a new ClassSignature. In opposite to PackageSignatures and ModuleSignatures, - * ClassSignatures are not cached because the are unique per class, and thus reusing them does not - * make sense. + * Returns a unique ClassType. The method looks up a cache if it already contains a ClassType with + * the given name/package/module. If the cache lookup fails a new ClassType is created. This lets + * callers compare ClassTypes with {@code ==}. * * @param className the simple name of the class * @param packageName the declaring package @@ -129,16 +127,14 @@ public ModuleJavaClassType getClassType( final @NonNull String className, final @NonNull String packageName, final @NonNull String moduleName) { - ModulePackageName packageIdentifier = getPackageName(packageName, moduleName); - return new ModuleJavaClassType(className, packageIdentifier); + return ModuleJavaClassType.of(className, getPackageName(packageName, moduleName)); } public ModuleJavaClassType getClassType( final @NonNull String className, final @NonNull String packageName, final @NonNull ModuleSignature moduleSignature) { - ModulePackageName packageIdentifier = getPackageName(packageName, moduleSignature); - return new ModuleJavaClassType(className, packageIdentifier); + return ModuleJavaClassType.of(className, getPackageName(packageName, moduleSignature)); } /** @@ -154,7 +150,7 @@ public ModuleJavaClassType getClassType( * the unnamed module. */ public static ModuleSignature getModuleSignature(@NonNull final String moduleName) { - return modules.asMap().computeIfAbsent(moduleName, ModuleSignature::new); + return ModuleSignature.of(moduleName); } @Override @@ -175,21 +171,12 @@ public ModulePackageName getPackageName(@NonNull final String packageName) { */ public ModulePackageName getPackageName( @NonNull final String packageName, @NonNull final String moduleName) { - String fqId = moduleName + "." + packageName; - return (ModulePackageName) - packageCache - .asMap() - .computeIfAbsent( - fqId, key -> new ModulePackageName(packageName, getModuleSignature(moduleName))); + return ModulePackageName.of(packageName, getModuleSignature(moduleName)); } public ModulePackageName getPackageName( @NonNull final String packageName, @NonNull final ModuleSignature moduleSignature) { - String fqId = moduleSignature.getModuleName() + "." + packageName; - return (ModulePackageName) - packageCache - .asMap() - .computeIfAbsent(fqId, key -> new ModulePackageName(packageName, moduleSignature)); + return ModulePackageName.of(packageName, moduleSignature); } /** Wrapper which refers to a given ModuleSignature when building stuff */ diff --git a/sootup.java.core/src/main/java/sootup/java/core/JavaPackageName.java b/sootup.java.core/src/main/java/sootup/java/core/JavaPackageName.java index b94fc43e6fe..ccc0def3c81 100644 --- a/sootup.java.core/src/main/java/sootup/java/core/JavaPackageName.java +++ b/sootup.java.core/src/main/java/sootup/java/core/JavaPackageName.java @@ -25,6 +25,7 @@ import org.jspecify.annotations.NonNull; import sootup.core.IdentifierFactory; import sootup.core.signatures.PackageName; +import sootup.core.signatures.SignatureInterner; /* * Actually, we could get rid of this class but removing it potentially breaks @@ -38,7 +39,16 @@ public class JavaPackageName extends PackageName { * * @param packageName the package's name */ - public JavaPackageName(@NonNull String packageName) { + protected JavaPackageName(@NonNull String packageName) { super(packageName); } + + /** + * Returns the unique {@link JavaPackageName} for the given package name, so that equal package + * names are the same instance and may be compared with {@code ==}. + */ + @NonNull + public static JavaPackageName of(@NonNull String packageName) { + return SignatureInterner.internPackageName(new JavaPackageName(packageName)); + } } diff --git a/sootup.java.core/src/main/java/sootup/java/core/exceptions/ExceptionInferResult.java b/sootup.java.core/src/main/java/sootup/java/core/exceptions/ExceptionInferResult.java index 30c3a6ed6de..6e3dd4e7312 100644 --- a/sootup.java.core/src/main/java/sootup/java/core/exceptions/ExceptionInferResult.java +++ b/sootup.java.core/src/main/java/sootup/java/core/exceptions/ExceptionInferResult.java @@ -26,7 +26,7 @@ import sootup.core.typehierarchy.TypeHierarchy; import sootup.core.types.ClassType; import sootup.core.util.ImmutableUtils; -import sootup.java.core.JavaIdentifierFactory; +import sootup.java.core.types.JavaClassType; public class ExceptionInferResult { @@ -122,39 +122,35 @@ protected ExceptionInferResult addExceptions( } public static class ExceptionType { - public static final JavaIdentifierFactory idFactory = JavaIdentifierFactory.getInstance(); - public static final ClassType THROWABLE = idFactory.getClassType("java.lang.Throwable"); + public static final ClassType THROWABLE = JavaClassType.of("java.lang.Throwable"); public static final ClassType NUll_POINTER_EXCEPTION = - idFactory.getClassType("java.lang.NullPointerException"); + JavaClassType.of("java.lang.NullPointerException"); public static final ClassType ARRAY_STORE_EXCEPTION = - idFactory.getClassType("java.lang.ArrayStoreException"); + JavaClassType.of("java.lang.ArrayStoreException"); public static final ClassType INDEX_OUT_OF_BOUNDS_EXCEPTION = - idFactory.getClassType("java.lang.IndexOutOfBoundsException"); + JavaClassType.of("java.lang.IndexOutOfBoundsException"); public static final ClassType ARITHMETIC_EXCEPTION = - idFactory.getClassType("java.lang.ArithmeticException"); + JavaClassType.of("java.lang.ArithmeticException"); public static final ClassType NEGATIVE_ARRAY_SIZE_EXCEPTION = - idFactory.getClassType("java.lang.NegativeArraySizeException"); + JavaClassType.of("java.lang.NegativeArraySizeException"); public static final ClassType CLASS_CAST_EXCEPTION = - idFactory.getClassType("java.lang.ClassCastException"); + JavaClassType.of("java.lang.ClassCastException"); public static final ClassType ILLEGAL_MONITOR_STATE_EXCEPTION = - idFactory.getClassType("java.lang.IllegalMonitorStateException"); + JavaClassType.of("java.lang.IllegalMonitorStateException"); } public static class ErrorType { - public static final JavaIdentifierFactory idFactory = JavaIdentifierFactory.getInstance(); - public static final ClassType INITIALIZATION_ERROR = idFactory.getClassType("java.lang.Error"); - public static final ClassType VM_ERROR = - idFactory.getClassType("java.lang.VirtualMachineError"); - public static final ClassType THREAD_DEATH = idFactory.getClassType("java.lang.ThreadDeath"); + public static final ClassType INITIALIZATION_ERROR = JavaClassType.of("java.lang.Error"); + public static final ClassType VM_ERROR = JavaClassType.of("java.lang.VirtualMachineError"); + public static final ClassType THREAD_DEATH = JavaClassType.of("java.lang.ThreadDeath"); public static final ClassType RESOLVE_FIELD_ERROR = - idFactory.getClassType("java.lang.NoSuchFieldError"); - public static final ClassType RESOLVE_CLASS_ERROR = - idFactory.getClassType("java.lang.LinkageError"); + JavaClassType.of("java.lang.NoSuchFieldError"); + public static final ClassType RESOLVE_CLASS_ERROR = JavaClassType.of("java.lang.LinkageError"); public static final ClassType ABSTRACT_METHOD_ERROR = - idFactory.getClassType("java.lang.AbstractMethodError"); + JavaClassType.of("java.lang.AbstractMethodError"); public static final ClassType NO_SUCH_METHOD_ERROR = - idFactory.getClassType("java.lang.NoSuchMethodError"); + JavaClassType.of("java.lang.NoSuchMethodError"); public static final ClassType UNSATISFIED_LINK_ERROR = - idFactory.getClassType("java.lang.UnsatisfiedLinkError"); + JavaClassType.of("java.lang.UnsatisfiedLinkError"); } } diff --git a/sootup.java.core/src/main/java/sootup/java/core/language/JavaJimple.java b/sootup.java.core/src/main/java/sootup/java/core/language/JavaJimple.java index a463a06d4bf..e32d1771542 100644 --- a/sootup.java.core/src/main/java/sootup/java/core/language/JavaJimple.java +++ b/sootup.java.core/src/main/java/sootup/java/core/language/JavaJimple.java @@ -24,6 +24,8 @@ import java.util.Collections; import java.util.List; +import org.jspecify.annotations.NonNull; +import sootup.core.IdentifierFactory; import sootup.core.jimple.Jimple; import sootup.core.jimple.common.constant.ClassConstant; import sootup.core.jimple.common.constant.EnumConstant; @@ -38,7 +40,6 @@ import sootup.core.types.Type; import sootup.core.types.VoidType; import sootup.java.core.AnnotationUsage; -import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.jimple.basic.JavaLocal; /** @@ -48,8 +49,6 @@ */ public class JavaJimple extends Jimple { - public static JavaIdentifierFactory javaIdentifierFactory = JavaIdentifierFactory.getInstance(); - public static boolean isJavaKeywordType(Type t) { // TODO: [JMP] Ensure that the check is complete. return t instanceof PrimitiveType || t instanceof VoidType || t instanceof NullType; @@ -60,39 +59,51 @@ public static JavaLocal newLocal(String name, Type t, Iterable return new JavaLocal(name, t, annotations); } - /** Constructs a CaughtExceptionRef() grammar chunk. */ - public static JCaughtExceptionRef newCaughtExceptionRef() { - return new JCaughtExceptionRef(javaIdentifierFactory.getType("java.lang.Throwable")); + /** + * Constructs a CaughtExceptionRef() grammar chunk. + * + * @param identifierFactory the factory that provides the {@code java.lang.Throwable} type + * @return the created caught exception reference + */ + public static JCaughtExceptionRef newCaughtExceptionRef( + @NonNull IdentifierFactory identifierFactory) { + return new JCaughtExceptionRef(identifierFactory.getType("java.lang.Throwable")); } - public static ClassConstant newClassConstant(String value) { - return new ClassConstant(value, javaIdentifierFactory.getType("java.lang.Class")); + public static ClassConstant newClassConstant( + String value, @NonNull IdentifierFactory identifierFactory) { + return new ClassConstant(value, identifierFactory.getType("java.lang.Class")); } - public static EnumConstant newEnumConstant(String value, String type) { - return new EnumConstant(value, javaIdentifierFactory.getClassType(type)); + public static EnumConstant newEnumConstant( + String value, String type, @NonNull IdentifierFactory identifierFactory) { + return new EnumConstant(value, identifierFactory.getClassType(type), identifierFactory); } - public static StringConstant newStringConstant(String value) { - return new StringConstant(value, javaIdentifierFactory.getType("java.lang.String")); + public static StringConstant newStringConstant( + String value, @NonNull IdentifierFactory identifierFactory) { + return new StringConstant(value, identifierFactory.getType("java.lang.String")); } public static MethodHandle newMethodHandle( - SootClassMemberSignature ref, int tag) { - return new MethodHandle( - ref, tag, javaIdentifierFactory.getType("java.lang.invoke.MethodHandle")); + SootClassMemberSignature ref, + int tag, + @NonNull IdentifierFactory identifierFactory) { + return new MethodHandle(ref, tag, identifierFactory.getType("java.lang.invoke.MethodHandle")); } public static MethodHandle newMethodHandle( - SootClassMemberSignature ref, MethodHandle.Kind kind) { - return new MethodHandle( - ref, kind, javaIdentifierFactory.getType("java.lang.invoke.MethodHandle")); + SootClassMemberSignature ref, + MethodHandle.Kind kind, + @NonNull IdentifierFactory identifierFactory) { + return new MethodHandle(ref, kind, identifierFactory.getType("java.lang.invoke.MethodHandle")); } - public static MethodType newMethodType(List parameterTypes, Type returnType) { + public static MethodType newMethodType( + List parameterTypes, Type returnType, @NonNull IdentifierFactory identifierFactory) { return new MethodType( - javaIdentifierFactory.getMethodSubSignature("__METHODTYPE__", returnType, parameterTypes), - javaIdentifierFactory.getClassType("java.lang.invoke.MethodType")); + identifierFactory.getMethodSubSignature("__METHODTYPE__", returnType, parameterTypes), + identifierFactory.getClassType("java.lang.invoke.MethodType")); } /** Constructs a Local with the given name and type. */ diff --git a/sootup.java.core/src/main/java/sootup/java/core/signatures/CommonClassSignatures.java b/sootup.java.core/src/main/java/sootup/java/core/signatures/CommonClassSignatures.java deleted file mode 100644 index 78773fcdffd..00000000000 --- a/sootup.java.core/src/main/java/sootup/java/core/signatures/CommonClassSignatures.java +++ /dev/null @@ -1,37 +0,0 @@ -package sootup.java.core.signatures; - -/*- - * #%L - * Soot - a J*va Optimization Framework - * %% - * Copyright (C) 2018-2020 Ben Hermann, Markus Schmidt - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 2.1 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * . - * #L% - */ - -import sootup.core.types.ClassType; -import sootup.java.core.JavaIdentifierFactory; - -/** - * Features class signatures for commonly used standard classes from the JDK - * - * @author Ben Hermann - */ -public final class CommonClassSignatures { - - public static final ClassType JavaLangObject = - JavaIdentifierFactory.getInstance().getClassType("Object", "java.lang"); -} diff --git a/sootup.java.core/src/main/java/sootup/java/core/signatures/ModulePackageName.java b/sootup.java.core/src/main/java/sootup/java/core/signatures/ModulePackageName.java index 5fbb4ac1724..55d3c7487d5 100644 --- a/sootup.java.core/src/main/java/sootup/java/core/signatures/ModulePackageName.java +++ b/sootup.java.core/src/main/java/sootup/java/core/signatures/ModulePackageName.java @@ -23,7 +23,9 @@ */ import com.google.common.base.Objects; +import org.jspecify.annotations.NonNull; import sootup.core.IdentifierFactory; +import sootup.core.signatures.SignatureInterner; import sootup.java.core.JavaPackageName; /** Represents the signature of a Java 9 package, referencing its module. */ @@ -38,11 +40,21 @@ public class ModulePackageName extends JavaPackageName { * @param packageName the package's name * @param moduleSignature the module declaring the package */ - public ModulePackageName(final String packageName, final ModuleSignature moduleSignature) { + protected ModulePackageName(final String packageName, final ModuleSignature moduleSignature) { super(packageName); this.moduleSignature = moduleSignature; } + /** + * Returns the unique {@link ModulePackageName} for the given package and module, so that equal + * module-scoped package names are the same instance and may be compared with {@code ==}. + */ + @NonNull + public static ModulePackageName of( + @NonNull String packageName, @NonNull ModuleSignature moduleSignature) { + return SignatureInterner.internPackageName(new ModulePackageName(packageName, moduleSignature)); + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/sootup.java.core/src/main/java/sootup/java/core/signatures/ModuleSignature.java b/sootup.java.core/src/main/java/sootup/java/core/signatures/ModuleSignature.java index 15385928b8a..98898a391c1 100644 --- a/sootup.java.core/src/main/java/sootup/java/core/signatures/ModuleSignature.java +++ b/sootup.java.core/src/main/java/sootup/java/core/signatures/ModuleSignature.java @@ -23,6 +23,9 @@ */ import com.google.common.base.Objects; +import com.google.common.collect.Interner; +import com.google.common.collect.Interners; +import org.jspecify.annotations.NonNull; import sootup.core.IdentifierFactory; import sootup.core.signatures.Signature; @@ -30,13 +33,16 @@ /** Represents a Java 9 module. */ public class ModuleSignature implements Signature { + + @NonNull private static final Interner INTERNER = Interners.newWeakInterner(); + /** * The unnamed module. If a request is made to load a type whose package is not defined in any * module then the module system load it from the classpath. To ensure that every type is * associated with a module, the type is associated with the unnamed module. @see http://openjdk.java.net/projects/jigsaw/spec/sotms/#the-unnamed-module */ - public static final ModuleSignature UNNAMED_MODULE = new ModuleSignature(""); + public static final ModuleSignature UNNAMED_MODULE = of(""); private final String moduleName; @@ -46,10 +52,19 @@ public class ModuleSignature implements Signature { * * @param moduleName module's name */ - public ModuleSignature(final String moduleName) { + protected ModuleSignature(final String moduleName) { this.moduleName = moduleName; } + /** + * Returns the unique {@link ModuleSignature} for the given module name, so that equal module + * signatures are the same instance and may be compared with {@code ==}. + */ + @NonNull + public static ModuleSignature of(@NonNull final String moduleName) { + return INTERNER.intern(new ModuleSignature(moduleName)); + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/sootup.java.core/src/main/java/sootup/java/core/types/JavaClassType.java b/sootup.java.core/src/main/java/sootup/java/core/types/JavaClassType.java index c2b67455e78..9867180f810 100644 --- a/sootup.java.core/src/main/java/sootup/java/core/types/JavaClassType.java +++ b/sootup.java.core/src/main/java/sootup/java/core/types/JavaClassType.java @@ -23,11 +23,14 @@ */ import com.google.common.base.Objects; +import com.google.common.collect.Interner; +import com.google.common.collect.Interners; import java.util.Optional; import java.util.regex.Pattern; +import org.apache.commons.lang3.ClassUtils; import org.jspecify.annotations.NonNull; -import sootup.core.IdentifierFactory; import sootup.core.signatures.PackageName; +import sootup.core.signatures.SignatureInterner; import sootup.core.types.*; /** Represents the unique fully-qualified name of a Class (aka its signature). */ @@ -46,13 +49,68 @@ public class JavaClassType extends ClassType { private final int hashCode; /** - * Internal: Constructs the fully-qualified ClassSignature. Instances should only be created by a - * {@link IdentifierFactory} + * Hash-conses every {@link JavaClassType} and its subclasses. Interning happens on the + * constructed object, so identity follows {@link #equals}, which compares the exact class - a + * {@link sootup.java.core.types.ModuleJavaClassType} therefore never aliases a plain {@link + * JavaClassType} with the same name. + */ + @NonNull private static final Interner INTERNER = Interners.newWeakInterner(); + + /** + * Returns the unique {@link JavaClassType} for the given class and package, so that equal class + * types are the same instance and may be compared with {@code ==}. + * + * @param className the simple name of the class, e.g., ClassA NOT my.package.ClassA + * @param packageName the package the class is contained in + * @return the hash-consed class type for {@code packageName.className} + */ + @NonNull + public static JavaClassType of( + @NonNull final String className, @NonNull final PackageName packageName) { + return intern(new JavaClassType(className, packageName)); + } + + /** + * Returns the unique {@link JavaClassType} for the given fully-qualified class name. + * + *

Prefer {@link sootup.core.IdentifierFactory#getClassType(String)} obtained from a {@link + * sootup.core.views.View}; this overload exists for the few well-known JDK types that are needed + * as {@code static final} constants, where no view is available. + * + * @param fullyQualifiedClassName the fully-qualified class name, e.g., my.package.ClassA + * @return the hash-consed class type for the given name + */ + @NonNull + public static JavaClassType of(@NonNull final String fullyQualifiedClassName) { + return of( + ClassUtils.getShortClassName(fullyQualifiedClassName), + SignatureInterner.getPackageName(ClassUtils.getPackageName(fullyQualifiedClassName))); + } + + /** + * Interns an already constructed class type. Subclasses construct themselves and pass the result + * through here so that they share the interning invariant. + * + * @param the concrete class type that is interned + * @param classType the freshly constructed class type + * @return the canonical instance that is equal to {@code classType} + */ + @NonNull + protected static T intern(@NonNull T classType) { + @SuppressWarnings("unchecked") + T interned = (T) INTERNER.intern(classType); + return interned; + } + + /** + * Internal: Constructs the fully-qualified ClassSignature. Instances should only be created via + * {@link #of(String, PackageName)} or an {@link sootup.core.IdentifierFactory}, so that they stay + * hash-consed. * * @param className the simple name of the class, e.g., ClassA NOT my.package.ClassA * @param packageName the corresponding package */ - public JavaClassType(@NonNull final String className, @NonNull final PackageName packageName) { + protected JavaClassType(@NonNull final String className, @NonNull final PackageName packageName) { String realClassName = className; // TODO: [ms] we shouldnt do that inner class conversion here? -> IdentifierFactory if (realClassName.contains(".")) { diff --git a/sootup.java.core/src/main/java/sootup/java/core/types/ModuleJavaClassType.java b/sootup.java.core/src/main/java/sootup/java/core/types/ModuleJavaClassType.java index 388599e00c1..9c44797f19a 100644 --- a/sootup.java.core/src/main/java/sootup/java/core/types/ModuleJavaClassType.java +++ b/sootup.java.core/src/main/java/sootup/java/core/types/ModuleJavaClassType.java @@ -29,11 +29,21 @@ public class ModuleJavaClassType extends JavaClassType { - public ModuleJavaClassType( + protected ModuleJavaClassType( @NonNull final String className, @NonNull final ModulePackageName packageName) { super(className, packageName); } + /** + * Returns the unique {@link ModuleJavaClassType} for the given class and module-scoped package, + * so that equal class types are the same instance and may be compared with {@code ==}. + */ + @NonNull + public static ModuleJavaClassType of( + @NonNull final String className, @NonNull final ModulePackageName packageName) { + return intern(new ModuleJavaClassType(className, packageName)); + } + @NonNull @Override public ModulePackageName getPackageName() { diff --git a/sootup.java.core/src/main/java/sootup/java/core/views/JavaEagerView.java b/sootup.java.core/src/main/java/sootup/java/core/views/JavaEagerView.java index d17d6c826f3..55adb621123 100644 --- a/sootup.java.core/src/main/java/sootup/java/core/views/JavaEagerView.java +++ b/sootup.java.core/src/main/java/sootup/java/core/views/JavaEagerView.java @@ -44,10 +44,6 @@ public JavaEagerView(@NonNull List inputLocations) { public JavaEagerView( @NonNull List inputLocations, @NonNull ClassCacheProvider cacheProvider) { - super( - inputLocations, - cacheProvider, - LoadingStrategy.eager(), - JavaIdentifierFactory.getInstance()); + super(inputLocations, cacheProvider, LoadingStrategy.eager(), new JavaIdentifierFactory()); } } diff --git a/sootup.java.core/src/main/java/sootup/java/core/views/JavaModuleView.java b/sootup.java.core/src/main/java/sootup/java/core/views/JavaModuleView.java index 000fddc4189..b31e9c35d6e 100644 --- a/sootup.java.core/src/main/java/sootup/java/core/views/JavaModuleView.java +++ b/sootup.java.core/src/main/java/sootup/java/core/views/JavaModuleView.java @@ -62,7 +62,7 @@ public JavaModuleView( inputLocations, cacheProvider, LoadingStrategy.onDemand(), - JavaModuleIdentifierFactory.getInstance()); + new JavaModuleIdentifierFactory()); this.moduleInfoAnalysisInputLocations = moduleInputLocations; JavaModuleInfo unnamedModuleInfo = JavaModuleInfo.getUnnamedModuleInfo(); moduleInfoMap.put(unnamedModuleInfo.getModuleSignature(), unnamedModuleInfo); diff --git a/sootup.java.core/src/main/java/sootup/java/core/views/JavaView.java b/sootup.java.core/src/main/java/sootup/java/core/views/JavaView.java index 3c7e75ce791..e1af749524a 100644 --- a/sootup.java.core/src/main/java/sootup/java/core/views/JavaView.java +++ b/sootup.java.core/src/main/java/sootup/java/core/views/JavaView.java @@ -71,10 +71,20 @@ public JavaView( @NonNull List inputLocations, @NonNull ClassCacheProvider cacheProvider, @NonNull LoadingStrategy loadingStrategy) { - this(inputLocations, cacheProvider, loadingStrategy, JavaIdentifierFactory.getInstance()); + this(inputLocations, cacheProvider, loadingStrategy, new JavaIdentifierFactory()); } - protected JavaView( + /** + * Creates a view that uses the given {@link JavaIdentifierFactory} instead of one of its own. The + * factory shares the lifetime of the views it is handed to, so passing the same instance to + * several views is the way to share it between them. + * + * @param inputLocations the input locations the view resolves classes from + * @param cacheProvider provides the cache that stores the resolved classes + * @param loadingStrategy determines when classes are resolved + * @param idf the identifier factory this view hands out + */ + public JavaView( @NonNull List inputLocations, @NonNull ClassCacheProvider cacheProvider, @NonNull LoadingStrategy loadingStrategy, diff --git a/sootup.java.core/src/test/java/sootup/java/core/ConstantUtilTest.java b/sootup.java.core/src/test/java/sootup/java/core/ConstantUtilTest.java index 3867f1957ff..e1f86d19832 100644 --- a/sootup.java.core/src/test/java/sootup/java/core/ConstantUtilTest.java +++ b/sootup.java.core/src/test/java/sootup/java/core/ConstantUtilTest.java @@ -6,12 +6,15 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; +import sootup.core.IdentifierFactory; import sootup.core.jimple.common.constant.Constant; import sootup.core.jimple.common.constant.IntConstant; import sootup.core.jimple.common.constant.NullConstant; class ConstantUtilTest { + private final IdentifierFactory identifierFactory = new JavaIdentifierFactory(); + /** * Reproduces failure when converting Byte and Character boxed literals to Jimple Constants. * @@ -26,24 +29,26 @@ class ConstantUtilTest { */ @Test void testFromObjectSupportsByteAndCharacter() { - Constant byteConst = ConstantUtil.fromObject((byte) 42); + Constant byteConst = ConstantUtil.fromObject((byte) 42, identifierFactory); IntConstant intConst1 = assertInstanceOf(IntConstant.class, byteConst); assertEquals(42, intConst1.getValue()); - Constant charConst = ConstantUtil.fromObject('Z'); + Constant charConst = ConstantUtil.fromObject('Z', identifierFactory); IntConstant intConst2 = assertInstanceOf(IntConstant.class, charConst); assertEquals((int) 'Z', intConst2.getValue()); } @Test void testFromObjectNull() { - assertInstanceOf(NullConstant.class, ConstantUtil.fromObject(null)); + assertInstanceOf(NullConstant.class, ConstantUtil.fromObject(null, identifierFactory)); } @Test void testFromObjectUnsupportedThrowsWithClassName() { IllegalArgumentException ex = - assertThrows(IllegalArgumentException.class, () -> ConstantUtil.fromObject(new Object())); + assertThrows( + IllegalArgumentException.class, + () -> ConstantUtil.fromObject(new Object(), identifierFactory)); assertTrue(ex.getMessage().contains("java.lang.Object")); } } diff --git a/sootup.java.core/src/test/java/sootup/java/core/jimple/common/stmt/JAssignStmtTest.java b/sootup.java.core/src/test/java/sootup/java/core/jimple/common/stmt/JAssignStmtTest.java index 448bf983a5f..5e70af90c1d 100644 --- a/sootup.java.core/src/test/java/sootup/java/core/jimple/common/stmt/JAssignStmtTest.java +++ b/sootup.java.core/src/test/java/sootup/java/core/jimple/common/stmt/JAssignStmtTest.java @@ -116,13 +116,13 @@ public void test() { // test JFieldRef cast for JFieldRef - should not throw an Exception Local someLocal = - new Local("r42", JavaIdentifierFactory.getInstance().getClassType("Abc.def.Alphabet")); + new Local("r42", new JavaIdentifierFactory().getClassType("Abc.def.Alphabet")); final JStaticFieldRef somefield = Jimple.newStaticFieldRef( - JavaIdentifierFactory.getInstance() + new JavaIdentifierFactory() .getFieldSignature( "somefield", - JavaIdentifierFactory.getInstance().getClassType("Abc.def.Alphabet"), + new JavaIdentifierFactory().getClassType("Abc.def.Alphabet"), PrimitiveType.getInt())); final JAssignStmt jAssignStmtField = Jimple.newAssignStmt(someLocal, somefield, StmtPositionInfo.getNoStmtPositionInfo()); diff --git a/sootup.java.core/src/test/java/sootup/java/core/jimple/common/stmt/JGotoStmtTest.java b/sootup.java.core/src/test/java/sootup/java/core/jimple/common/stmt/JGotoStmtTest.java index 34375db7c0e..82369397202 100644 --- a/sootup.java.core/src/test/java/sootup/java/core/jimple/common/stmt/JGotoStmtTest.java +++ b/sootup.java.core/src/test/java/sootup/java/core/jimple/common/stmt/JGotoStmtTest.java @@ -42,7 +42,7 @@ public class JGotoStmtTest { public void test() { StmtPositionInfo nop = StmtPositionInfo.getNoStmtPositionInfo(); - JavaIdentifierFactory typeFactory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory typeFactory = new JavaIdentifierFactory(); Local local = new Local("r0", typeFactory.getType("java.lang.Exception")); diff --git a/sootup.java.core/src/test/java/sootup/java/core/jimple/common/stmt/JIdentityStmtTest.java b/sootup.java.core/src/test/java/sootup/java/core/jimple/common/stmt/JIdentityStmtTest.java index a6be491a93a..21125a45d2f 100644 --- a/sootup.java.core/src/test/java/sootup/java/core/jimple/common/stmt/JIdentityStmtTest.java +++ b/sootup.java.core/src/test/java/sootup/java/core/jimple/common/stmt/JIdentityStmtTest.java @@ -43,7 +43,7 @@ public class JIdentityStmtTest { @Test public void test() { StmtPositionInfo nop = StmtPositionInfo.getNoStmtPositionInfo(); - JavaIdentifierFactory typeFactory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory typeFactory = new JavaIdentifierFactory(); Local thiz = new Local("r0", typeFactory.getType("somepackage.dummy.MyClass")); Stmt thisIdStmt = @@ -55,7 +55,8 @@ public void test() { new JIdentityStmt(param, new JParameterRef(PrimitiveType.getInt(), 123), nop); Local exception = new Local("r1", typeFactory.getType("java.lang.Exception")); - Stmt exceptionIdStmt = new JIdentityStmt(exception, JavaJimple.newCaughtExceptionRef(), nop); + Stmt exceptionIdStmt = + new JIdentityStmt(exception, JavaJimple.newCaughtExceptionRef(typeFactory), nop); // toString assertEquals("r0 := @this: somepackage.dummy.MyClass", thisIdStmt.toString()); @@ -99,7 +100,7 @@ public void test() { thisIdStmt.equivTo( new JIdentityStmt( new Local("r1", typeFactory.getType("somepckg.NotMyException")), - JavaJimple.newCaughtExceptionRef(), + JavaJimple.newCaughtExceptionRef(typeFactory), nop))); assertFalse(paramIdStmt.equivTo(thisIdStmt)); assertFalse(paramIdStmt.equivTo(exceptionIdStmt)); diff --git a/sootup.java.core/src/test/java/sootup/java/core/jimple/common/stmt/JInvokeStmtTest.java b/sootup.java.core/src/test/java/sootup/java/core/jimple/common/stmt/JInvokeStmtTest.java index 014d26b470e..19f8af1a2ac 100644 --- a/sootup.java.core/src/test/java/sootup/java/core/jimple/common/stmt/JInvokeStmtTest.java +++ b/sootup.java.core/src/test/java/sootup/java/core/jimple/common/stmt/JInvokeStmtTest.java @@ -63,7 +63,7 @@ public class JInvokeStmtTest { public void test() { StmtPositionInfo nop = StmtPositionInfo.getNoStmtPositionInfo(); - JavaIdentifierFactory dif = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory dif = new JavaIdentifierFactory(); Path dummyPath = Paths.get(URI.create("file:/nonexistent.java")); JavaClassType superClassSignature = dif.getClassType("java.lang.Object"); @@ -94,7 +94,8 @@ public void test() { Stmt staticInvokeStmt = new JInvokeStmt( new JStaticInvokeExpr( - statMethodSig, Collections.singletonList(JavaJimple.newStringConstant("Towel"))), + statMethodSig, + Collections.singletonList(JavaJimple.newStringConstant("Towel", dif))), nop); // toString diff --git a/sootup.java.core/src/test/java/sootup/java/core/jimple/common/stmt/JThrowStmtTest.java b/sootup.java.core/src/test/java/sootup/java/core/jimple/common/stmt/JThrowStmtTest.java index 172a3a1a184..6e9f0430384 100644 --- a/sootup.java.core/src/test/java/sootup/java/core/jimple/common/stmt/JThrowStmtTest.java +++ b/sootup.java.core/src/test/java/sootup/java/core/jimple/common/stmt/JThrowStmtTest.java @@ -40,7 +40,7 @@ public class JThrowStmtTest { @Test public void test() { StmtPositionInfo nop = StmtPositionInfo.getNoStmtPositionInfo(); - JavaIdentifierFactory typeFactory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory typeFactory = new JavaIdentifierFactory(); Local local = new Local("r0", typeFactory.getType("java.lang.Exception")); Local localEqual = new Local("r0", typeFactory.getType("java.lang.Exception")); diff --git a/sootup.java.core/src/test/java/sootup/java/core/model/SootMethodTest.java b/sootup.java.core/src/test/java/sootup/java/core/model/SootMethodTest.java index 7ec88e59bbe..d9372359db2 100644 --- a/sootup.java.core/src/test/java/sootup/java/core/model/SootMethodTest.java +++ b/sootup.java.core/src/test/java/sootup/java/core/model/SootMethodTest.java @@ -128,7 +128,7 @@ public void testCreateMethod() { @Test public void constructorTest() { - IdentifierFactory idf = JavaIdentifierFactory.getInstance(); + IdentifierFactory idf = new JavaIdentifierFactory(); SootMethod mockedConstructor = spy(JavaSootMethod.JavaSootMethodBuilder.builder().build()); MethodSignature mockedSignature = mock(MethodSignature.class); diff --git a/sootup.java.core/src/test/java/sootup/java/core/signatures/JavaIdentifierFactoryTest.java b/sootup.java.core/src/test/java/sootup/java/core/signatures/JavaIdentifierFactoryTest.java index 2da92d34f2e..65f048b0b29 100644 --- a/sootup.java.core/src/test/java/sootup/java/core/signatures/JavaIdentifierFactoryTest.java +++ b/sootup.java.core/src/test/java/sootup/java/core/signatures/JavaIdentifierFactoryTest.java @@ -41,8 +41,8 @@ public class JavaIdentifierFactoryTest { - private final IdentifierFactory identifierFactory = JavaIdentifierFactory.getInstance(); - private final JavaIdentifierFactory typeFactory = JavaIdentifierFactory.getInstance(); + private final IdentifierFactory identifierFactory = new JavaIdentifierFactory(); + private final JavaIdentifierFactory typeFactory = new JavaIdentifierFactory(); @Test public void getSamePackageSignature() { @@ -131,9 +131,9 @@ public void getClassSignatureFullyQualified() { public void getInnerClassSignature() { JavaClassType classSignature1 = typeFactory.getClassType("java.lang.System$MyClass"); JavaClassType classSignature2 = typeFactory.getClassType("System$MyClass", "java.lang"); - // Class Signatures are unique but not their package - assertNotSame(classSignature1, classSignature2); - assertEquals(classSignature1, classSignature2); + // the two spellings differ only in where the name is split; the inner-class normalization in + // the constructor makes them equal, and interning makes them the same instance + assertSame(classSignature1, classSignature2); } @Test @@ -209,6 +209,59 @@ public void getFieldSignature() { assertEquals("" + "", fieldSignature.toString()); } + @Test + public void getSameMethodSignature() { + List parameters = Collections.singletonList("java.lang.Class"); + MethodSignature methodSignature1 = + identifierFactory.getMethodSignature("java.lang.System", "foo", "java.lang.A", parameters); + MethodSignature methodSignature2 = + identifierFactory.getMethodSignature("java.lang.System", "foo", "java.lang.A", parameters); + assertSame(methodSignature1, methodSignature2); + } + + @Test + public void getSameMethodSignatureViaSubSignature() { + ClassType declClass = identifierFactory.getClassType("java.lang.System"); + MethodSubSignature subSignature = + identifierFactory.getMethodSubSignature( + "foo", VoidType.getInstance(), Collections.emptyList()); + MethodSignature methodSignature1 = + identifierFactory.getMethodSignature(declClass, subSignature); + MethodSignature methodSignature2 = + identifierFactory.getMethodSignature(declClass, subSignature); + assertSame(methodSignature1, methodSignature2); + } + + @Test + public void getSameFieldSignature() { + ClassType classSignature = identifierFactory.getClassType("java.lang.System"); + FieldSignature fieldSignature1 = + identifierFactory.getFieldSignature("foo", classSignature, "int"); + FieldSignature fieldSignature2 = + identifierFactory.getFieldSignature("foo", classSignature, "int"); + assertSame(fieldSignature1, fieldSignature2); + } + + @Test + public void getSameMethodSubSignature() { + MethodSubSignature subSignature1 = + identifierFactory.getMethodSubSignature( + "foo", VoidType.getInstance(), Collections.singletonList(PrimitiveType.getInt())); + MethodSubSignature subSignature2 = + identifierFactory.getMethodSubSignature( + "foo", VoidType.getInstance(), Collections.singletonList(PrimitiveType.getInt())); + assertSame(subSignature1, subSignature2); + } + + @Test + public void getSameFieldSubSignature() { + FieldSubSignature subSignature1 = + identifierFactory.getFieldSubSignature("foo", PrimitiveType.getInt()); + FieldSubSignature subSignature2 = + identifierFactory.getFieldSubSignature("foo", PrimitiveType.getInt()); + assertSame(subSignature1, subSignature2); + } + @Test public void compMethodSignature2() { List parameters = new ArrayList<>(); @@ -232,7 +285,8 @@ public void compMethodSignature1() { MethodSignature methodSignature2 = identifierFactory.getMethodSignature("java.lang.System", "foo", "void", parameters); - assertEquals(methodSignature, methodSignature2); + // now hash-consed: equal MethodSignatures are the same instance + assertSame(methodSignature, methodSignature2); assertEquals(methodSignature.hashCode(), methodSignature2.hashCode()); } @@ -343,7 +397,49 @@ public void testParse() { String fieldsSigStr = ""; assertThrows( IllegalArgumentException.class, - () -> JavaModuleIdentifierFactory.getInstance().parseFieldSignature(fieldsSigStr)); + () -> new JavaModuleIdentifierFactory().parseFieldSignature(fieldsSigStr)); + } + + @Test + public void classTypesWithAmbiguousNameSplitAreDistinct() { + // "AB" in the default package and "B.AB"... would collide under a cache keyed on the plain + // concatenation of class name and package name. + JavaClassType defaultPackaged = typeFactory.getClassType("AB", ""); + JavaClassType packaged = typeFactory.getClassType("B", "A"); + assertNotEquals(defaultPackaged, packaged); + assertNotSame(defaultPackaged, packaged); + assertEquals("AB", defaultPackaged.getFullyQualifiedName()); + assertEquals("A.B", packaged.getFullyQualifiedName()); + } + + @Test + public void subSignatureIsSharedWithTheSignatureBuiltFromRawNames() { + MethodSignature methodSignature = + identifierFactory.getMethodSignature( + typeFactory.getClassType("some.Klass"), + "foo", + "void", + Collections.singletonList("int")); + MethodSubSignature subSignature = + identifierFactory.getMethodSubSignature( + "foo", VoidType.getInstance(), Collections.singletonList(PrimitiveType.getInt())); + assertSame(subSignature, methodSignature.getSubSignature()); + + FieldSignature fieldSignature = + identifierFactory.getFieldSignature( + "bar", typeFactory.getClassType("some.Klass"), PrimitiveType.getInt()); + assertSame( + identifierFactory.getFieldSubSignature("bar", PrimitiveType.getInt()), + fieldSignature.getSubSignature()); + } + + @Test + public void packageNameEqualityIsSymmetric() { + PackageName packageName = identifierFactory.getPackageName("java.lang"); + ModulePackageName modulePackageName = + new JavaModuleIdentifierFactory().getPackageName("java.lang", "java.base"); + assertEquals(packageName.equals(modulePackageName), modulePackageName.equals(packageName)); + assertNotSame(packageName, modulePackageName); } /** diff --git a/sootup.java.core/src/test/java/sootup/java/core/signatures/JavaModuleIdentifierFactoryTest.java b/sootup.java.core/src/test/java/sootup/java/core/signatures/JavaModuleIdentifierFactoryTest.java index 41ea48412eb..f5ad1596ca8 100644 --- a/sootup.java.core/src/test/java/sootup/java/core/signatures/JavaModuleIdentifierFactoryTest.java +++ b/sootup.java.core/src/test/java/sootup/java/core/signatures/JavaModuleIdentifierFactoryTest.java @@ -5,7 +5,10 @@ import org.junit.jupiter.api.Test; import sootup.core.signatures.FieldSignature; import sootup.core.signatures.MethodSignature; +import sootup.core.types.ClassType; +import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.JavaModuleIdentifierFactory; +import sootup.java.core.types.ModuleJavaClassType; /*- * #%L @@ -33,14 +36,14 @@ public class JavaModuleIdentifierFactoryTest extends JavaIdentifierFactoryTest { @Test public void getPackageSignatureUnnamedModule() { - JavaModuleIdentifierFactory identifierFactory = JavaModuleIdentifierFactory.getInstance(); + JavaModuleIdentifierFactory identifierFactory = new JavaModuleIdentifierFactory(); ModulePackageName packageName1 = identifierFactory.getPackageName("java.lang"); assertSame(packageName1.getModuleSignature(), ModuleSignature.UNNAMED_MODULE); } @Test public void getPackageSignatureNamedModule() { - JavaModuleIdentifierFactory identifierFactory = JavaModuleIdentifierFactory.getInstance(); + JavaModuleIdentifierFactory identifierFactory = new JavaModuleIdentifierFactory(); ModulePackageName packageName1 = identifierFactory.getPackageName("java.lang", "myModule"); assertNotSame(packageName1.getModuleSignature(), ModuleSignature.UNNAMED_MODULE); assertEquals(packageName1.getModuleSignature().toString(), "myModule"); @@ -48,7 +51,7 @@ public void getPackageSignatureNamedModule() { @Test public void getModulePackageSignature() { - JavaModuleIdentifierFactory identifierFactory = JavaModuleIdentifierFactory.getInstance(); + JavaModuleIdentifierFactory identifierFactory = new JavaModuleIdentifierFactory(); ModulePackageName packageSignature1 = identifierFactory.getPackageName("java.lang", "myModule"); ModulePackageName packageSignature2 = identifierFactory.getPackageName("java.lang.invoke", "myModule"); @@ -57,7 +60,7 @@ public void getModulePackageSignature() { @Test public void getModulePackageSignatureSameModule() { - JavaModuleIdentifierFactory identifierFactory = JavaModuleIdentifierFactory.getInstance(); + JavaModuleIdentifierFactory identifierFactory = new JavaModuleIdentifierFactory(); ModulePackageName packageSignature1 = identifierFactory.getPackageName("java.lang", "myModule"); ModulePackageName packageSignature2 = identifierFactory.getPackageName("java.lang", "myModule"); @@ -67,7 +70,7 @@ public void getModulePackageSignatureSameModule() { @Test public void getModulePackageSignatureDiffModule() { - JavaModuleIdentifierFactory identifierFactory = JavaModuleIdentifierFactory.getInstance(); + JavaModuleIdentifierFactory identifierFactory = new JavaModuleIdentifierFactory(); ModulePackageName packageSignature1 = identifierFactory.getPackageName("java.lang", "myModule1"); ModulePackageName packageSignature2 = @@ -80,9 +83,30 @@ public void getModulePackageSignatureDiffModule() { assertNotEquals(packageSignature1, packageSignature2); } + @Test + public void getSameClassTypeSameModule() { + JavaModuleIdentifierFactory identifierFactory = new JavaModuleIdentifierFactory(); + ModuleJavaClassType classType1 = + identifierFactory.getClassType("Strawberry", "fruit.red", "food.fruit"); + ModuleJavaClassType classType2 = + identifierFactory.getClassType("Strawberry", "fruit.red", "food.fruit"); + assertSame(classType1, classType2); + } + + @Test + public void getDiffClassTypeDiffModule() { + JavaModuleIdentifierFactory identifierFactory = new JavaModuleIdentifierFactory(); + ModuleJavaClassType classType1 = + identifierFactory.getClassType("Strawberry", "fruit.red", "food.fruit"); + ModuleJavaClassType classType2 = + identifierFactory.getClassType("Strawberry", "fruit.red", "food.other"); + assertNotSame(classType1, classType2); + assertNotEquals(classType1, classType2); + } + @Test public void testModuleInfoSignature() { - JavaModuleIdentifierFactory typeFactory = JavaModuleIdentifierFactory.getInstance(); + JavaModuleIdentifierFactory typeFactory = new JavaModuleIdentifierFactory(); assertThrows(IllegalArgumentException.class, () -> typeFactory.getClassType("module-info")); } @@ -109,12 +133,12 @@ public void compModuleSignature2() { public void parseMethodnFieldSig() { String methodSignatureString = ""; MethodSignature methodSignature = - JavaModuleIdentifierFactory.getInstance().parseMethodSignature(methodSignatureString); + new JavaModuleIdentifierFactory().parseMethodSignature(methodSignatureString); assertEquals(methodSignatureString, methodSignature.toString()); String fieldsSigStr = ""; FieldSignature fieldSignature = - JavaModuleIdentifierFactory.getInstance().parseFieldSignature(fieldsSigStr); + new JavaModuleIdentifierFactory().parseFieldSignature(fieldsSigStr); assertEquals(fieldsSigStr, fieldSignature.toString()); } @@ -122,7 +146,7 @@ public void parseMethodnFieldSig() { public void wrapper_test() { ModuleSignature baseSig = JavaModuleIdentifierFactory.getModuleSignature("java.base"); - JavaModuleIdentifierFactory wrapper = JavaModuleIdentifierFactory.getInstance(baseSig); + JavaModuleIdentifierFactory wrapper = new JavaModuleIdentifierFactory().forModule(baseSig); assertEquals( "java.base/fruit.red.Strawberry", wrapper.getClassType("fruit.red.Strawberry").toString()); assertEquals( @@ -178,7 +202,7 @@ public void wrapper_test() { @Test public void isMainMethod() { - JavaModuleIdentifierFactory identifierFactory = JavaModuleIdentifierFactory.getInstance(); + JavaModuleIdentifierFactory identifierFactory = new JavaModuleIdentifierFactory(); MethodSignature mainMethodSig = identifierFactory.parseMethodSignature( ""); @@ -189,4 +213,53 @@ public void isMainMethod() { ""); assertTrue(identifierFactory.isMainSubSignature(mainMethodSigJ9.getSubSignature())); } + + @Test + public void modulePackageNamesWithAmbiguousSplitAreDistinct() { + // module "a" / package "b.c" and module "a.b" / package "c" would collide under a cache keyed + // on moduleName + "." + packageName. + JavaModuleIdentifierFactory identifierFactory = new JavaModuleIdentifierFactory(); + ModulePackageName first = identifierFactory.getPackageName("b.c", "a"); + ModulePackageName second = identifierFactory.getPackageName("c", "a.b"); + assertNotSame(first, second); + assertNotEquals(first, second); + assertEquals("b.c", first.getName()); + assertEquals("c", second.getName()); + assertEquals("a", first.getModuleSignature().getModuleName()); + assertEquals("a.b", second.getModuleSignature().getModuleName()); + } + + @Test + public void classTypeIsSharedAcrossModuleFactoryInstances() { + // every per-module wrapper used to keep its own caches, so the same class requested through + // two of them came back as two instances + ModuleSignature module = JavaModuleIdentifierFactory.getModuleSignature("myModule"); + ModuleJavaClassType viaWrapper = + new JavaModuleIdentifierFactory().forModule(module).getClassType("some.Klass"); + ModuleJavaClassType viaBaseFactory = + new JavaModuleIdentifierFactory().getClassType("Klass", "some", module); + assertSame(viaWrapper, viaBaseFactory); + + ModuleJavaClassType viaOtherWrapper = + new JavaModuleIdentifierFactory() + .forModule("otherModule") + .getClassType("myModule/some.Klass"); + assertSame(viaWrapper, viaOtherWrapper); + } + + @Test + public void moduleClassTypeIsNotConflatedWithPlainClassType() { + ClassType plain = new JavaIdentifierFactory().getClassType("some.Klass"); + ModuleJavaClassType moduleScoped = new JavaModuleIdentifierFactory().getClassType("some.Klass"); + assertNotSame(plain, moduleScoped); + assertNotEquals(plain, moduleScoped); + } + + @Test + public void moduleSignatureIsHashConsed() { + assertSame( + JavaModuleIdentifierFactory.getModuleSignature("myModule"), + JavaModuleIdentifierFactory.getModuleSignature("myModule")); + assertSame(ModuleSignature.UNNAMED_MODULE, JavaModuleIdentifierFactory.getModuleSignature("")); + } } diff --git a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/ClassVisitor.java b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/ClassVisitor.java index 5ec77918f32..ef418ccdacf 100644 --- a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/ClassVisitor.java +++ b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/ClassVisitor.java @@ -64,10 +64,10 @@ public abstract class ClassVisitor extends JimpleBaseVisitor { public ClassVisitor( @NonNull Path path, @NonNull List bodyInterceptors, @NonNull View view) { this.path = path; - util = new JimpleConverterUtil(path); this.bodyInterceptors = bodyInterceptors; this.view = view; this.identifierFactory = view.getIdentifierFactory(); + util = new JimpleConverterUtil(path, identifierFactory); } protected abstract MethodVisitor createMethodVisitor(); diff --git a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleConverterUtil.java b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleConverterUtil.java index a32173ffc8e..4bcb0450137 100644 --- a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleConverterUtil.java +++ b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/JimpleConverterUtil.java @@ -37,7 +37,6 @@ import sootup.core.signatures.PackageName; import sootup.core.types.ClassType; import sootup.core.types.Type; -import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.types.JavaClassType; import sootup.jimple.JimpleLexer; import sootup.jimple.JimpleParser; @@ -49,12 +48,13 @@ */ public class JimpleConverterUtil { - private final IdentifierFactory identifierFactory = JavaIdentifierFactory.getInstance(); + @NonNull private final IdentifierFactory identifierFactory; private final Map imports = new HashMap<>(); @NonNull private final Path fileUri; - public JimpleConverterUtil(@NonNull Path file) { + public JimpleConverterUtil(@NonNull Path file, @NonNull IdentifierFactory identifierFactory) { this.fileUri = file; + this.identifierFactory = identifierFactory; } public IdentifierFactory getIdentifierFactory() { diff --git a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/StmtVisitor.java b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/StmtVisitor.java index 39752fa3a94..43705289e5b 100644 --- a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/StmtVisitor.java +++ b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/StmtVisitor.java @@ -125,7 +125,7 @@ public Stmt visitStmt(JimpleParser.StmtContext ctx) { IdentityRef ref; final JimpleParser.Identity_refContext identityRefCtx = assignments.identity_ref(); if (identityRefCtx.caught != null) { - ref = JavaJimple.newCaughtExceptionRef(); + ref = JavaJimple.newCaughtExceptionRef(state.getIdentifierFactory()); } else { final String type = assignments.identity_ref().type().getText(); if (identityRefCtx.parameter_idx != null) { diff --git a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/ValueVisitor.java b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/ValueVisitor.java index c5542795cc7..78b04db010d 100644 --- a/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/ValueVisitor.java +++ b/sootup.jimple.frontend/src/main/java/sootup/jimple/frontend/ValueVisitor.java @@ -41,7 +41,6 @@ import sootup.core.signatures.SootClassMemberSignature; import sootup.core.signatures.SootClassMemberSubSignature; import sootup.core.types.*; -import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.language.JavaJimple; import sootup.jimple.JimpleBaseVisitor; import sootup.jimple.JimpleParser; @@ -75,7 +74,7 @@ public Value visitValue(JimpleParser.ValueContext ctx) { } Immediate dim = visitImmediate(ctx.array_descriptor().immediate()); - return JavaJimple.newNewArrayExpr(type, dim, JavaIdentifierFactory.getInstance()); + return JavaJimple.newNewArrayExpr(type, dim, state.getIdentifierFactory()); } else if (ctx.NEWMULTIARRAY() != null && ctx.immediate() != null) { final Type type = state.getUtil().getType(ctx.multiarray_type.getText()); if (!(type instanceof ReferenceType || type instanceof PrimitiveType)) { @@ -222,10 +221,10 @@ public Constant visitConstant(JimpleParser.ConstantContext ctx) { return DoubleConstant.getInstance(Double.parseDouble(floatStr)); } else if (ctx.CLASS() != null) { final String text = JimpleUtils.unescape(ctx.STRING_CONSTANT().getText()); - return JavaJimple.newClassConstant(text); + return JavaJimple.newClassConstant(text, state.getIdentifierFactory()); } else if (ctx.STRING_CONSTANT() != null) { final String text = JimpleUtils.unescape(ctx.STRING_CONSTANT().getText()); - return JavaJimple.newStringConstant(text); + return JavaJimple.newStringConstant(text, state.getIdentifierFactory()); } else if (ctx.BOOL_CONSTANT() != null) { final char firstChar = ctx.BOOL_CONSTANT().getText().charAt(0); return BooleanConstant.getInstance(firstChar == 't' || firstChar == 'T'); @@ -242,13 +241,15 @@ public Constant visitConstant(JimpleParser.ConstantContext ctx) { : state.getUtil().getFieldSignature(methodhandleContext.field_signature()); return JavaJimple.newMethodHandle( referenceSignature, - MethodHandle.Kind.getKind(kindName.substring(1, kindName.length() - 1))); + MethodHandle.Kind.getKind(kindName.substring(1, kindName.length() - 1)), + state.getIdentifierFactory()); } else if (ctx.methodtype != null && ctx.method_subsignature() != null) { final JimpleParser.Type_listContext typelist = ctx.method_subsignature().type_list(); final List typeList = state.getUtil().getTypeList(typelist); return JavaJimple.newMethodType( typeList, - state.getIdentifierFactory().getType(ctx.method_subsignature().type().getText())); + state.getIdentifierFactory().getType(ctx.method_subsignature().type().getText()), + state.getIdentifierFactory()); } throw new ResolveException( "Unknown Constant.", state.getPath(), JimpleConverterUtil.buildPositionFromCtx(ctx)); diff --git a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleAnalysisInputLocationTest.java b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleAnalysisInputLocationTest.java index 9441d94ff40..1c4b80b3ef1 100644 --- a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleAnalysisInputLocationTest.java +++ b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleAnalysisInputLocationTest.java @@ -14,6 +14,7 @@ import sootup.core.signatures.PackageName; import sootup.core.types.*; import sootup.interceptors.CopyPropagator; +import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.JavaSootClass; import sootup.java.core.views.JavaEagerView; import sootup.java.core.views.JavaView; @@ -38,7 +39,7 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName(""); + return new JavaIdentifierFactory().getPackageName(""); } }; @@ -57,7 +58,7 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName("jimple"); + return new JavaIdentifierFactory().getPackageName("jimple"); } }; @@ -76,7 +77,7 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName("jimple"); + return new JavaIdentifierFactory().getPackageName("jimple"); } }; diff --git a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleConverterTest.java b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleConverterTest.java index 1e0b955d4e7..e206817a23a 100644 --- a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleConverterTest.java +++ b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleConverterTest.java @@ -16,11 +16,11 @@ import sootup.core.jimple.JimpleUtils; import sootup.core.jimple.common.Trap; import sootup.core.model.*; -import sootup.core.signatures.MethodSubSignature; import sootup.core.types.PrimitiveType; import sootup.core.types.VoidType; import sootup.core.util.StringTools; import sootup.core.util.printer.BriefStmtPrinter; +import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.JavaSootClass; import sootup.java.core.OverridingJavaClassSource; import sootup.java.core.views.JavaView; @@ -283,7 +283,9 @@ public void testNonGreeedyCommentEverywhere() { SootClass sc = parseJimpleClass(cs); assertTrue( sc.getMethod( - new MethodSubSignature("another", Collections.emptyList(), VoidType.getInstance())) + new JavaIdentifierFactory() + .getMethodSubSignature( + "another", VoidType.getInstance(), Collections.emptyList())) .isPresent()); } diff --git a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleVisitorTest.java b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleVisitorTest.java index 07c787f7f99..99c941159bd 100644 --- a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleVisitorTest.java +++ b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleVisitorTest.java @@ -25,7 +25,7 @@ public class JimpleVisitorTest { public void setUp() { EagerInputLocation loc = new EagerInputLocation(); JavaView view = new JavaView(loc); - JimpleConverterUtil util = new JimpleConverterUtil(Paths.get("")); + JimpleConverterUtil util = new JimpleConverterUtil(Paths.get(""), view.getIdentifierFactory()); ClassType clazz = view.getIdentifierFactory().getClassType("TestClass"); state = diff --git a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/LazyJimpleMethodSourceTest.java b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/LazyJimpleMethodSourceTest.java index 59f4af8e8ce..bd095ae118b 100644 --- a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/LazyJimpleMethodSourceTest.java +++ b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/LazyJimpleMethodSourceTest.java @@ -26,7 +26,7 @@ private LazyJimpleMethodSource createLazySource(String code) { EagerInputLocation loc = new EagerInputLocation(); View view = new JavaView(loc); - JimpleConverterUtil util = new JimpleConverterUtil(Paths.get("")); + JimpleConverterUtil util = new JimpleConverterUtil(Paths.get(""), view.getIdentifierFactory()); ClassType clazz = view.getIdentifierFactory().getClassType("Test"); // Mocking the signature and modifiers diff --git a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/javatestsuite/JimpleTestSuiteBase.java b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/javatestsuite/JimpleTestSuiteBase.java index 4fc07ba7230..d5e98238ddb 100644 --- a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/javatestsuite/JimpleTestSuiteBase.java +++ b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/javatestsuite/JimpleTestSuiteBase.java @@ -24,7 +24,7 @@ public abstract class JimpleTestSuiteBase { static final String baseDir = "src/test/java/resources/jimple/"; - protected JavaIdentifierFactory identifierFactory = JavaIdentifierFactory.getInstance(); + protected JavaIdentifierFactory identifierFactory = new JavaIdentifierFactory(); private JavaView view; @BeforeEach diff --git a/sootup.qilin/src/main/java/qilin/core/ArtificialMethod.java b/sootup.qilin/src/main/java/qilin/core/ArtificialMethod.java index c00393d6e73..fdffd4b6c6c 100644 --- a/sootup.qilin/src/main/java/qilin/core/ArtificialMethod.java +++ b/sootup.qilin/src/main/java/qilin/core/ArtificialMethod.java @@ -21,7 +21,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import qilin.util.JavaTypes; import sootup.core.IdentifierFactory; import sootup.core.jimple.Jimple; import sootup.core.jimple.basic.*; @@ -43,7 +42,6 @@ import sootup.core.types.ClassType; import sootup.core.types.Type; import sootup.core.views.View; -import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.language.JavaJimple; public abstract class ArtificialMethod { @@ -107,9 +105,7 @@ protected Local getNew(ClassType type) { } protected Local getNewArray(ClassType type) { - Value newExpr = - JavaJimple.newNewArrayExpr( - type, IntConstant.getInstance(1), JavaIdentifierFactory.getInstance()); + Value newExpr = JavaJimple.newNewArrayExpr(type, IntConstant.getInstance(1), identifierFactory); Local local = getNextLocal(new ArrayType(type, 1)); addAssign(local, newExpr); return local; @@ -131,7 +127,7 @@ protected void addReturn(Immediate ret) { } protected JStaticFieldRef getStaticFieldRef(String className, String name) { - ClassType classType = JavaTypes.getClassType(className); + ClassType classType = identifierFactory.getClassType(className); SootClass sc = view.getClass(classType).get(); SootField field = sc.getField(name).get(); return Jimple.newStaticFieldRef(field.getSignature()); diff --git a/sootup.qilin/src/main/java/qilin/core/builder/FakeMainFactory.java b/sootup.qilin/src/main/java/qilin/core/builder/FakeMainFactory.java index cc0ec475c2b..4792bdee7e8 100644 --- a/sootup.qilin/src/main/java/qilin/core/builder/FakeMainFactory.java +++ b/sootup.qilin/src/main/java/qilin/core/builder/FakeMainFactory.java @@ -161,7 +161,8 @@ private void makeFakeMain(SootField currentThread) { addAssign(getArrayRef(strArray), mockStr); addInvoke(entry.getSignature().toString(), strArray); implicitCallEdgeCount++; - } else if (config.isSeedEntryPointClinits() || !JavaTypes.isStaticInitializer(entry)) { + } else if (config.isSeedEntryPointClinits() + || !JavaTypes.isStaticInitializer(view, entry)) { // when not eagerly seeding, we won't add a call directly for methods - they're // resolved on the fly instead. addInvoke(entry.getSignature().toString()); diff --git a/sootup.qilin/src/main/java/qilin/core/builder/MethodNodeFactory.java b/sootup.qilin/src/main/java/qilin/core/builder/MethodNodeFactory.java index cfec6dabb26..582c1b6c2de 100644 --- a/sootup.qilin/src/main/java/qilin/core/builder/MethodNodeFactory.java +++ b/sootup.qilin/src/main/java/qilin/core/builder/MethodNodeFactory.java @@ -63,7 +63,6 @@ import sootup.core.types.ClassType; import sootup.core.types.ReferenceType; import sootup.core.types.Type; -import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.JavaSootField; import sootup.java.core.language.JavaJimple; @@ -265,7 +264,7 @@ private VarNode caseNewMultiArrayExpr(JNewMultiArrayExpr nmae) { AllocNode prevAn = pag.makeAllocNode( JavaJimple.newNewArrayExpr( - type, nmae.getSize(pos), JavaIdentifierFactory.getInstance()), + type, nmae.getSize(pos), scene.getView().getIdentifierFactory()), type, method); VarNode prevVn = pag.makeLocalVarNode(prevAn.getNewExpr(), prevAn.getType(), method); @@ -286,7 +285,7 @@ private VarNode caseNewMultiArrayExpr(JNewMultiArrayExpr nmae) { } AllocNode an = pag.makeAllocNode( - JavaJimple.newNewArrayExpr(type, sizeVal, JavaIdentifierFactory.getInstance()), + JavaJimple.newNewArrayExpr(type, sizeVal, scene.getView().getIdentifierFactory()), type, method); VarNode vn = pag.makeLocalVarNode(an.getNewExpr(), an.getType(), method); diff --git a/sootup.qilin/src/main/java/qilin/core/pag/PAG.java b/sootup.qilin/src/main/java/qilin/core/pag/PAG.java index cf2aab6d7b2..62339ed87f0 100644 --- a/sootup.qilin/src/main/java/qilin/core/pag/PAG.java +++ b/sootup.qilin/src/main/java/qilin/core/pag/PAG.java @@ -383,7 +383,9 @@ public AllocNode makeAllocNode(Object newExpr, Type type, SootMethod m) { public AllocNode makeStringConstantNode(StringConstant sc) { StringConstant stringConstant = sc; if (!pta.getConfig().isStringConstants()) { - stringConstant = JavaJimple.newStringConstant(PointsToAnalysis.STRING_NODE); + stringConstant = + JavaJimple.newStringConstant( + PointsToAnalysis.STRING_NODE, pta.getView().getIdentifierFactory()); } AllocNode ret = valToAllocNode.get(stringConstant); if (ret == null) { diff --git a/sootup.qilin/src/main/java/qilin/core/reflection/TamiflexModel.java b/sootup.qilin/src/main/java/qilin/core/reflection/TamiflexModel.java index ec91d1b6ea7..37ec7487b27 100644 --- a/sootup.qilin/src/main/java/qilin/core/reflection/TamiflexModel.java +++ b/sootup.qilin/src/main/java/qilin/core/reflection/TamiflexModel.java @@ -55,7 +55,6 @@ import sootup.core.types.ArrayType; import sootup.core.types.ClassType; import sootup.core.types.ReferenceType; -import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.language.JavaJimple; /** @@ -82,7 +81,9 @@ Collection transformClassForName(InvokableStmt s) { Collection fornames = classForNames.get(s); for (String clazz : fornames) { // !TODO potential bug - ClassConstant cc = JavaJimple.newClassConstant(dot2slashStyle(clazz)); + ClassConstant cc = + JavaJimple.newClassConstant( + dot2slashStyle(clazz), ptaScene.getView().getIdentifierFactory()); if (s instanceof JAssignStmt) { LValue lvalue = ((JAssignStmt) s).getLeftOp(); ret.add(new JAssignStmt(lvalue, cc, StmtPositionInfo.getNoStmtPositionInfo())); @@ -296,7 +297,9 @@ protected Collection transformArrayNewInstance(InvokableStmt s) { ArrayType at = (ArrayType) ptaScene.getView().getIdentifierFactory().getType(arrayType); JNewArrayExpr newExpr = JavaJimple.newNewArrayExpr( - at.getElementType(), IntConstant.getInstance(1), JavaIdentifierFactory.getInstance()); + at.getElementType(), + IntConstant.getInstance(1), + ptaScene.getView().getIdentifierFactory()); if (s instanceof JAssignStmt) { LValue lvalue = ((JAssignStmt) s).getLeftOp(); ret.add(new JAssignStmt(lvalue, newExpr, StmtPositionInfo.getNoStmtPositionInfo())); diff --git a/sootup.qilin/src/main/java/qilin/core/solver/Solver.java b/sootup.qilin/src/main/java/qilin/core/solver/Solver.java index 97968cc2399..d13e6089740 100644 --- a/sootup.qilin/src/main/java/qilin/core/solver/Solver.java +++ b/sootup.qilin/src/main/java/qilin/core/solver/Solver.java @@ -50,7 +50,6 @@ import sootup.core.signatures.MethodSubSignature; import sootup.core.types.ClassType; import sootup.core.types.Type; -import sootup.java.core.JavaIdentifierFactory; public class Solver extends Propagator { private final TreeSet valNodeWorkList = new TreeSet<>(); @@ -255,7 +254,7 @@ private void handleImplicitCallToFinalizerRegister(AllocNode heap) { private boolean supportFinalize(AllocNode heap) { MethodSubSignature sigFinalize = - JavaIdentifierFactory.getInstance().parseMethodSubSignature("void finalize()"); + pta.getView().getIdentifierFactory().parseMethodSubSignature("void finalize()"); Type type = heap.getType(); if (type instanceof ClassType && type != JavaTypes.OBJECT) { ClassType refType = (ClassType) type; diff --git a/sootup.qilin/src/main/java/qilin/pta/toolkits/conch/Conch.java b/sootup.qilin/src/main/java/qilin/pta/toolkits/conch/Conch.java index d0c8be3b2d4..1a876f9e006 100644 --- a/sootup.qilin/src/main/java/qilin/pta/toolkits/conch/Conch.java +++ b/sootup.qilin/src/main/java/qilin/pta/toolkits/conch/Conch.java @@ -83,7 +83,7 @@ private SootMethod findInvokedConstructorOf(AllocNode heap) { SootMethod target = pta.getView().getMethod(iie.getMethodSignature()).get(); if (v1pts.size() == 1 && v1pts.toCIPointsToSet().contains(heap) - && JavaTypes.isConstructor(target)) { + && JavaTypes.isConstructor(pta.getView(), target)) { return target; } } @@ -218,7 +218,7 @@ private Trilean checkResult(Set res) { private Trilean isCommingFromParams(LocalVarNode from, SootMethod method, AllocNode heap) { Set ret = this.pfg.fetchReachableParamsOf(from); - if (JavaTypes.isConstructor(method)) { + if (JavaTypes.isConstructor(pta.getView(), method)) { return handleTransitiveConstructors(method, heap, ret); } else { return checkResult(ret); @@ -316,7 +316,7 @@ public void runClassifier() { ciHeaps.add(heap); } else { SootMethod mthd = heap.getMethod(); - if (JavaTypes.isStaticInitializer(mthd)) { + if (JavaTypes.isStaticInitializer(pta.getView(), mthd)) { ciHeaps.add(heap); } else { remainToSolve.add(heap); diff --git a/sootup.qilin/src/main/java/qilin/pta/toolkits/debloaterx/DebloaterX.java b/sootup.qilin/src/main/java/qilin/pta/toolkits/debloaterx/DebloaterX.java index 0ce4d34b4f9..9f8a37dad8b 100644 --- a/sootup.qilin/src/main/java/qilin/pta/toolkits/debloaterx/DebloaterX.java +++ b/sootup.qilin/src/main/java/qilin/pta/toolkits/debloaterx/DebloaterX.java @@ -111,7 +111,7 @@ public void run() { Map> m2o = new HashMap<>(); for (AllocNode heap : pag.getAllocNodes()) { SootMethod method = heap.getMethod(); - if (method == null || JavaTypes.isStaticInitializer(method)) { + if (method == null || JavaTypes.isStaticInitializer(pta.getView(), method)) { continue; } m2o.computeIfAbsent(method, k -> new HashSet<>()).add(heap); diff --git a/sootup.qilin/src/main/java/qilin/util/JavaTypes.java b/sootup.qilin/src/main/java/qilin/util/JavaTypes.java index 9c005399250..229f574113f 100644 --- a/sootup.qilin/src/main/java/qilin/util/JavaTypes.java +++ b/sootup.qilin/src/main/java/qilin/util/JavaTypes.java @@ -25,41 +25,38 @@ import sootup.core.types.PrimitiveType; import sootup.core.types.Type; import sootup.core.views.View; -import sootup.java.core.JavaIdentifierFactory; +import sootup.java.core.types.JavaClassType; /** Well-known JDK {@link ClassType}s, plus type-hierarchy and method-kind checks. */ public final class JavaTypes { private JavaTypes() {} - public static ClassType getClassType(String fullyQualifiedClassName) { - return JavaIdentifierFactory.getInstance().getClassType(fullyQualifiedClassName); - } - - public static final ClassType OBJECT = getClassType("java.lang.Object"); - public static final ClassType STRING = getClassType("java.lang.String"); - public static final ClassType THROWABLE = getClassType("java.lang.Throwable"); - public static final ClassType THREAD = getClassType("java.lang.Thread"); - public static final ClassType THREAD_GROUP = getClassType("java.lang.ThreadGroup"); - public static final ClassType CLASS = getClassType("java.lang.Class"); - public static final ClassType EXCEPTION = getClassType("java.lang.Exception"); - public static final ClassType RUNNABLE = getClassType("java.lang.Runnable"); - public static final ClassType STRING_BUFFER = getClassType("java.lang.StringBuffer"); - public static final ClassType STRING_BUILDER = getClassType("java.lang.StringBuilder"); - public static final ClassType PROTECTION_DOMAIN = getClassType("java.security.ProtectionDomain"); + public static final ClassType OBJECT = JavaClassType.of("java.lang.Object"); + public static final ClassType STRING = JavaClassType.of("java.lang.String"); + public static final ClassType THROWABLE = JavaClassType.of("java.lang.Throwable"); + public static final ClassType THREAD = JavaClassType.of("java.lang.Thread"); + public static final ClassType THREAD_GROUP = JavaClassType.of("java.lang.ThreadGroup"); + public static final ClassType CLASS = JavaClassType.of("java.lang.Class"); + public static final ClassType EXCEPTION = JavaClassType.of("java.lang.Exception"); + public static final ClassType RUNNABLE = JavaClassType.of("java.lang.Runnable"); + public static final ClassType STRING_BUFFER = JavaClassType.of("java.lang.StringBuffer"); + public static final ClassType STRING_BUILDER = JavaClassType.of("java.lang.StringBuilder"); + public static final ClassType PROTECTION_DOMAIN = + JavaClassType.of("java.security.ProtectionDomain"); public static final ClassType PRIVILEGED_ACTION_EXCEPTION = - getClassType("java.security.PrivilegedActionException"); - public static final ClassType APP_CLASS_LOADER = getClassType("sun.misc.Launcher$AppClassLoader"); - public static final ClassType SYSTEM = getClassType("java.lang.System"); - public static final ClassType UNIX_FILE_SYSTEM = getClassType("java.io.UnixFileSystem"); - public static final ClassType COLLECTION = getClassType("java.util.Collection"); + JavaClassType.of("java.security.PrivilegedActionException"); + public static final ClassType APP_CLASS_LOADER = + JavaClassType.of("sun.misc.Launcher$AppClassLoader"); + public static final ClassType SYSTEM = JavaClassType.of("java.lang.System"); + public static final ClassType UNIX_FILE_SYSTEM = JavaClassType.of("java.io.UnixFileSystem"); + public static final ClassType COLLECTION = JavaClassType.of("java.util.Collection"); - public static boolean isStaticInitializer(SootMethod method) { - return JavaIdentifierFactory.getInstance() - .isStaticInitializerSubSignature(method.getSubSignature()); + public static boolean isStaticInitializer(View view, SootMethod method) { + return view.getIdentifierFactory().isStaticInitializerSubSignature(method.getSubSignature()); } - public static boolean isConstructor(SootMethod method) { - return method.isConstructor(JavaIdentifierFactory.getInstance()); + public static boolean isConstructor(View view, SootMethod method) { + return method.isConstructor(view.getIdentifierFactory()); } public static boolean isThrowable(View view, Type type) { diff --git a/sootup.spark/src/main/java/sootup/spark/NodeFactory.java b/sootup.spark/src/main/java/sootup/spark/NodeFactory.java index 5f37255e8ca..d2b0b1dc3e3 100644 --- a/sootup.spark/src/main/java/sootup/spark/NodeFactory.java +++ b/sootup.spark/src/main/java/sootup/spark/NodeFactory.java @@ -27,6 +27,7 @@ import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.experimental.FieldDefaults; +import sootup.core.IdentifierFactory; import sootup.core.jimple.common.Value; import sootup.core.signatures.MethodSignature; import sootup.spark.node.Node; @@ -41,6 +42,7 @@ public class NodeFactory { @NonNull SparkOptions sparkOptions; + @NonNull IdentifierFactory identifierFactory; /** * creates a PAG node for a given Jimple value @@ -51,7 +53,7 @@ public class NodeFactory { @NonNull public Optional createNode(@NonNull Value value, MethodSignature containingMethodSig) { ValueToNodeConversionVisitor visitor = - new ValueToNodeConversionVisitor(containingMethodSig, sparkOptions); + new ValueToNodeConversionVisitor(containingMethodSig, sparkOptions, identifierFactory); value.accept(visitor); return visitor.getResult(); } diff --git a/sootup.spark/src/main/java/sootup/spark/PointsToAnalysis.java b/sootup.spark/src/main/java/sootup/spark/PointsToAnalysis.java index 5b747b0c640..bfd22ae0d9b 100644 --- a/sootup.spark/src/main/java/sootup/spark/PointsToAnalysis.java +++ b/sootup.spark/src/main/java/sootup/spark/PointsToAnalysis.java @@ -73,7 +73,8 @@ public class PointsToAnalysis { private PointsToAnalysis(Solver solver) { this.solver = solver; - this.nodeFactory = new NodeFactory(solver.getSparkOptions()); + this.nodeFactory = + new NodeFactory(solver.getSparkOptions(), solver.getView().getIdentifierFactory()); } /** diff --git a/sootup.spark/src/main/java/sootup/spark/Solver.java b/sootup.spark/src/main/java/sootup/spark/Solver.java index 663ebca07ee..90d8f05b4eb 100644 --- a/sootup.spark/src/main/java/sootup/spark/Solver.java +++ b/sootup.spark/src/main/java/sootup/spark/Solver.java @@ -122,7 +122,7 @@ private void buildMethodPAG(SootMethod method) { .PAG(pag) .callGraph(callGraph) .view(view) - .nodeFactory(new NodeFactory(sparkOptions)) + .nodeFactory(new NodeFactory(sparkOptions, view.getIdentifierFactory())) .build(); method.getBody().getStmts().forEach(stmt -> stmt.accept(stmtVisitor)); } @@ -135,7 +135,7 @@ private void buildMethodPAG(SootMethod method) { */ private void solveOnTheFly() { MutableCallGraph cg = (MutableCallGraph) callGraph; - NodeFactory nodeFactory = new NodeFactory(sparkOptions); + NodeFactory nodeFactory = new NodeFactory(sparkOptions, view.getIdentifierFactory()); Deque worklist = new ArrayDeque<>(entryPoints); Set processed = new HashSet<>(); Map visitors = new HashMap<>(); diff --git a/sootup.spark/src/main/java/sootup/spark/node/ValueToNodeConversionVisitor.java b/sootup.spark/src/main/java/sootup/spark/node/ValueToNodeConversionVisitor.java index c96846804ef..50f02375619 100644 --- a/sootup.spark/src/main/java/sootup/spark/node/ValueToNodeConversionVisitor.java +++ b/sootup.spark/src/main/java/sootup/spark/node/ValueToNodeConversionVisitor.java @@ -29,6 +29,7 @@ import lombok.experimental.FieldDefaults; import lombok.extern.slf4j.Slf4j; import lombok.val; +import sootup.core.IdentifierFactory; import sootup.core.jimple.common.Local; import sootup.core.jimple.common.Value; import sootup.core.jimple.common.constant.BooleanConstant; @@ -72,13 +73,10 @@ import sootup.core.jimple.common.expr.JXorExpr; import sootup.core.jimple.common.ref.*; import sootup.core.jimple.visitor.AbstractValueVisitor; -import sootup.core.signatures.FieldSignature; import sootup.core.signatures.MethodSignature; -import sootup.core.signatures.PackageName; import sootup.core.types.ArrayType; import sootup.core.types.ClassType; import sootup.core.types.VoidType; -import sootup.java.core.types.JavaClassType; import sootup.spark.Engine; import sootup.spark.SparkOptions; @@ -95,21 +93,25 @@ @FieldDefaults(level = AccessLevel.PRIVATE) public class ValueToNodeConversionVisitor extends AbstractValueVisitor { - private static final MethodSignature GLOBAL_SCOPE = - new MethodSignature( - new JavaClassType("GLOBAL", new PackageName("sootup.global")), - "GLOBAL_SCOPE", - Collections.emptyList(), - VoidType.getInstance()); - final MethodSignature containingMethodSig; final SparkOptions sparkOptions; + final IdentifierFactory identifierFactory; + final MethodSignature globalScope; Node node; public ValueToNodeConversionVisitor( - MethodSignature containingMethodSig, SparkOptions sparkOptions) { + MethodSignature containingMethodSig, + SparkOptions sparkOptions, + @NonNull IdentifierFactory identifierFactory) { this.containingMethodSig = containingMethodSig; this.sparkOptions = sparkOptions; + this.identifierFactory = identifierFactory; + this.globalScope = + identifierFactory.getMethodSignature( + identifierFactory.getClassType("GLOBAL", "sootup.global"), + "GLOBAL_SCOPE", + VoidType.getInstance(), + Collections.emptyList()); } /** @@ -335,7 +337,7 @@ public void caseStaticFieldRef(@NonNull JStaticFieldRef ref) { @Override public void caseInstanceFieldRef(@NonNull JInstanceFieldRef ref) { val fieldBasedScope = sparkOptions.isIgnoreBaseObjects(); - val baseMethodSig = fieldBasedScope ? GLOBAL_SCOPE : containingMethodSig; + val baseMethodSig = fieldBasedScope ? globalScope : containingMethodSig; val baseName = fieldBasedScope ? ref.getBase().getType().toString() : ref.getBase().getName(); val base = VariableNode.builder() @@ -359,9 +361,10 @@ public void caseArrayRef(@NonNull JArrayRef ref) { val baseType = arrayType.getBaseType(); if (baseType instanceof ClassType declaringClassType) { val field = - new FieldSignature(declaringClassType, String.valueOf(ref.getIndex()), ref.getType()); + identifierFactory.getFieldSignature( + String.valueOf(ref.getIndex()), declaringClassType, ref.getType()); val fieldBasedScope = sparkOptions.isIgnoreBaseObjects(); - val baseMethodSig = fieldBasedScope ? GLOBAL_SCOPE : containingMethodSig; + val baseMethodSig = fieldBasedScope ? globalScope : containingMethodSig; val baseName = fieldBasedScope ? ref.getBase().getType().toString() : ref.getBase().getName(); val base = diff --git a/sootup.spark/src/test/java/sootup/spark/test/AllocationNodeTest.java b/sootup.spark/src/test/java/sootup/spark/test/AllocationNodeTest.java index 966b989c732..f8333d9e708 100644 --- a/sootup.spark/src/test/java/sootup/spark/test/AllocationNodeTest.java +++ b/sootup.spark/src/test/java/sootup/spark/test/AllocationNodeTest.java @@ -14,10 +14,10 @@ class AllocationNodeTest { ClassType aType = SparkTestUtil.simpleType("A"); ClassType bType = SparkTestUtil.simpleType("B"); MethodSignature methodSig = - JavaIdentifierFactory.getInstance() + new JavaIdentifierFactory() .getMethodSignature(aType, "test", "void", Collections.emptyList()); MethodSignature otherMethodSig = - JavaIdentifierFactory.getInstance() + new JavaIdentifierFactory() .getMethodSignature(aType, "other", "void", Collections.emptyList()); @Test diff --git a/sootup.spark/src/test/java/sootup/spark/test/SparkTestUtil.java b/sootup.spark/src/test/java/sootup/spark/test/SparkTestUtil.java index 16f5077f622..ea514a85d97 100644 --- a/sootup.spark/src/test/java/sootup/spark/test/SparkTestUtil.java +++ b/sootup.spark/src/test/java/sootup/spark/test/SparkTestUtil.java @@ -12,7 +12,6 @@ import sootup.core.types.VoidType; import sootup.java.bytecode.frontend.inputlocation.JavaClassPathAnalysisInputLocation; import sootup.java.core.JavaIdentifierFactory; -import sootup.java.core.types.JavaClassType; import sootup.java.core.views.JavaView; import sootup.spark.PAGEdge; import sootup.spark.PAGVisualizer; @@ -26,15 +25,15 @@ @UtilityClass public class SparkTestUtil { - public static final JavaIdentifierFactory idFactory = JavaIdentifierFactory.getInstance(); + public static final JavaIdentifierFactory idFactory = new JavaIdentifierFactory(); public static final JavaView view = new JavaView(new JavaClassPathAnalysisInputLocation("src/test/resources/pta/binary")); public static final MethodSignature GLOBAL_SCOPE = - new MethodSignature( - new JavaClassType("GLOBAL", new PackageName("sootup.global")), + idFactory.getMethodSignature( + idFactory.getClassType("GLOBAL", "sootup.global"), "GLOBAL_SCOPE", - Collections.emptyList(), - VoidType.getInstance()); + VoidType.getInstance(), + Collections.emptyList()); public static ClassType simpleType(String name) { return new ClassType() { @@ -50,7 +49,7 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName(""); + return new JavaIdentifierFactory().getPackageName(""); } }; } diff --git a/sootup.spark/src/test/java/sootup/spark/test/StmtToEdgeConversionTest.java b/sootup.spark/src/test/java/sootup/spark/test/StmtToEdgeConversionTest.java index 16241de6d36..e3c54bb6b16 100644 --- a/sootup.spark/src/test/java/sootup/spark/test/StmtToEdgeConversionTest.java +++ b/sootup.spark/src/test/java/sootup/spark/test/StmtToEdgeConversionTest.java @@ -31,13 +31,12 @@ public class StmtToEdgeConversionTest { + JavaIdentifierFactory identifierFactory = new JavaIdentifierFactory(); ClassType aType = SparkTestUtil.simpleType("A"); ClassType bType = SparkTestUtil.simpleType("B"); - FieldSignature fieldSig = - JavaIdentifierFactory.getInstance().getFieldSignature("f", aType, bType); + FieldSignature fieldSig = identifierFactory.getFieldSignature("f", aType, bType); MethodSignature methodSig = - JavaIdentifierFactory.getInstance() - .getMethodSignature(aType, "test", "void", Collections.emptyList()); + identifierFactory.getMethodSignature(aType, "test", "void", Collections.emptyList()); @BeforeEach public void reset() { @@ -117,7 +116,7 @@ private Edge doAssignment(Value right, LValue left, PAG methodPAG, PAGEdge.EdgeT MethodPAGStmtVisitor stmtVisitor = MethodPAGStmtVisitor.builder() .PAG(methodPAG) - .nodeFactory(new NodeFactory(SparkOptions.defaultOptions())) + .nodeFactory(new NodeFactory(SparkOptions.defaultOptions(), identifierFactory)) .methodSignature(methodSig) .build(); assignStmt.accept(stmtVisitor); diff --git a/sootup.spark/src/test/java/sootup/spark/test/ValueToNodeConversionTest.java b/sootup.spark/src/test/java/sootup/spark/test/ValueToNodeConversionTest.java index 910f0804aff..64c15f6be6a 100644 --- a/sootup.spark/src/test/java/sootup/spark/test/ValueToNodeConversionTest.java +++ b/sootup.spark/src/test/java/sootup/spark/test/ValueToNodeConversionTest.java @@ -38,14 +38,13 @@ class ValueToNodeConversionTest { - NodeFactory nodeFactory = new NodeFactory(SparkOptions.defaultOptions()); + JavaIdentifierFactory identifierFactory = new JavaIdentifierFactory(); + NodeFactory nodeFactory = new NodeFactory(SparkOptions.defaultOptions(), identifierFactory); ClassType aType = SparkTestUtil.simpleType("A"); ClassType bType = SparkTestUtil.simpleType("B"); - FieldSignature fieldSig = - JavaIdentifierFactory.getInstance().getFieldSignature("f", aType, bType); + FieldSignature fieldSig = identifierFactory.getFieldSignature("f", aType, bType); MethodSignature methodSig = - JavaIdentifierFactory.getInstance() - .getMethodSignature(aType, "test", "void", Collections.emptyList()); + identifierFactory.getMethodSignature(aType, "test", "void", Collections.emptyList()); @BeforeEach public void reset() { @@ -137,8 +136,7 @@ void testCastExprToNodeConversion() { @Test void testNewArrayExprToNodeConversion() { val newArrayExpr = - JavaJimple.newNewArrayExpr( - aType, IntConstant.getInstance(1), JavaIdentifierFactory.getInstance()); + JavaJimple.newNewArrayExpr(aType, IntConstant.getInstance(1), new JavaIdentifierFactory()); val node = nodeFactory.createNode(newArrayExpr, methodSig); assertTrue(node.isEmpty()); } @@ -180,7 +178,7 @@ void testBooleanConstantToNodeConversion() { @Test void testClassConstantToNodeConversion() { - val classConstant = JavaJimple.newClassConstant(aType.toString()); + val classConstant = JavaJimple.newClassConstant(aType.toString(), identifierFactory); val node = nodeFactory.createNode(classConstant, methodSig); assertTrue(node.isEmpty()); } @@ -427,28 +425,32 @@ void testXorExprToNodeConversion() { @Test void testCaughtExceptionRefToNodeConversion() { - val caughtExceptionRef = JavaJimple.newCaughtExceptionRef(); + val caughtExceptionRef = JavaJimple.newCaughtExceptionRef(identifierFactory); val node = nodeFactory.createNode(caughtExceptionRef, methodSig); assertTrue(node.isEmpty()); } @Test void testEnumConstantToNodeConversion() { - val enumConstant = JavaJimple.newEnumConstant("VALUE", "MyEnum"); + val enumConstant = JavaJimple.newEnumConstant("VALUE", "MyEnum", identifierFactory); val node = nodeFactory.createNode(enumConstant, methodSig); assertTrue(node.isEmpty()); } @Test void testMethodHandleToNodeConversion() { - val methodHandle = JavaJimple.newMethodHandle(methodSig, MethodHandle.Kind.REF_INVOKE_VIRTUAL); + val methodHandle = + JavaJimple.newMethodHandle( + methodSig, MethodHandle.Kind.REF_INVOKE_VIRTUAL, identifierFactory); val node = nodeFactory.createNode(methodHandle, methodSig); assertTrue(node.isEmpty()); } @Test void testMethodTypeToNodeConversion() { - val methodType = JavaJimple.newMethodType(Collections.emptyList(), VoidType.getInstance()); + val methodType = + JavaJimple.newMethodType( + Collections.emptyList(), VoidType.getInstance(), identifierFactory); val node = nodeFactory.createNode(methodType, methodSig); assertTrue(node.isEmpty()); } @@ -468,7 +470,7 @@ void testPhiExprToNodeConversion() { @Test void testStringConstantToNodeConversion() { - val stringConstant = JavaJimple.newStringConstant("hello"); + val stringConstant = JavaJimple.newStringConstant("hello", identifierFactory); val nodeOpt = nodeFactory.createNode(stringConstant, methodSig); assertTrue(nodeOpt.isPresent()); val node = nodeOpt.get(); diff --git a/sootup.spark/src/test/java/sootup/spark/test/options/IgnoreBaseObjectsTest.java b/sootup.spark/src/test/java/sootup/spark/test/options/IgnoreBaseObjectsTest.java index 131c3dd06e7..9e2b5b360b2 100644 --- a/sootup.spark/src/test/java/sootup/spark/test/options/IgnoreBaseObjectsTest.java +++ b/sootup.spark/src/test/java/sootup/spark/test/options/IgnoreBaseObjectsTest.java @@ -8,6 +8,7 @@ import sootup.core.signatures.FieldSignature; import sootup.core.signatures.MethodSignature; import sootup.core.types.ClassType; +import sootup.java.core.JavaIdentifierFactory; import sootup.spark.Engine; import sootup.spark.PAGEdge; import sootup.spark.SparkOptions; @@ -24,7 +25,8 @@ public class IgnoreBaseObjectsTest { ClassType containerType = SparkTestUtil.idFactory.getClassType("IgnoreBaseObjects$Container"); ClassType valueType = SparkTestUtil.idFactory.getClassType("IgnoreBaseObjects$Value"); - FieldSignature valueField = new FieldSignature(containerType, "value", valueType); + FieldSignature valueField = + new JavaIdentifierFactory().getFieldSignature("value", containerType, valueType); @BeforeEach public void reset() { diff --git a/sootup.tests/src/test/java/sootup/tests/MutatingSootClassTest.java b/sootup.tests/src/test/java/sootup/tests/MutatingSootClassTest.java index 8169f99cafa..f664dd66664 100644 --- a/sootup.tests/src/test/java/sootup/tests/MutatingSootClassTest.java +++ b/sootup.tests/src/test/java/sootup/tests/MutatingSootClassTest.java @@ -22,17 +22,14 @@ import sootup.core.model.SootField; import sootup.core.model.SootMethod; import sootup.core.model.SourceType; -import sootup.core.signatures.FieldSignature; -import sootup.core.signatures.FieldSubSignature; import sootup.core.signatures.MethodSignature; -import sootup.core.signatures.MethodSubSignature; -import sootup.core.signatures.PackageName; import sootup.core.types.ArrayType; import sootup.core.types.PrimitiveType.BooleanType; import sootup.core.types.PrimitiveType.IntType; import sootup.core.types.VoidType; import sootup.java.bytecode.frontend.inputlocation.PathBasedAnalysisInputLocation; import sootup.java.core.*; +import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.JavaSootField.JavaSootFieldBuilder; import sootup.java.core.language.JavaJimple; import sootup.java.core.types.JavaClassType; @@ -103,8 +100,10 @@ public void test() { .withModifier(Collections.singletonList(FieldModifier.PUBLIC)) .withPosition(NoPositionInformation.getInstance()) .withSignature( - new FieldSignature( - sootClass.getType(), new FieldSubSignature("g", IntType.getInt()))) + new JavaIdentifierFactory() + .getFieldSignature( + sootClass.getType(), + new JavaIdentifierFactory().getFieldSubSignature("g", IntType.getInt()))) .build(); OverridingJavaClassSource newClassSource = @@ -119,12 +118,13 @@ public void test() { SootMethod methodNew = newClass .getMethod( - new MethodSubSignature( - "main", - Collections.singletonList( - new ArrayType( - new JavaClassType("String", new PackageName("java.lang")), 1)), - VoidType.getInstance())) + new JavaIdentifierFactory() + .getMethodSubSignature( + "main", + VoidType.getInstance(), + Collections.singletonList( + new ArrayType( + identifierFactory.getClassType("String", "java.lang"), 1)))) .orElse(null); assertNotNull(methodNew); assertEquals(newLocal, methodNew.getBody().getLocals().stream().findFirst().orElse(null)); @@ -133,7 +133,9 @@ public void test() { SootMethod constructorNew = sootClass .getMethod( - new MethodSubSignature("", Collections.emptyList(), VoidType.getInstance())) + new JavaIdentifierFactory() + .getMethodSubSignature( + "", VoidType.getInstance(), Collections.emptyList())) .orElse(null); assertNotNull(constructorNew); assertFalse(constructorNew.getBody().getLocals().isEmpty()); @@ -141,12 +143,13 @@ public void test() { SootMethod oldMethod = sootClass .getMethod( - new MethodSubSignature( - "main", - Collections.singletonList( - new ArrayType( - new JavaClassType("String", new PackageName("java.lang")), 1)), - VoidType.getInstance())) + new JavaIdentifierFactory() + .getMethodSubSignature( + "main", + VoidType.getInstance(), + Collections.singletonList( + new ArrayType( + identifierFactory.getClassType("String", "java.lang"), 1)))) .orElse(null); assertNotNull(oldMethod); assertTrue(oldMethod.getBody().getLocals().stream().noneMatch(local -> local.equals(newLocal))); @@ -155,9 +158,11 @@ public void test() { newField .withModifiers(Collections.singletonList(FieldModifier.PRIVATE)) .withSignature( - new FieldSignature( - newField.getDeclaringClassType(), - new FieldSubSignature(newField.getName(), BooleanType.getInstance()))); + new JavaIdentifierFactory() + .getFieldSignature( + newField.getDeclaringClassType(), + new JavaIdentifierFactory() + .getFieldSubSignature(newField.getName(), BooleanType.getInstance()))); OverridingJavaClassSource newerClassSource = overridingJavaClassSource .withReplacedField(newField, replacedField) diff --git a/sootup.tests/src/test/java/sootup/tests/ReplaceUseExprVisitorTest.java b/sootup.tests/src/test/java/sootup/tests/ReplaceUseExprVisitorTest.java index 4aeb13038f1..aa5b3a3b6ca 100644 --- a/sootup.tests/src/test/java/sootup/tests/ReplaceUseExprVisitorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/ReplaceUseExprVisitorTest.java @@ -30,7 +30,7 @@ */ public class ReplaceUseExprVisitorTest { - JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory factory = new JavaIdentifierFactory(); JavaClassType intType = factory.getClassType("int"); JavaClassType testClass = factory.getClassType("TestClass"); JavaClassType voidType = factory.getClassType("void"); @@ -61,7 +61,8 @@ public class ReplaceUseExprVisitorTest { new MutableBasicBlockImpl(Arrays.asList(stmt3, stmt3), Collections.emptyMap()); MethodSignature methodeWithOutParas = - new MethodSignature(testClass, "invokeExpr", Collections.emptyList(), voidType); + new JavaIdentifierFactory() + .getMethodSignature(testClass, "invokeExpr", voidType, Collections.emptyList()); /** Test use replacing in case BinopExpr. JaddExpr is as an example. */ @Test @@ -111,7 +112,9 @@ public void testCaseInvokeExpr() { parameters.add(intType); parameters.add(intType); - MethodSignature method = new MethodSignature(testClass, "invokeExpr", parameters, voidType); + MethodSignature method = + new JavaIdentifierFactory() + .getMethodSignature(testClass, "invokeExpr", voidType, parameters); ReplaceUseExprVisitor visitor = new ReplaceUseExprVisitor(); visitor.init(arg1, newArg); @@ -155,7 +158,9 @@ public void testCaseInstanceInvokeExpr() { parameters.add(intType); parameters.add(intType); - MethodSignature method = new MethodSignature(testClass, "invokeExpr", parameters, voidType); + MethodSignature method = + new JavaIdentifierFactory() + .getMethodSignature(testClass, "invokeExpr", voidType, parameters); ReplaceUseExprVisitor visitor = new ReplaceUseExprVisitor(); visitor.init(arg1, newArg); diff --git a/sootup.tests/src/test/java/sootup/tests/ReplaceUseRefVisitorTest.java b/sootup.tests/src/test/java/sootup/tests/ReplaceUseRefVisitorTest.java index 83009695d2b..ee2d1250829 100644 --- a/sootup.tests/src/test/java/sootup/tests/ReplaceUseRefVisitorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/ReplaceUseRefVisitorTest.java @@ -20,7 +20,7 @@ * @author Zun Wang */ public class ReplaceUseRefVisitorTest { - JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory factory = new JavaIdentifierFactory(); JavaClassType intType = factory.getClassType("int"); JavaClassType arrayType = factory.getClassType("Array"); @@ -33,7 +33,8 @@ public class ReplaceUseRefVisitorTest { Local localIndex = JavaJimple.newLocal("index", intType); Local localNewIndex = JavaJimple.newLocal("newIndex", intType); - FieldSignature fieldSignature = new FieldSignature(arrayType, "field", intType); + FieldSignature fieldSignature = + new JavaIdentifierFactory().getFieldSignature("field", arrayType, intType); /** Test use replacing in case JArrayRef. */ @Test diff --git a/sootup.tests/src/test/java/sootup/tests/ReplaceUseStmtVisitorTest.java b/sootup.tests/src/test/java/sootup/tests/ReplaceUseStmtVisitorTest.java index c3f47a17049..dfcdcc6f958 100644 --- a/sootup.tests/src/test/java/sootup/tests/ReplaceUseStmtVisitorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/ReplaceUseStmtVisitorTest.java @@ -24,7 +24,7 @@ * @author Zun Wang */ public class ReplaceUseStmtVisitorTest { - JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory factory = new JavaIdentifierFactory(); JavaClassType intType = factory.getClassType("int"); JavaClassType testClass = factory.getClassType("TestClass"); JavaClassType voidType = factory.getClassType("void"); @@ -37,7 +37,8 @@ public class ReplaceUseStmtVisitorTest { Local base = JavaJimple.newLocal("base", testClass); MethodSignature methodeWithOutParas = - new MethodSignature(testClass, "invokeExpr", Collections.emptyList(), voidType); + new JavaIdentifierFactory() + .getMethodSignature(testClass, "invokeExpr", voidType, Collections.emptyList()); StmtPositionInfo noStmtPositionInfo = StmtPositionInfo.getNoStmtPositionInfo(); diff --git a/sootup.tests/src/test/java/sootup/tests/exceptions/StmtExceptionAnalyzerTest.java b/sootup.tests/src/test/java/sootup/tests/exceptions/StmtExceptionAnalyzerTest.java index ccdf5edfa4e..4016e961874 100644 --- a/sootup.tests/src/test/java/sootup/tests/exceptions/StmtExceptionAnalyzerTest.java +++ b/sootup.tests/src/test/java/sootup/tests/exceptions/StmtExceptionAnalyzerTest.java @@ -38,7 +38,7 @@ public class StmtExceptionAnalyzerTest { - JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); + JavaIdentifierFactory factory = new JavaIdentifierFactory(); ClassType clazzType = factory.getClassType("StmtExceptions"); String location = "src/test/resources/exceptions/"; final Path path = Paths.get(location + "StmtExceptions.class"); diff --git a/sootup.tests/src/test/java/sootup/tests/validator/CheckInitValidatorTest.java b/sootup.tests/src/test/java/sootup/tests/validator/CheckInitValidatorTest.java index e3753522f8d..3823bd11434 100644 --- a/sootup.tests/src/test/java/sootup/tests/validator/CheckInitValidatorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/validator/CheckInitValidatorTest.java @@ -15,6 +15,7 @@ import sootup.core.types.*; import sootup.core.validation.CheckInitValidator; import sootup.core.validation.ValidationException; +import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.JavaSootClass; import sootup.java.core.views.JavaView; import sootup.jimple.frontend.JimpleAnalysisInputLocation; @@ -44,7 +45,7 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName("jimple"); + return new JavaIdentifierFactory().getPackageName("jimple"); } }; diff --git a/sootup.tests/src/test/java/sootup/tests/validator/FieldModifiersValidatorTest.java b/sootup.tests/src/test/java/sootup/tests/validator/FieldModifiersValidatorTest.java index ee1d6d4190e..bf2cebefcde 100644 --- a/sootup.tests/src/test/java/sootup/tests/validator/FieldModifiersValidatorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/validator/FieldModifiersValidatorTest.java @@ -17,7 +17,6 @@ import sootup.core.model.*; import sootup.core.signatures.FieldSignature; import sootup.core.signatures.MethodSignature; -import sootup.core.signatures.PackageName; import sootup.core.types.ClassType; import sootup.core.types.PrimitiveType; import sootup.core.validation.FieldModifiersValidator; @@ -26,7 +25,6 @@ import sootup.java.core.JavaSootField; import sootup.java.core.JavaSootMethod; import sootup.java.core.OverridingJavaClassSource; -import sootup.java.core.types.JavaClassType; import sootup.java.core.views.JavaView; public class FieldModifiersValidatorTest { @@ -68,10 +66,11 @@ public JavaSootClass testClassCreatorWithModifiers( assertEquals(1, body.getLocalCount()); FieldSignature fieldSignature = - new FieldSignature( - new JavaClassType("FieldModifiersValidator", PackageName.DEFAULT_PACKAGE), - "i", - PrimitiveType.IntType.getInstance()); + view.getIdentifierFactory() + .getFieldSignature( + "i", + view.getIdentifierFactory().getClassType("FieldModifiersValidator"), + PrimitiveType.IntType.getInstance()); JavaSootField dummyField = JavaSootField.JavaSootFieldBuilder.builder() .withSignature(fieldSignature) diff --git a/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java b/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java index 9f7572a96ee..7352b63e5ae 100644 --- a/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java @@ -16,6 +16,7 @@ import sootup.core.types.*; import sootup.core.validation.FieldRefValidator; import sootup.core.validation.ValidationException; +import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.JavaSootClass; import sootup.java.core.views.JavaView; import sootup.jimple.frontend.JimpleAnalysisInputLocation; @@ -45,7 +46,7 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName("jimple"); + return new JavaIdentifierFactory().getPackageName("jimple"); } }; diff --git a/sootup.tests/src/test/java/sootup/tests/validator/IdentityStmtsValidatorTest.java b/sootup.tests/src/test/java/sootup/tests/validator/IdentityStmtsValidatorTest.java index fc243f998a6..8d4f5dc04a0 100644 --- a/sootup.tests/src/test/java/sootup/tests/validator/IdentityStmtsValidatorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/validator/IdentityStmtsValidatorTest.java @@ -14,6 +14,7 @@ import sootup.core.types.*; import sootup.core.validation.IdentityStmtsValidator; import sootup.core.validation.ValidationException; +import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.JavaSootClass; import sootup.java.core.JavaSootMethod; import sootup.java.core.views.JavaView; @@ -43,7 +44,7 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName(""); + return new JavaIdentifierFactory().getPackageName(""); } }; diff --git a/sootup.tests/src/test/java/sootup/tests/validator/JimpleTrapValidatorTest.java b/sootup.tests/src/test/java/sootup/tests/validator/JimpleTrapValidatorTest.java index c056a343943..95e05509fcd 100644 --- a/sootup.tests/src/test/java/sootup/tests/validator/JimpleTrapValidatorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/validator/JimpleTrapValidatorTest.java @@ -14,6 +14,7 @@ import sootup.core.types.*; import sootup.core.validation.JimpleTrapValidator; import sootup.core.validation.ValidationException; +import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.JavaSootClass; import sootup.java.core.views.JavaView; import sootup.jimple.frontend.JimpleAnalysisInputLocation; @@ -42,7 +43,7 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName("jimple"); + return new JavaIdentifierFactory().getPackageName("jimple"); } }; diff --git a/sootup.tests/src/test/java/sootup/tests/validator/LocalsValidatorTest.java b/sootup.tests/src/test/java/sootup/tests/validator/LocalsValidatorTest.java index ade2dad9bc1..5ca863f04e0 100644 --- a/sootup.tests/src/test/java/sootup/tests/validator/LocalsValidatorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/validator/LocalsValidatorTest.java @@ -15,6 +15,7 @@ import sootup.core.types.*; import sootup.core.validation.LocalsValidator; import sootup.core.validation.ValidationException; +import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.JavaSootClass; import sootup.java.core.views.JavaView; import sootup.jimple.frontend.JimpleAnalysisInputLocation; @@ -44,7 +45,7 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName("jimple"); + return new JavaIdentifierFactory().getPackageName("jimple"); } }; diff --git a/sootup.tests/src/test/java/sootup/tests/validator/MethodValidatorTest.java b/sootup.tests/src/test/java/sootup/tests/validator/MethodValidatorTest.java index f978e5b35fd..9cf1c0f2de0 100644 --- a/sootup.tests/src/test/java/sootup/tests/validator/MethodValidatorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/validator/MethodValidatorTest.java @@ -12,6 +12,7 @@ import sootup.core.types.*; import sootup.core.validation.MethodValidator; import sootup.core.validation.ValidationException; +import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.JavaSootClass; import sootup.java.core.JavaSootMethod; import sootup.java.core.views.JavaView; @@ -39,7 +40,7 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName("jimple"); + return new JavaIdentifierFactory().getPackageName("jimple"); } }; diff --git a/sootup.tests/src/test/java/sootup/tests/validator/NewValidatorTest.java b/sootup.tests/src/test/java/sootup/tests/validator/NewValidatorTest.java index bfb449f2d5b..f4c006e561b 100644 --- a/sootup.tests/src/test/java/sootup/tests/validator/NewValidatorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/validator/NewValidatorTest.java @@ -16,6 +16,7 @@ import sootup.core.types.*; import sootup.core.validation.NewValidator; import sootup.core.validation.ValidationException; +import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.JavaSootClass; import sootup.java.core.views.JavaView; import sootup.jimple.frontend.JimpleAnalysisInputLocation; @@ -46,7 +47,7 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName("jimple"); + return new JavaIdentifierFactory().getPackageName("jimple"); } }; diff --git a/sootup.tests/src/test/java/sootup/tests/validator/TrapsValidatorTest.java b/sootup.tests/src/test/java/sootup/tests/validator/TrapsValidatorTest.java index e57c2275391..27717cc7f21 100644 --- a/sootup.tests/src/test/java/sootup/tests/validator/TrapsValidatorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/validator/TrapsValidatorTest.java @@ -14,6 +14,7 @@ import sootup.core.types.*; import sootup.core.validation.TrapsValidator; import sootup.core.validation.ValidationException; +import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.JavaSootClass; import sootup.java.core.views.JavaView; import sootup.jimple.frontend.JimpleAnalysisInputLocation; @@ -42,7 +43,7 @@ public String getClassName() { @Override public PackageName getPackageName() { - return new PackageName("jimple"); + return new JavaIdentifierFactory().getPackageName("jimple"); } };