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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations