Search in sources :

Example 6 with TypeFactory

use of spoon.reflect.factory.TypeFactory in project spoon by INRIA.

the class JDTTreeBuilderHelper method createVariableAccessNoClasspath.

/**
 * Analyzes if {@code singleNameReference} points to a {@link CtVariable} visible in current
 * scope and, if existent, returns its corresponding {@link CtVariableAccess}. Returns
 * {@code null} if {@code singleNameReference} could not be resolved as variable access. Since
 * we are in noclasspath mode this function may also returns {@code null} if
 * {@code singleNameReference} points to a variable declared by an unknown class.
 *
 * @param singleNameReference
 * 		The potential variable access.
 * @return A {@link CtVariableAccess} if {@code singleNameReference} points to a variable
 * 		   visible in current scope, {@code null} otherwise.
 */
<T> CtVariableAccess<T> createVariableAccessNoClasspath(SingleNameReference singleNameReference) {
    final TypeFactory typeFactory = jdtTreeBuilder.getFactory().Type();
    final CoreFactory coreFactory = jdtTreeBuilder.getFactory().Core();
    final ExecutableFactory executableFactory = jdtTreeBuilder.getFactory().Executable();
    final ContextBuilder contextBuilder = jdtTreeBuilder.getContextBuilder();
    final ReferenceBuilder referenceBuilder = jdtTreeBuilder.getReferencesBuilder();
    final PositionBuilder positionBuilder = jdtTreeBuilder.getPositionBuilder();
    final String name = CharOperation.charToString(singleNameReference.token);
    final CtVariable<T> variable = contextBuilder.getVariableDeclaration(name);
    if (variable == null) {
        return null;
    }
    final CtVariableReference<T> variableReference;
    final CtVariableAccess<T> variableAccess;
    if (variable instanceof CtParameter) {
        // create variable of concrete type to avoid type casting while calling methods
        final CtParameterReference<T> parameterReference = coreFactory.createParameterReference();
        if (variable.getParent() instanceof CtLambda) {
        // nothing
        } else {
            // Unfortunately, we can not use `variable.getReference()` here as some parent
            // references (in terms of Java objects) have not been set up yet. Thus, we need to
            // create the required parameter reference by our own.
            // Since the given parameter has not been declared in a lambda expression it must
            // have been declared by a method/constructor.
            final CtExecutable executable = (CtExecutable) variable.getParent();
            // create list of executable's parameter types
            final List<CtTypeReference<?>> parameterTypesOfExecutable = new ArrayList<>();
            @SuppressWarnings("unchecked") final List<CtParameter<?>> parametersOfExecutable = executable.getParameters();
            for (CtParameter<?> parameter : parametersOfExecutable) {
                parameterTypesOfExecutable.add(parameter.getType() != null ? parameter.getType().clone() : // it's the best match :(
                typeFactory.OBJECT.clone());
            }
            // find executable's corresponding jdt element
            AbstractMethodDeclaration executableJDT = null;
            for (final ASTPair astPair : contextBuilder.stack) {
                if (astPair.element == executable) {
                    executableJDT = (AbstractMethodDeclaration) astPair.node;
                }
            }
            assert executableJDT != null;
            // create a reference to executable's declaring class
            final CtTypeReference declaringReferenceOfExecutable = // available
            executableJDT.binding == null ? coreFactory.createTypeReference() : referenceBuilder.getTypeReference(executableJDT.binding.declaringClass);
            // If executable is a constructor, `executable.getType()` returns null since the
            // parent is not available yet. Fortunately, however, the return type of a
            // constructor is its declaring class which, in our case, is already available with
            // declaringReferenceOfExecutable.
            CtTypeReference executableTypeReference = executable instanceof CtConstructor ? // indirectly sets the parent of `rt` and, thus, may break the AST!
            declaringReferenceOfExecutable.clone() : executable.getType().clone();
        }
        variableReference = parameterReference;
        variableAccess = isLhsAssignment(contextBuilder, singleNameReference) ? coreFactory.<T>createVariableWrite() : coreFactory.<T>createVariableRead();
    } else if (variable instanceof CtField) {
        variableReference = variable.getReference();
        variableAccess = isLhsAssignment(contextBuilder, singleNameReference) ? coreFactory.<T>createFieldWrite() : coreFactory.<T>createFieldRead();
    } else {
        // CtLocalVariable, CtCatchVariable, ...
        variableReference = variable.getReference();
        variableAccess = isLhsAssignment(contextBuilder, singleNameReference) ? coreFactory.<T>createVariableWrite() : coreFactory.<T>createVariableRead();
    }
    variableReference.setSimpleName(name);
    variableReference.setPosition(positionBuilder.buildPosition(singleNameReference.sourceStart(), singleNameReference.sourceEnd()));
    variableAccess.setVariable(variableReference);
    return variableAccess;
}
Also used : CtLambda(spoon.reflect.code.CtLambda) ArrayList(java.util.ArrayList) CtParameter(spoon.reflect.declaration.CtParameter) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtField(spoon.reflect.declaration.CtField) CoreFactory(spoon.reflect.factory.CoreFactory) CtExecutable(spoon.reflect.declaration.CtExecutable) CtConstructor(spoon.reflect.declaration.CtConstructor) TypeFactory(spoon.reflect.factory.TypeFactory) ExecutableFactory(spoon.reflect.factory.ExecutableFactory) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)

