Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/maven/org.ow2.asm-asm-all-6.0_BETA
Browse files Browse the repository at this point in the history
  • Loading branch information
khatchad authored Jul 29, 2024
2 parents c68b0fa + 65525f9 commit a6f7280
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ public static boolean isPytestCase(IClass klass) {
IClass container = dmb.getContainer();
String containerName = container.getReference().getName().getClassName().toString();

if (containerName.startsWith("Test") && container instanceof PythonClass) {
if ((containerName.startsWith("Test") || containerName.endsWith("Test"))
&& container instanceof PythonClass) {
// It's a test class.
PythonClass containerClass = (PythonClass) container;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,91 +231,7 @@ public void visitPropertyRead(AstPropertyRead instruction) {
logger.fine(
"Detected wildcard for " + instruction.getMemberRef() + " in " + instruction + ".");

int objRef = instruction.getObjectRef();
logger.fine("Seeing if " + objRef + " refers to an import.");

SSAInstruction def = this.du.getDef(objRef);
logger.finer("Found definition: " + def + ".");

TypeName scriptTypeName =
this.ir.getMethod().getReference().getDeclaringClass().getName();
logger.finer("Found script: " + scriptTypeName + ".");

String scriptName = getScriptName(scriptTypeName);
logger.fine("Script name is: " + scriptName);
assert scriptName.endsWith("." + PYTHON_FILE_EXTENSION);

if (def instanceof SSAInvokeInstruction) {
// Library case.
SSAInvokeInstruction invokeInstruction = (SSAInvokeInstruction) def;
MethodReference declaredTarget = invokeInstruction.getDeclaredTarget();
Atom declaredTargetName = declaredTarget.getName();

if (declaredTargetName.equals(IMPORT_FUNCTION_NAME)) {
// It's an import "statement" importing a library.
logger.fine("Found library import statement in: " + scriptTypeName + ".");

logger.info(
"Adding: "
+ declaredTarget.getDeclaringClass().getName().toString().substring(1)
+ " to wildcard imports for: "
+ scriptName
+ ".");

// Add the library to the script's queue of wildcard imports.
getBuilder()
.getScriptToWildcardImports()
.compute(
scriptName,
(k, v) -> {
if (v == null) {
Deque<MethodReference> deque = new ArrayDeque<>();
deque.push(declaredTarget);
return deque;
} else {
v.push(declaredTarget);
return v;
}
});
}
} else if (def instanceof SSAGetInstruction) {
// We are importing from a script.
SSAGetInstruction getInstruction = (SSAGetInstruction) def;
String strippedFieldName = getStrippedDeclaredFieldName(getInstruction);

MethodReference methodReference =
getMethodReferenceRepresentingScript(strippedFieldName);

logger.info(
"Adding: "
+ methodReference.getDeclaringClass().getName().toString().substring(1)
+ " to wildcard imports for: "
+ scriptName
+ ".");

// Add the script to the queue of this script's wildcard imports.
getBuilder()
.getScriptToWildcardImports()
.compute(
scriptName,
(k, v) -> {
if (v == null) {
Deque<MethodReference> deque = new ArrayDeque<>();
deque.push(methodReference);
return deque;
} else {
v.push(methodReference);
return v;
}
});
} else
throw new IllegalArgumentException(
"Not expecting the definition: "
+ def
+ " of the object reference of: "
+ instruction
+ " to be: "
+ def.getClass());
processWildcardImports(instruction);
}

// check if we are reading from an module initialization script.
Expand Down Expand Up @@ -346,6 +262,100 @@ public void visitPropertyRead(AstPropertyRead instruction) {
}
}

/**
* Processes the given {@link AstPropertyRead} for any potential wildcard imports being utilized
* by the instruction.
*
* @param instruction The {@link AstPropertyRead} whose definition may depend on a wildcard
* import.
*/
private void processWildcardImports(AstPropertyRead instruction) {
int objRef = instruction.getObjectRef();
logger.fine("Seeing if " + objRef + " refers to an import.");

SSAInstruction def = this.du.getDef(objRef);
logger.finer("Found definition: " + def + ".");

TypeName scriptTypeName = this.ir.getMethod().getReference().getDeclaringClass().getName();
logger.finer("Found script: " + scriptTypeName + ".");

String scriptName = getScriptName(scriptTypeName);
logger.fine("Script name is: " + scriptName);
assert scriptName.endsWith("." + PYTHON_FILE_EXTENSION);

if (def instanceof SSAInvokeInstruction) {
// Library case.
SSAInvokeInstruction invokeInstruction = (SSAInvokeInstruction) def;
MethodReference declaredTarget = invokeInstruction.getDeclaredTarget();
Atom declaredTargetName = declaredTarget.getName();

if (declaredTargetName.equals(IMPORT_FUNCTION_NAME)) {
// It's an import "statement" importing a library.
logger.fine("Found library import statement in: " + scriptTypeName + ".");

logger.info(
"Adding: "
+ declaredTarget.getDeclaringClass().getName().toString().substring(1)
+ " to wildcard imports for: "
+ scriptName
+ ".");

// Add the library to the script's queue of wildcard imports.
getBuilder()
.getScriptToWildcardImports()
.compute(
scriptName,
(k, v) -> {
if (v == null) {
Deque<MethodReference> deque = new ArrayDeque<>();
deque.push(declaredTarget);
return deque;
} else {
v.push(declaredTarget);
return v;
}
});
}
} else if (def instanceof SSAGetInstruction) {
// We are importing from a script.
SSAGetInstruction getInstruction = (SSAGetInstruction) def;
String strippedFieldName = getStrippedDeclaredFieldName(getInstruction);

MethodReference methodReference = getMethodReferenceRepresentingScript(strippedFieldName);

logger.info(
"Adding: "
+ methodReference.getDeclaringClass().getName().toString().substring(1)
+ " to wildcard imports for: "
+ scriptName
+ ".");

// Add the script to the queue of this script's wildcard imports.
getBuilder()
.getScriptToWildcardImports()
.compute(
scriptName,
(k, v) -> {
if (v == null) {
Deque<MethodReference> deque = new ArrayDeque<>();
deque.push(methodReference);
return deque;
} else {
v.push(methodReference);
return v;
}
});
} else if (def instanceof AstPropertyRead) processWildcardImports((AstPropertyRead) def);
else
throw new IllegalArgumentException(
"Not expecting the definition: "
+ def
+ " of the object reference of: "
+ instruction
+ " to be: "
+ def.getClass());
}

/**
* Given a script's name, returns the {@link MethodReference} representing the script.
*
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<build-alias>b000</build-alias>
<wala.version>1.6.0</wala.version>
<spotless.version>2.43.0</spotless.version>
<maven.surefire.version>3.3.0</maven.surefire.version>
<maven.surefire.version>3.3.1</maven.surefire.version>
<parallel>both</parallel>
<logging.config.file>${maven.multiModuleProjectDirectory}/logging.properties</logging.config.file>
</properties>
Expand Down

0 comments on commit a6f7280

Please sign in to comment.