Search in sources :

Example 1 with CtTypeAccess

use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.

the class CodeFactoryTest method testThisAccess.

@Test
public void testThisAccess() throws Exception {
    final Factory factory = createFactory();
    final CtTypeReference<Object> type = factory.Type().createReference("fr.inria.Test");
    final CtThisAccess<Object> thisAccess = factory.Code().createThisAccess(type);
    assertNotNull(thisAccess.getTarget());
    assertTrue(thisAccess.getTarget() instanceof CtTypeAccess);
    assertEquals(type, ((CtTypeAccess) thisAccess.getTarget()).getAccessedType());
}
Also used : Factory(spoon.reflect.factory.Factory) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) CtTypeAccess(spoon.reflect.code.CtTypeAccess) Test(org.junit.Test)

Example 2 with CtTypeAccess

use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.

the class DefaultJavaPrettyPrinter method visitCtInvocation.

@Override
public <T> void visitCtInvocation(CtInvocation<T> invocation) {
    enterCtStatement(invocation);
    enterCtExpression(invocation);
    if (invocation.getExecutable().isConstructor()) {
        // It's a constructor (super or this)
        elementPrinterHelper.writeActualTypeArguments(invocation.getExecutable());
        CtType<?> parentType;
        try {
            parentType = invocation.getParent(CtType.class);
        } catch (ParentNotInitializedException e) {
            parentType = null;
        }
        if (parentType != null && parentType.getQualifiedName() != null && parentType.getQualifiedName().equals(invocation.getExecutable().getDeclaringType().getQualifiedName())) {
            printer.writeKeyword("this");
        } else {
            if (invocation.getTarget() != null && !invocation.getTarget().isImplicit()) {
                scan(invocation.getTarget());
                printer.writeSeparator(".");
            }
            printer.writeKeyword("super");
        }
    } else {
        // It's a method invocation
        boolean isImported = this.isImported(invocation.getExecutable());
        if (!isImported) {
            try (Writable _context = context.modify()) {
                if (invocation.getTarget() instanceof CtTypeAccess) {
                    _context.ignoreGenerics(true);
                }
                if (invocation.getTarget() != null && !invocation.getTarget().isImplicit()) {
                    scan(invocation.getTarget());
                    printer.writeSeparator(".");
                }
            }
        }
        elementPrinterHelper.writeActualTypeArguments(invocation);
        if (env.isPreserveLineNumbers()) {
            getPrinterHelper().adjustStartPosition(invocation);
        }
        printer.writeIdentifier(invocation.getExecutable().getSimpleName());
    }
    try (ListPrinter lp = elementPrinterHelper.createListPrinter(false, "(", false, false, ",", true, false, ")")) {
        for (CtExpression<?> e : invocation.getArguments()) {
            lp.printSeparatorIfAppropriate();
            scan(e);
        }
    }
    exitCtExpression(invocation);
}
Also used : ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) CtType(spoon.reflect.declaration.CtType) Writable(spoon.reflect.visitor.PrintingContext.Writable) CtTypeAccess(spoon.reflect.code.CtTypeAccess)

Example 3 with CtTypeAccess

use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.

the class DefaultJavaPrettyPrinter method printCtFieldAccess.

private <T> void printCtFieldAccess(CtFieldAccess<T> f) {
    enterCtExpression(f);
    try (Writable _context = context.modify()) {
        if ((f.getVariable().isStatic() || "class".equals(f.getVariable().getSimpleName())) && f.getTarget() instanceof CtTypeAccess) {
            _context.ignoreGenerics(true);
        }
        CtExpression<?> target = f.getTarget();
        if (target != null) {
            boolean isInitializeStaticFinalField = isInitializeStaticFinalField(f.getTarget());
            boolean isStaticField = f.getVariable().isStatic();
            boolean isImportedField = this.isImported(f.getVariable());
            if (!isInitializeStaticFinalField && !(isStaticField && isImportedField)) {
                if (target.isImplicit() && !(f.getVariable().getFieldDeclaration() == null && this.env.getNoClasspath())) {
                    /*
						 * target is implicit, check whether there is no conflict with an local variable, catch variable or parameter
						 * in case of conflict make it explicit, otherwise the field access is shadowed by that variable.
						 * Search for potential variable declaration until we found a class which declares or inherits this field
						 */
                    final CtField<?> field = f.getVariable().getFieldDeclaration();
                    if (field != null) {
                        final String fieldName = field.getSimpleName();
                        CtVariable<?> var = f.getVariable().map(new PotentialVariableDeclarationFunction(fieldName)).first();
                        if (var != field) {
                            // another variable declaration was found which is hiding the field declaration for this field access. Make the field access expicit
                            target.setImplicit(false);
                        }
                    } else {
                        // There is a model inconsistency
                        printer.writeComment(f.getFactory().createComment("ERROR: Missing field \"" + f.getVariable().getSimpleName() + "\", please check your model. The code may not compile.", CommentType.BLOCK)).writeSpace();
                    }
                }
                // the implicit drives the separator
                if (!target.isImplicit()) {
                    scan(target);
                    printer.writeSeparator(".");
                }
            }
            _context.ignoreStaticAccess(true);
        }
        scan(f.getVariable());
    }
    exitCtExpression(f);
}
Also used : PotentialVariableDeclarationFunction(spoon.reflect.visitor.filter.PotentialVariableDeclarationFunction) Writable(spoon.reflect.visitor.PrintingContext.Writable) CtTypeAccess(spoon.reflect.code.CtTypeAccess)