Example 7 with TypeFactory

use of spoon.reflect.factory.TypeFactory in project spoon by INRIA.

the class LiteralTest method testBuildLiternal.

@Test
public void testBuildLiternal() throws Exception {
    CtType<Tacos> ctType = buildClass(Tacos.class);
    TypeFactory typeFactory = ctType.getFactory().Type();
    CtLiteral<?> literal = (CtLiteral<?>) ctType.getField("a").getDefaultExpression();
    assertEquals(0, literal.getValue());
    assertTrue(literal.getType().isPrimitive());
    assertEquals(typeFactory.INTEGER_PRIMITIVE, literal.getType());
    literal = (CtLiteral<?>) ctType.getField("b").getDefaultExpression();
    assertEquals(0x0, literal.getValue());
    assertTrue(literal.getType().isPrimitive());
    assertEquals(typeFactory.INTEGER_PRIMITIVE, literal.getType());
    literal = (CtLiteral<?>) ctType.getField("c").getDefaultExpression();
    assertEquals(0f, literal.getValue());
    assertTrue(literal.getType().isPrimitive());
    assertEquals(typeFactory.FLOAT_PRIMITIVE, literal.getType());
    literal = (CtLiteral<?>) ctType.getField("d").getDefaultExpression();
    assertEquals(0l, literal.getValue());
    assertTrue(literal.getType().isPrimitive());
    assertEquals(typeFactory.LONG_PRIMITIVE, literal.getType());
    literal = (CtLiteral<?>) ctType.getField("e").getDefaultExpression();
    assertEquals(0d, literal.getValue());
    assertTrue(literal.getType().isPrimitive());
    assertEquals(typeFactory.DOUBLE_PRIMITIVE, literal.getType());
    literal = (CtLiteral<?>) ctType.getField("f").getDefaultExpression();
    assertEquals('0', literal.getValue());
    assertTrue(literal.getType().isPrimitive());
    assertEquals(typeFactory.CHARACTER_PRIMITIVE, literal.getType());
    literal = (CtLiteral<?>) ctType.getField("g").getDefaultExpression();
    assertEquals("0", literal.getValue());
    assertFalse(literal.getType().isPrimitive());
    assertEquals(typeFactory.STRING, literal.getType());
    literal = (CtLiteral<?>) ctType.getField("h").getDefaultExpression();
    assertEquals(null, literal.getValue());
    assertFalse(literal.getType().isPrimitive());
    assertEquals(typeFactory.NULL_TYPE, literal.getType());
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) Tacos(spoon.test.literal.testclasses.Tacos) TypeFactory(spoon.reflect.factory.TypeFactory) Test(org.junit.Test)

Example 8 with TypeFactory

use of spoon.reflect.factory.TypeFactory in project spoon by INRIA.

the class TypeTest method testGetUsedTypes.

@Test
public void testGetUsedTypes() throws Exception {
    CtType<?> type = build("spoon.test.model", "Foo");
    TypeFactory tf = type.getFactory().Type();
    Set<CtTypeReference<?>> usedTypes = type.getUsedTypes(true);
    assertEquals(3, usedTypes.size());
    assertTrue(usedTypes.contains(tf.createReference(Bar.class)));
    assertTrue(usedTypes.contains(tf.createReference(Baz.class)));
    assertTrue(usedTypes.contains(tf.createReference(Baz.Inner.class)));
    assertEquals(0, type.getUsedTypes(false).size());
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) TypeFactory(spoon.reflect.factory.TypeFactory) Test(org.junit.Test)

Aggregations

TypeFactory (spoon.reflect.factory.TypeFactory)8 Test (org.junit.Test)6 CtTypeReference (spoon.reflect.reference.CtTypeReference)4 FieldFactory (spoon.reflect.factory.FieldFactory)3 CtElement (spoon.reflect.declaration.CtElement)2 CtField (spoon.reflect.declaration.CtField)2 CtType (spoon.reflect.declaration.CtType)2 CoreFactory (spoon.reflect.factory.CoreFactory)2 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)1 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)1 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)1 FieldBinding (org.eclipse.jdt.internal.compiler.lookup.FieldBinding)1 ReferenceBinding (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)1 Environment (spoon.compiler.Environment)1 CtLambda (spoon.reflect.code.CtLambda)1 CtLiteral (spoon.reflect.code.CtLiteral)1 CtConstructor (spoon.reflect.declaration.CtConstructor)1