Search in sources :

Example 11 with CtTypeReference

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

the class ReferenceBuilder method getExecutableReference.

@SuppressWarnings("unchecked")
<T> CtExecutableReference<T> getExecutableReference(MethodBinding exec) {
    if (exec == null) {
        return null;
    }
    final CtExecutableReference ref = this.jdtTreeBuilder.getFactory().Core().createExecutableReference();
    if (exec.isConstructor()) {
        ref.setSimpleName(CtExecutableReference.CONSTRUCTOR_NAME);
        ref.setType(getTypeReference(exec.declaringClass));
    } else {
        ref.setSimpleName(new String(exec.selector));
        ref.setType(getTypeReference(exec.returnType));
    }
    if (exec instanceof ProblemMethodBinding) {
        if (exec.declaringClass != null && Arrays.asList(exec.declaringClass.methods()).contains(exec)) {
            ref.setDeclaringType(getTypeReference(exec.declaringClass));
        } else {
            final CtReference declaringType = getDeclaringReferenceFromImports(exec.constantPoolName());
            if (declaringType instanceof CtTypeReference) {
                ref.setDeclaringType((CtTypeReference<?>) declaringType);
            }
        }
        if (exec.isConstructor()) {
            // super() invocation have a good declaring class.
            ref.setDeclaringType(getTypeReference(exec.declaringClass));
        }
        ref.setStatic(true);
    } else {
        ref.setDeclaringType(getTypeReference(exec.declaringClass));
        ref.setStatic(exec.isStatic());
    }
    if (exec.declaringClass instanceof ParameterizedTypeBinding) {
        ref.setDeclaringType(getTypeReference(exec.declaringClass.actualType()));
    }
    // original() method returns a result not null when the current method is generic.
    if (exec.original() != null) {
        final List<CtTypeReference<?>> parameters = new ArrayList<>(exec.original().parameters.length);
        for (TypeBinding b : exec.original().parameters) {
            parameters.add(getTypeReference(b));
        }
        ref.setParameters(parameters);
    } else if (exec.parameters != null) {
        // This is a method without a generic argument.
        final List<CtTypeReference<?>> parameters = new ArrayList<>();
        for (TypeBinding b : exec.parameters) {
            parameters.add(getTypeReference(b));
        }
        ref.setParameters(parameters);
    }
    return ref;
}
Also used : ParameterizedTypeBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding) ProblemMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding) CtReference(spoon.reflect.reference.CtReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) BinaryTypeBinding(org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding) ParameterizedTypeBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) MissingTypeBinding(org.eclipse.jdt.internal.compiler.lookup.MissingTypeBinding) SourceTypeBinding(org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding) JDTTreeBuilderQuery.searchTypeBinding(spoon.support.compiler.jdt.JDTTreeBuilderQuery.searchTypeBinding) PolyTypeBinding(org.eclipse.jdt.internal.compiler.lookup.PolyTypeBinding) LocalTypeBinding(org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding) BaseTypeBinding(org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding) RawTypeBinding(org.eclipse.jdt.internal.compiler.lookup.RawTypeBinding) ArrayList(java.util.ArrayList) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) List(java.util.List) ArrayList(java.util.ArrayList)

Example 12 with CtTypeReference

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

the class ReferenceBuilder method getLambdaExecutableReference.

/**
 * In noclasspath, lambda doesn't have always a binding for their variables accesses in their block/expression.
 * Here, we make the job of JDT and bind their variables accesses to their parameters.
 *
 * @param singleNameReference Name of the variable access.
 * @return executable reference which corresponds to the lambda.
 */
