Search in sources :

Example 1 with ExecutableFactory

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

the class ExecutableFactoryTest method testCreateReference.

@Test
public void testCreateReference() {
    Factory f = createFactory();
    ExecutableFactory ef = f.Executable();
    String signature = "boolean Object#equals(Object)";
    CtExecutableReference<Object> eref = ef.createReference(signature);
    String type = eref.getType().getQualifiedName();
    String decltype = eref.getDeclaringType().getQualifiedName();
    String name = eref.getSimpleName();
    List<CtTypeReference<?>> params = eref.getParameters();
    List<CtTypeReference<?>> atas = eref.getActualTypeArguments();
    Assert.assertEquals("boolean", type);
    Assert.assertEquals("Object", decltype);
    Assert.assertEquals("equals", name);
    Assert.assertEquals(1, params.size());
    Assert.assertEquals(0, atas.size());
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) ExecutableFactory(spoon.reflect.factory.ExecutableFactory) Factory(spoon.reflect.factory.Factory) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) ExecutableFactory(spoon.reflect.factory.ExecutableFactory) Test(org.junit.Test)

Example 2 with ExecutableFactory

use of spoon.reflect.factory.ExecutableFactory 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)

Aggregations

ExecutableFactory (spoon.reflect.factory.ExecutableFactory)2 CtTypeReference (spoon.reflect.reference.CtTypeReference)2 ArrayList (java.util.ArrayList)1 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)1 Test (org.junit.Test)1 CtLambda (spoon.reflect.code.CtLambda)1 CtConstructor (spoon.reflect.declaration.CtConstructor)1 CtExecutable (spoon.reflect.declaration.CtExecutable)1 CtField (spoon.reflect.declaration.CtField)1 CtParameter (spoon.reflect.declaration.CtParameter)1 CoreFactory (spoon.reflect.factory.CoreFactory)1 Factory (spoon.reflect.factory.Factory)1 TypeFactory (spoon.reflect.factory.TypeFactory)1 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)1