Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FieldDeclarationVisitor #30

Merged
merged 8 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion JavaParser.astub
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import org.checkerframework.checker.signature.qual.*;
import org.checkerframework.checker.nullness.qual.Nullable;

package com.github.javaparser.resolution.types;

Expand Down Expand Up @@ -29,4 +30,10 @@ package com.github.javaparser.symbolsolver.resolution.typesolvers;
class JarTypeSolver {
// this method lists all the names of classes solved by JarTypeSolver. All the names are in fully-qualified names.
Set<@FullyQualifiedName String> getKnownClasses();
}
}

package com.github.javaparser.ast.stmt;

class IfStmt {
IfStmt setElseStmt(@Nullable Statement elseStmt);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.checkerframework.specimin;

import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.expr.SimpleName;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import java.util.HashMap;
import java.util.Map;
import org.checkerframework.checker.signature.qual.ClassGetSimpleName;

/**
* This visitor is designed to assist the UnsolvedSymbolVisitor in creating synthetic files for
* unsolved NameExpr instances by listing all the names of declared fields in the current input
* file. It's important to note that this visitor is intended to be used in conjunction with the
* UnsolvedSymbolVisitor, so both visitors will traverse the same Java file.
* Thus, @ClassGetSimpleName names for involved classes should be sufficient.
*/
public class FieldDeclarationsVisitor extends VoidVisitorAdapter<Void> {
/**
* A mapping of field names to the @ClassGetSimpleName name of the classes in which they are
* declared. Since inner classes can be involved, a map is used instead of a simple list.
*/
Map<String, @ClassGetSimpleName String> fieldNameToClassNameMap;

/** Constructs a new FieldDeclarationsVisitor. */
public FieldDeclarationsVisitor() {
fieldNameToClassNameMap = new HashMap<>();
}

@Override
public void visit(FieldDeclaration decl, Void p) {
ClassOrInterfaceDeclaration classNode =
(ClassOrInterfaceDeclaration) decl.getParentNode().get();
SimpleName classNodeSimpleName = classNode.getName();
String className = classNodeSimpleName.asString();
for (VariableDeclarator var : decl.getVariables()) {
fieldNameToClassNameMap.put(var.getNameAsString(), className);
}
}

/**
* Get the value of fieldAndItsClass
*
* @return the value of fieldAndItsClass
*/
public Map<String, @ClassGetSimpleName String> getFieldAndItsClass() {
return fieldNameToClassNameMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public class MethodPrunerVisitor extends ModifierVisitor<Void> {
*/
private Set<String> methodsToEmpty;

/**
* This boolean tracks whether the element currently being visited is inside a target method. It
* is set by {@link #visit(MethodDeclaration, Void)}.
*/
private boolean insideTargetMethod = false;

/**
* Creates the pruner. All methods this pruner encounters other than those in its input sets will
* be removed entirely. For both arguments, the Strings should be in the format produced by
Expand All @@ -46,12 +52,19 @@ public MethodPrunerVisitor(Set<String> methodsToKeep, Set<String> methodsToEmpty
public Visitable visit(MethodDeclaration methodDecl, Void p) {
ResolvedMethodDeclaration resolved = methodDecl.resolve();
if (methodsToLeaveUnchanged.contains(resolved.getQualifiedSignature())) {
return super.visit(methodDecl, p);
insideTargetMethod = true;
Visitable result = super.visit(methodDecl, p);
insideTargetMethod = false;
return result;
} else if (methodsToEmpty.contains(resolved.getQualifiedSignature())) {
methodDecl.setBody(StaticJavaParser.parseBlock("{ throw new Error(); }"));
return methodDecl;
} else {
methodDecl.remove();
// if insideTargetMethod is true, this current method declaration belongs to an anonnymous
// class inside the target method.
if (!insideTargetMethod) {
methodDecl.remove();
}
return methodDecl;
}
}
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/org/checkerframework/specimin/SpeciminRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -26,7 +25,6 @@
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.checkerframework.checker.signature.qual.ClassGetSimpleName;

/** This class is the main runner for Specimin. Use its main() method to start Specimin. */
public class SpeciminRunner {
Expand Down Expand Up @@ -113,6 +111,11 @@ public static void performMinimization(
addMissingClass.setExceptionToFalse();
for (CompilationUnit cu : parsedTargetFiles.values()) {
addMissingClass.setImportStatement(cu.getImports());
// it's important to make sure that getDeclarations and addMissingClass will visit the same
// file for each execution of the loop
FieldDeclarationsVisitor getDeclarations = new FieldDeclarationsVisitor();
cu.accept(getDeclarations, null);
addMissingClass.setFieldNameToClassNameMap(getDeclarations.getFieldAndItsClass());
cu.accept(addMissingClass, null);
}
addMissingClass.updateSyntheticSourceCode();
Expand Down Expand Up @@ -177,13 +180,13 @@ public static void performMinimization(
cu.accept(methodPruner, null);
}
for (Entry<String, CompilationUnit> target : parsedTargetFiles.entrySet()) {
// If a compilation output's entire body has been removed, do not output it.
// If a compilation output's entire body has been removed and the related class is not used by
// the target methods, do not output it.
if (isEmptyCompilationUnit(target.getValue())) {
boolean isASyntheticReturnType = addMissingClass.isASyntheticReturnType(target.getKey());
Collection<@ClassGetSimpleName String> listOfSuperClass = addMissingClass.getSuperClass();
boolean isASyntheticSuperClass =
!listOfSuperClass.isEmpty() && listOfSuperClass.contains(target.getKey());
if (!isASyntheticSuperClass && !isASyntheticReturnType) {
// target key will have this form: "path/of/package/ClassName.java"
String classFullyQualfiedName = target.getKey().replace("/", ".");
classFullyQualfiedName = classFullyQualfiedName.replace(".java", "");
if (!finder.getUsedClass().contains(classFullyQualfiedName)) {
continue;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.ast.expr.SuperExpr;
import com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt;
import com.github.javaparser.ast.type.ReferenceType;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.ast.visitor.ModifierVisitor;
import com.github.javaparser.ast.visitor.Visitable;
import com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration;
import java.util.ArrayList;
Expand Down Expand Up @@ -177,10 +180,24 @@ public Visitable visit(MethodDeclaration method, Void p) {
// TODO: test this with annotations
String methodName =
this.classFQName + "#" + methodDeclAsString.substring(methodDeclAsString.indexOf(' ') + 1);
// this method belongs to an anonymous class inside the target method
if (insideTargetMethod) {
ObjectCreationExpr parentExpression = (ObjectCreationExpr) method.getParentNode().get();
ResolvedConstructorDeclaration resolved = parentExpression.resolve();
String methodPackage = resolved.getPackageName();
String methodClass = resolved.getClassName();
usedMembers.add(methodPackage + "." + methodClass + "." + method.getNameAsString() + "()");
usedClass.add(methodPackage + "." + methodClass);
}

if (this.targetMethodNames.contains(methodName)) {
insideTargetMethod = true;
targetMethods.add(method.resolve().getQualifiedSignature());
unfoundMethods.remove(methodName);
Type returnType = method.getType();
if (returnType instanceof ReferenceType) {
usedClass.add(returnType.resolve().asReferenceType().getQualifiedName());
}
}
Visitable result = super.visit(method, p);
insideTargetMethod = false;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/checkerframework/specimin/UnsolvedMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ public void setReturnType(@ClassGetSimpleName String returnType) {
public @ClassGetSimpleName String getReturnType() {
return returnType;
}

/**
* Get the name of this method
*
* @return the name of this method
*/
public String getName() {
return name;
}

/**
* Return the content of the method. Note that the body of the method is stubbed out.
*
Expand Down
Loading