public CtExecutableReference<?> getLambdaExecutableReference(SingleNameReference singleNameReference) {
    ASTPair potentialLambda = null;
    for (ASTPair astPair : jdtTreeBuilder.getContextBuilder().stack) {
        if (astPair.node instanceof LambdaExpression) {
            potentialLambda = astPair;
            // stop at innermost lambda, fixes #1100
            break;
        }
    }
    if (potentialLambda == null) {
        return null;
    }
    LambdaExpression lambdaJDT = (LambdaExpression) potentialLambda.node;
    for (Argument argument : lambdaJDT.arguments()) {
        if (CharOperation.equals(argument.name, singleNameReference.token)) {
            CtTypeReference<?> declaringType = null;
            if (lambdaJDT.enclosingScope instanceof MethodScope) {
                declaringType = jdtTreeBuilder.getReferencesBuilder().getTypeReference(((MethodScope) lambdaJDT.enclosingScope).parent.enclosingSourceType());
            }
            CtLambda<?> ctLambda = (CtLambda<?>) potentialLambda.element;
            List<CtTypeReference<?>> parametersType = new ArrayList<>();
            List<CtParameter<?>> parameters = ctLambda.getParameters();
            for (CtParameter<?> parameter : parameters) {
                parametersType.add(parameter.getType() != null ? parameter.getType().clone() : // it's the best match :(
                jdtTreeBuilder.getFactory().Type().OBJECT.clone());
            }
            return jdtTreeBuilder.getFactory().Executable().createReference(declaringType, ctLambda.getType(), ctLambda.getSimpleName(), parametersType);
        }
    }
    return null;
}
Also used : CtLambda(spoon.reflect.code.CtLambda) Argument(org.eclipse.jdt.internal.compiler.ast.Argument) ArrayList(java.util.ArrayList) CtParameter(spoon.reflect.declaration.CtParameter) MethodScope(org.eclipse.jdt.internal.compiler.lookup.MethodScope) CtTypeReference(spoon.reflect.reference.CtTypeReference) LambdaExpression(org.eclipse.jdt.internal.compiler.ast.LambdaExpression)

Example 13 with CtTypeReference

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

the class ReferenceBuilder method buildTypeReferenceInternal.

private <T> CtTypeReference<T> buildTypeReferenceInternal(CtTypeReference<T> typeReference, TypeReference type, Scope scope) {
    if (type == null) {
        return null;
    }
    CtTypeReference<?> currentReference = typeReference;
    for (int position = type.getTypeName().length - 1; position >= 0; position--) {
        if (currentReference == null) {
            break;
        }
        this.jdtTreeBuilder.getContextBuilder().enter(currentReference, type);
        if (type.annotations != null && type.annotations.length - 1 <= position && type.annotations[position] != null && type.annotations[position].length > 0) {
            for (Annotation annotation : type.annotations[position]) {
                if (scope instanceof ClassScope) {
                    annotation.traverse(this.jdtTreeBuilder, (ClassScope) scope);
                } else if (scope instanceof BlockScope) {
                    annotation.traverse(this.jdtTreeBuilder, (BlockScope) scope);
                } else {
                    annotation.traverse(this.jdtTreeBuilder, (BlockScope) null);
                }
            }
        }
        if (type.getTypeArguments() != null && type.getTypeArguments().length - 1 <= position && type.getTypeArguments()[position] != null && type.getTypeArguments()[position].length > 0) {
            currentReference.getActualTypeArguments().clear();
            for (TypeReference typeArgument : type.getTypeArguments()[position]) {
                if (typeArgument instanceof Wildcard || typeArgument.resolvedType instanceof WildcardBinding || typeArgument.resolvedType instanceof TypeVariableBinding) {
                    currentReference.addActualTypeArgument(buildTypeParameterReference(typeArgument, scope));
                } else {
                    currentReference.addActualTypeArgument(buildTypeReference(typeArgument, scope));
                }
            }
        } else if ((type instanceof ParameterizedSingleTypeReference || type instanceof ParameterizedQualifiedTypeReference) && !isTypeArgumentExplicit(type.getTypeArguments())) {
            for (CtTypeReference<?> actualTypeArgument : currentReference.getActualTypeArguments()) {
                actualTypeArgument.setImplicit(true);
                if (actualTypeArgument instanceof CtArrayTypeReference) {
                    ((CtArrayTypeReference) actualTypeArgument).getComponentType().setImplicit(true);
                }
            }
        }
        if (type instanceof Wildcard && typeReference instanceof CtTypeParameterReference) {
            ((CtTypeParameterReference) typeReference).setBoundingType(buildTypeReference(((Wildcard) type).bound, scope));
        }
        this.jdtTreeBuilder.getContextBuilder().exit(type);
        currentReference = currentReference.getDeclaringType();
    }
    return typeReference;
}
Also used : TypeVariableBinding(org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding) WildcardBinding(org.eclipse.jdt.internal.compiler.lookup.WildcardBinding) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) ClassScope(org.eclipse.jdt.internal.compiler.lookup.ClassScope) CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) Wildcard(org.eclipse.jdt.internal.compiler.ast.Wildcard) CtTypeReference(spoon.reflect.reference.CtTypeReference) BlockScope(org.eclipse.jdt.internal.compiler.lookup.BlockScope) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference)

