Skip to content

Commit

Permalink
fix major bug and improve model integrity detection
Browse files Browse the repository at this point in the history
this commit will likely hurt performance by a lot, because previous optimizations had to be removed in the Evaluator
  • Loading branch information
Luro02 committed Jun 25, 2024
1 parent a235be0 commit 8710f81
Show file tree
Hide file tree
Showing 11 changed files with 322 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@
import spoon.processing.AbstractProcessor;
import spoon.processing.Processor;
import spoon.reflect.CtModel;
import spoon.reflect.code.CtCatchVariable;
import spoon.reflect.code.CtTargetedExpression;
import spoon.reflect.declaration.*;
import spoon.reflect.factory.CodeFactory;
import spoon.reflect.factory.Factory;
import spoon.reflect.factory.FactoryImpl;
import spoon.reflect.reference.CtCatchVariableReference;
import spoon.reflect.reference.CtReference;
import spoon.reflect.visitor.*;
import spoon.reflect.visitor.filter.NamedElementFilter;
import spoon.support.DefaultCoreFactory;
import spoon.support.StandardEnvironment;

import java.io.IOException;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -139,7 +145,36 @@ private void buildModelMaybe() {
return;
}

Launcher launcher = new Launcher();

// Fix for something similar to https://github.com/INRIA/spoon/issues/5868
Factory baseFactory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment()) {
private transient CodeFactory code;

@Override
public CodeFactory Code() {
if (this.code == null) {
this.code = new CodeFactory(this) {
@Override
public <T> CtCatchVariableReference<T> createCatchVariableReference(CtCatchVariable<T> catchVariable) {
// The implementation of the original method is broken, resulting in elements which point to the
// wrong or an invalid parent.
//
// This is a workaround until the issue is fixed in spoon.
CtCatchVariableReference<T> ref = this.factory.Core().createCatchVariableReference();

ref.setType(catchVariable.getType() == null ? null : catchVariable.getType().clone());
ref.setSimpleName(catchVariable.getSimpleName());
ref.setParent(catchVariable);

return ref;
}
};
}
return this.code;
}
};

Launcher launcher = new Launcher(baseFactory);
launcher.addInputResource(file.getSpoonResource());
launcher.getEnvironment().setShouldCompile(false);
launcher.getEnvironment().setSourceClasspath(new String[]{jar.toAbsolutePath().toString()});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,9 @@ public List<Problem> checkFile(

AnalysisResult result;
try (TempLocation tempLinterLocation = this.tempLocation.createTempDirectory("linter")) {
Path tmpLocation = tempLinterLocation.toPath();

if (!integratedChecks.isEmpty()) {
scheduler.submitTask((s, reporter) -> {
IntegratedAnalysis analysis = new IntegratedAnalysis(file, tmpLocation);
IntegratedAnalysis analysis = new IntegratedAnalysis(file);
analysis.lint(integratedChecks, statusConsumer, s);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ private static boolean isExceptionWithoutMessage(CtExpression<?> expression) {
return false;
}

// check if the invoked constructor passes a message to the exception
// check if the invoked constructor passes a message to the parent exception like this:
// class MyException extends Exception { MyException() { super("here is the message"); } }
if (ctConstructorCall.getExecutable().getExecutableDeclaration() instanceof CtConstructor<?> ctConstructor
&& ctConstructor.getBody().filterChildren(ctElement -> ctElement instanceof CtInvocation<?> ctInvocation
// we just check if there is any invocation with a message, because this is easier and might be enough
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,29 @@
import java.util.Optional;
import java.util.function.Consumer;

public class UploadedFile implements AutoCloseable {
public final class UploadedFile implements AutoCloseable {
private static final Logger LOG = LoggerFactory.getLogger(UploadedFile.class);

private final CodeModel model;
private final SourceInfo source;
private final CompilationResult compilationResult;
private final ClassLoader classLoader;
private final TempLocation tempLocation;

private UploadedFile(CodeModel model, SourceInfo source, CompilationResult compilationResult) {
private UploadedFile(CodeModel model, SourceInfo source, CompilationResult compilationResult, ClassLoader classLoader, TempLocation tempLocation) {
this.model = model;
this.source = source;
this.compilationResult = compilationResult;
this.classLoader = classLoader;
this.tempLocation = tempLocation;
}

public UploadedFile copy() {
try {
return UploadedFile.build(this.source, this.tempLocation.createTempDirectory("copy"), unused -> {}, this.classLoader);
} catch (IOException | CompilationFailureException exception) {
throw new IllegalStateException(exception);
}
}

public static UploadedFile build(
Expand Down Expand Up @@ -54,7 +66,7 @@ public static UploadedFile build(

var model = CodeModel.buildFor(source, compilationResult.get().jar(), classLoader);

return new UploadedFile(model, source, compilationResult.get());
return new UploadedFile(model, source, compilationResult.get(), classLoader, tmpLocation);
}

public SourceInfo getSource() {
Expand Down
Loading

0 comments on commit 8710f81

Please sign in to comment.