use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class SignatureTest method testNullSignatureInUnboundVariable.
@Test
public void testNullSignatureInUnboundVariable() throws Exception {
// Unbound variable access bug fix:
// Bug description: The signature printer ignored the element Unbound variable reference
// (as well all Visitor that extend CtVisitor)
// Fix description: modify CtVisitor (including SignaturePrinter) for visiting unbound variable access.
Factory factory = new Launcher().createFactory();
// We want to compile a class with an reference to a class that is not
// in the classpath
// As consequence, we set the option NoClasspath as true.
factory.getEnvironment().setNoClasspath(true);
String unboundVarAccess = "Complex.I";
String content = "" + "class X {" + "public Object foo(java.util.List<String> l) {" + " Integer.toString(" + unboundVarAccess + ");" + " return null;" + "}};";
SpoonModelBuilder builder = new JDTSnippetCompiler(factory, content);
try {
builder.build();
Assert.fail();
} catch (Exception e) {
// Must fail due to the unbound element "Complex.I"
}
CtClass<?> clazz1 = (CtClass<?>) factory.Type().getAll().get(0);
Set<CtMethod<?>> methods = clazz1.getMethods();
CtMethod<?> method = (CtMethod<?>) methods.toArray()[0];
assertEquals("foo(java.util.List)", method.getSignature());
CtInvocation<?> invo = (CtInvocation<?>) method.getBody().getStatement(0);
CtExpression<?> argument1 = invo.getArguments().get(0);
assertEquals(unboundVarAccess, argument1.toString());
}
use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class SignatureTest method testUnboundFieldSignature.
@Test
public void testUnboundFieldSignature() {
Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
factory.getEnvironment().setNoClasspath(true);
String content = "" + "class PR {" + "public java.io.File foo(String p) {" + " this.mfield = p; " + " return null;" + "}" + "};";
SpoonModelBuilder builder = new JDTSnippetCompiler(factory, content);
try {
builder.build();
fail();
} catch (Exception e) {
// must fail
}
CtClass<?> clazz1 = (CtClass<?>) factory.Type().getAll().get(0);
assertNotNull(clazz1);
// **FIRST PART: passing local variable access.
// /--------From the first method we take the method invocations
CtMethod<?> methodString = (CtMethod<?>) clazz1.getMethods().toArray()[0];
assertEquals("foo(java.lang.String)", methodString.getSignature());
CtAssignment<?, ?> invoToInt1 = (CtAssignment<?, ?>) methodString.getBody().getStatement(0);
CtExpression<?> left = invoToInt1.getAssigned();
assertEquals("this.mfield", left.toString());
// null because noclasspath
assertEquals(null, left.getType());
assertEquals("this.mfield = p", invoToInt1.toString());
}
use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class SignatureTest method testBugSignature.
@Test
public void testBugSignature() throws Exception {
// contract: two methods with same name and return type yet different argument types
// must have different signatures
Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
SpoonModelBuilder comp = launcher.createCompiler();
comp.addInputSources(SpoonResourceHelper.resources("./src/main/java/spoon/SpoonModelBuilder.java"));
comp.build();
CtType<?> ctClass = (CtType<?>) comp.getFactory().Type().get(SpoonModelBuilder.class);
List<CtMethod> methods = ctClass.getElements(new NamedElementFilter<>(CtMethod.class, "addInputSource"));
assertEquals(2, methods.size());
CtMethod<?> method = methods.get(0);
assertEquals("addInputSource(java.io.File)", method.getSignature());
CtMethod<?> method2 = methods.get(1);
assertEquals("addInputSource(spoon.compiler.SpoonResource)", method2.getSignature());
assertNotEquals(method, method2);
}
use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class ModelUtils method build.
/**
* Utility method for testing: creates the model of `packageName` and the factory from src/test/java and returns the CtType corresponding to `className`
*/
public static <T extends CtType<?>> T build(String packageName, String className, final Factory f) throws Exception {
Launcher launcher = new Launcher() {
@Override
public Factory createFactory() {
return f;
}
};
SpoonModelBuilder comp = launcher.createCompiler();
comp.addInputSources(SpoonResourceHelper.resources("./src/test/java/" + packageName.replace('.', '/') + "/" + className + ".java"));
comp.build();
return comp.getFactory().Package().get(packageName).getType(className);
}
use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class ModelUtils method canBeBuilt.
/**
* checks that the file `outputDirectoryFile` can be parsed with Spoon , given a compliance level and the noclasspath option.
*/
public static void canBeBuilt(File outputDirectoryFile, int complianceLevel, boolean noClasspath) {
final Launcher launcher = new Launcher();
final Factory factory = launcher.getFactory();
factory.getEnvironment().setComplianceLevel(complianceLevel);
factory.getEnvironment().setNoClasspath(noClasspath);
final SpoonModelBuilder compiler = launcher.createCompiler(factory);
compiler.addInputSource(outputDirectoryFile);
try {
compiler.build();
} catch (Exception e) {
final AssertionError error = new AssertionError("Can't compile " + outputDirectoryFile.getName() + " because " + e.getMessage());
error.initCause(e);
throw error;
}
}
Aggregations