Example 14 with CtTypeReference

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

the class ReferenceBuilder method getExecutableReference.

<T> CtExecutableReference<T> getExecutableReference(AllocationExpression allocationExpression) {
    CtExecutableReference<T> ref;
    if (allocationExpression.binding != null) {
        ref = getExecutableReference(allocationExpression.binding);
    } else {
        ref = jdtTreeBuilder.getFactory().Core().createExecutableReference();
        ref.setSimpleName(CtExecutableReference.CONSTRUCTOR_NAME);
        ref.setDeclaringType(getTypeReference(null, allocationExpression.type));
        final List<CtTypeReference<?>> parameters = new ArrayList<>(allocationExpression.argumentTypes.length);
        for (TypeBinding b : allocationExpression.argumentTypes) {
            parameters.add(getTypeReference(b));
        }
        ref.setParameters(parameters);
    }
    if (allocationExpression.type == null) {
        ref.setType(this.<T>getTypeReference(allocationExpression.expectedType()));
    }
    return ref;
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) BinaryTypeBinding(org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding) ParameterizedTypeBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) MissingTypeBinding(org.eclipse.jdt.internal.compiler.lookup.MissingTypeBinding) SourceTypeBinding(org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding) JDTTreeBuilderQuery.searchTypeBinding(spoon.support.compiler.jdt.JDTTreeBuilderQuery.searchTypeBinding) PolyTypeBinding(org.eclipse.jdt.internal.compiler.lookup.PolyTypeBinding) LocalTypeBinding(org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding) BaseTypeBinding(org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding) RawTypeBinding(org.eclipse.jdt.internal.compiler.lookup.RawTypeBinding) ArrayList(java.util.ArrayList)

Example 15 with CtTypeReference

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

the class DefaultCoreFactory method createWildcardStaticTypeMemberReference.

@Override
public CtTypeReference createWildcardStaticTypeMemberReference() {
    CtTypeReference result = new CtWildcardStaticTypeMemberReferenceImpl();
    result.setFactory(getMainFactory());
    return result;
}
Also used : CtWildcardStaticTypeMemberReferenceImpl(spoon.support.reflect.reference.CtWildcardStaticTypeMemberReferenceImpl) CtTypeReference(spoon.reflect.reference.CtTypeReference)

Aggregations

CtTypeReference (spoon.reflect.reference.CtTypeReference)121 Test (org.junit.Test)56 Launcher (spoon.Launcher)43 Factory (spoon.reflect.factory.Factory)32 CtMethod (spoon.reflect.declaration.CtMethod)26 CtType (spoon.reflect.declaration.CtType)24 ArrayList (java.util.ArrayList)22 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)17 CtClass (spoon.reflect.declaration.CtClass)16 CtField (spoon.reflect.declaration.CtField)16 List (java.util.List)15 CtElement (spoon.reflect.declaration.CtElement)14 CtInvocation (spoon.reflect.code.CtInvocation)13 CtReference (spoon.reflect.reference.CtReference)13 CtParameter (spoon.reflect.declaration.CtParameter)12 CtExpression (spoon.reflect.code.CtExpression)11 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)11 CtTypeParameterReference (spoon.reflect.reference.CtTypeParameterReference)11 CtStatement (spoon.reflect.code.CtStatement)9 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)9