Search in sources :

Example 16 with CtFieldReference

use of spoon.reflect.reference.CtFieldReference in project spoon by INRIA.

the class ImportScannerTest method testTargetTypeNull.

@Test
public void testTargetTypeNull() throws Exception {
    Launcher spoon = new Launcher();
    Factory factory = spoon.createFactory();
    CtFieldReference fieldRef = factory.createFieldReference();
    fieldRef.setStatic(true);
    ImportScanner importScanner = new MinimalImportScanner();
    importScanner.computeImports(fieldRef);
    Collection<CtImport> imports = importScanner.getAllImports();
    assertEquals(0, imports.size());
}
Also used : CtImport(spoon.reflect.declaration.CtImport) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtFieldReference(spoon.reflect.reference.CtFieldReference) MinimalImportScanner(spoon.reflect.visitor.MinimalImportScanner) ImportScanner(spoon.reflect.visitor.ImportScanner) MinimalImportScanner(spoon.reflect.visitor.MinimalImportScanner) Test(org.junit.Test)

Example 17 with CtFieldReference

use of spoon.reflect.reference.CtFieldReference in project spoon by INRIA.

the class ImportBuilderTest method testSimpleStaticImport.

@Test
public void testSimpleStaticImport() {
    // contract: simple static import are imported correctly
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/jdtimportbuilder/testclasses/StaticImport.java");
    spoon.getEnvironment().setAutoImports(true);
    spoon.buildModel();
    CtClass classA = spoon.getFactory().Class().get(StaticImport.class);
    CompilationUnit unitA = spoon.getFactory().CompilationUnit().getMap().get(classA.getPosition().getFile().getPath());
    Collection<CtImport> imports = unitA.getImports();
    assertEquals(1, imports.size());
    CtImport ref = imports.iterator().next();
    assertTrue(ref.getReference() instanceof CtFieldReference);
    assertEquals("spoon.test.jdtimportbuilder.testclasses.staticimport.Dependency#ANY", ((CtFieldReference) ref.getReference()).getQualifiedName());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtClass(spoon.reflect.declaration.CtClass) CtImport(spoon.reflect.declaration.CtImport) Launcher(spoon.Launcher) CtFieldReference(spoon.reflect.reference.CtFieldReference) Test(org.junit.Test)

Example 18 with CtFieldReference

use of spoon.reflect.reference.CtFieldReference in project spoon by INRIA.

the class QualifiedThisRefTest method testPrintCtFieldAccessWorkEvenWhenParentNotInitialized.

@Test
public void testPrintCtFieldAccessWorkEvenWhenParentNotInitialized() throws Exception {
    CtClass zeclass = factory.Class().get(QualifiedThisRef.class);
    List<CtMethod> methods = zeclass.getMethodsByName("bla");
    assertEquals(1, methods.size());
    CtStatement invocation = methods.get(0).getBody().getStatement(0);
    assertTrue(invocation instanceof CtInvocation);
    CtInvocation<?> arg0 = (CtInvocation) invocation;
    CtExpression param = arg0.getArguments().get(0);
    CtExecutableReference execref = factory.Core().createExecutableReference();
    execref.setDeclaringType(factory.Type().createReference("java.util.Map"));
    execref.setSimpleName("exorcise");
    execref.setStatic(true);
    CtTypeReference tmp = param.getType();
    CtExpression arg = null;
    CtFieldReference ctfe = factory.createFieldReference();
    ctfe.setSimpleName("class");
    ctfe.setDeclaringType(tmp.box());
    arg = factory.Core().createFieldRead();
    ((CtFieldAccessImpl) arg).setVariable(ctfe);
    CtLiteral location = factory.Core().createLiteral();
    location.setType(factory.Type().createReference(String.class));
    CtTypeReference tmpref = factory.Core().clone(tmp);
    CtInvocation invoc = factory.Core().createInvocation();
    invoc.setExecutable(execref);
    invoc.setArguments(Arrays.asList(new CtExpression[] { param, arg, location }));
    execref.setActualTypeArguments(Arrays.asList(new CtTypeReference<?>[] { tmpref }));
    // succeeds
    arg0.getArguments().set(0, invoc);
    DefaultJavaPrettyPrinter printer = new DefaultJavaPrettyPrinter(factory.getEnvironment());
    printer.visitCtClass(zeclass);
    assertFalse(printer.getResult().isEmpty());
}
Also used : CtExpression(spoon.reflect.code.CtExpression) CtFieldReference(spoon.reflect.reference.CtFieldReference) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) CtLiteral(spoon.reflect.code.CtLiteral) CtStatement(spoon.reflect.code.CtStatement) CtFieldAccessImpl(spoon.support.reflect.code.CtFieldAccessImpl) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

CtFieldReference (spoon.reflect.reference.CtFieldReference)18 Test (org.junit.Test)9 CtTypeReference (spoon.reflect.reference.CtTypeReference)6 Launcher (spoon.Launcher)5 CtField (spoon.reflect.declaration.CtField)5 CtFieldAccess (spoon.reflect.code.CtFieldAccess)4 CtClass (spoon.reflect.declaration.CtClass)4 CtElement (spoon.reflect.declaration.CtElement)4 CtMethod (spoon.reflect.declaration.CtMethod)4 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)4 CtInvocation (spoon.reflect.code.CtInvocation)3 CtImport (spoon.reflect.declaration.CtImport)3 ParentNotInitializedException (spoon.reflect.declaration.ParentNotInitializedException)3 Factory (spoon.reflect.factory.Factory)3 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 SpoonException (spoon.SpoonException)2 MetamodelPropertyField (spoon.reflect.annotations.MetamodelPropertyField)2