Search in sources :

Example 6 with SpoonModelBuilder

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());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) JDTSnippetCompiler(spoon.support.compiler.jdt.JDTSnippetCompiler) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 7 with SpoonModelBuilder

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());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtAssignment(spoon.reflect.code.CtAssignment) DefaultCoreFactory(spoon.support.DefaultCoreFactory) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) FactoryImpl(spoon.reflect.factory.FactoryImpl) CtClass(spoon.reflect.declaration.CtClass) JDTSnippetCompiler(spoon.support.compiler.jdt.JDTSnippetCompiler) CtMethod(spoon.reflect.declaration.CtMethod) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Example 8 with SpoonModelBuilder

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);
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 9 with SpoonModelBuilder

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);
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher)

Example 10 with SpoonModelBuilder

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;
    }
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) DefaultCoreFactory(spoon.support.DefaultCoreFactory) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

SpoonModelBuilder (spoon.SpoonModelBuilder)45 Launcher (spoon.Launcher)43 Test (org.junit.Test)35 Factory (spoon.reflect.factory.Factory)29 File (java.io.File)17 CtClass (spoon.reflect.declaration.CtClass)16 CtMethod (spoon.reflect.declaration.CtMethod)6 SpoonResource (spoon.compiler.SpoonResource)5 CtInvocation (spoon.reflect.code.CtInvocation)5 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)4 DefaultCoreFactory (spoon.support.DefaultCoreFactory)4 JDTSnippetCompiler (spoon.support.compiler.jdt.JDTSnippetCompiler)4 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)4 Before (org.junit.Before)3 FileNotFoundException (java.io.FileNotFoundException)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 CtParameterRemoveRefactoring (spoon.refactoring.CtParameterRemoveRefactoring)2 CtNewClass (spoon.reflect.code.CtNewClass)2 CtExecutable (spoon.reflect.declaration.CtExecutable)2 FactoryImpl (spoon.reflect.factory.FactoryImpl)2