Example 4 with CtTypeAccess

use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.

the class CtAnnotationImpl method convertValueToExpression.

private CtExpression convertValueToExpression(Object value) {
    CtExpression res;
    if (value.getClass().isArray()) {
        // Value should be converted to a CtNewArray.
        res = getFactory().Core().createNewArray();
        Object[] values = (Object[]) value;
        res.setType(getFactory().Type().createArrayReference(getFactory().Type().createReference(value.getClass().getComponentType())));
        for (Object o : values) {
            ((CtNewArray) res).addElement(convertValueToExpression(o));
        }
    } else if (value instanceof Collection) {
        // Value should be converted to a CtNewArray.
        res = getFactory().Core().createNewArray();
        Collection values = (Collection) value;
        res.setType(getFactory().Type().createArrayReference(getFactory().Type().createReference(values.toArray()[0].getClass())));
        for (Object o : values) {
            ((CtNewArray) res).addElement(convertValueToExpression(o));
        }
    } else if (value instanceof Class) {
        // Value should be a field access to a .class.
        res = getFactory().Code().createClassAccess(getFactory().Type().createReference((Class) value));
    } else if (value instanceof Field) {
        // Value should be a field access to a field.
        CtFieldReference<Object> variable = getFactory().Field().createReference((Field) value);
        variable.setStatic(true);
        CtTypeAccess target = getFactory().Code().createTypeAccess(getFactory().Type().createReference(((Field) value).getDeclaringClass()));
        CtFieldRead fieldRead = getFactory().Core().createFieldRead();
        fieldRead.setVariable(variable);
        fieldRead.setTarget(target);
        fieldRead.setType(target.getAccessedType());
        res = fieldRead;
    } else if (isPrimitive(value.getClass()) || value instanceof String) {
        // Value should be a literal.
        res = getFactory().Code().createLiteral(value);
    } else if (value.getClass().isEnum()) {
        final CtTypeReference declaringClass = getFactory().Type().createReference(((Enum) value).getDeclaringClass());
        final CtFieldReference variableRef = getFactory().Field().createReference(declaringClass, declaringClass, ((Enum) value).name());
        CtTypeAccess target = getFactory().Code().createTypeAccess(declaringClass);
        CtFieldRead fieldRead = getFactory().Core().createFieldRead();
        fieldRead.setVariable(variableRef);
        fieldRead.setTarget(target);
        fieldRead.setType(declaringClass);
        res = fieldRead;
    } else {
        throw new SpoonException("Please, submit a valid value.");
    }
    return res;
}
Also used : CtFieldRead(spoon.reflect.code.CtFieldRead) CtExpression(spoon.reflect.code.CtExpression) SpoonException(spoon.SpoonException) CtFieldReference(spoon.reflect.reference.CtFieldReference) CtNewArray(spoon.reflect.code.CtNewArray) CtField(spoon.reflect.declaration.CtField) MetamodelPropertyField(spoon.reflect.annotations.MetamodelPropertyField) Field(java.lang.reflect.Field) CtTypeReference(spoon.reflect.reference.CtTypeReference) Collection(java.util.Collection) CtTypeAccess(spoon.reflect.code.CtTypeAccess)

Example 5 with CtTypeAccess

use of spoon.reflect.code.CtTypeAccess in project spoon by INRIA.

the class InvocationTest method testTypeOfStaticInvocation.

@Test
public void testTypeOfStaticInvocation() throws Exception {
    SpoonAPI launcher = new Launcher();
    launcher.run(new String[] { "-i", "./src/test/java/spoon/test/invocations/testclasses/", "-o", "./target/spooned/" });
    Factory factory = launcher.getFactory();
    CtClass<?> aClass = factory.Class().get(Foo.class);
    final List<CtInvocation<?>> elements = aClass.getElements(new AbstractFilter<CtInvocation<?>>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation<?> element) {
            return element.getTarget() != null;
        }
    });
    assertEquals(2, elements.size());
    assertTrue(elements.get(0).getTarget() instanceof CtTypeAccess);
    assertTrue(elements.get(1).getTarget() instanceof CtTypeAccess);
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtTypeAccess(spoon.reflect.code.CtTypeAccess) SpoonAPI(spoon.SpoonAPI) Test(org.junit.Test)

Aggregations

CtTypeAccess (spoon.reflect.code.CtTypeAccess)14 Test (org.junit.Test)6 Launcher (spoon.Launcher)5 CtTypeReference (spoon.reflect.reference.CtTypeReference)5 CtExpression (spoon.reflect.code.CtExpression)3 CtType (spoon.reflect.declaration.CtType)3 Factory (spoon.reflect.factory.Factory)3 Writable (spoon.reflect.visitor.PrintingContext.Writable)3 Pozole (spoon.test.type.testclasses.Pozole)3 Collection (java.util.Collection)2 CtBinaryOperator (spoon.reflect.code.CtBinaryOperator)2 CtFieldRead (spoon.reflect.code.CtFieldRead)2 CtNewArray (spoon.reflect.code.CtNewArray)2 CtFieldReference (spoon.reflect.reference.CtFieldReference)2 AmplificationHelper (fr.inria.diversify.utils.AmplificationHelper)1 Field (java.lang.reflect.Field)1 Collections (java.util.Collections)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 ArrayAllocationExpression (org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression)1