use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.
the class JavaReflectionTreeBuilder method visitTypeParameterReference.
@Override
public <T extends GenericDeclaration> void visitTypeParameterReference(TypeVariable<T> parameter) {
final CtTypeParameterReference typeParameterReference = factory.Core().createTypeParameterReference();
typeParameterReference.setSimpleName(parameter.getName());
enter(new TypeReferenceRuntimeBuilderContext(typeParameterReference));
super.visitTypeParameterReference(parameter);
exit();
contexts.peek().addTypeName(typeParameterReference);
}
use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.
the class TemplateMatcher method getTemplateTypeParameters.
private List<CtTypeReference<?>> getTemplateTypeParameters(final CtClass<? extends Template<?>> templateType) {
final List<CtTypeReference<?>> ts = new ArrayList<>();
final Collection<String> c = Parameters.getNames(templateType);
new CtScanner() {
@Override
public void visitCtTypeParameterReference(CtTypeParameterReference reference) {
if (c.contains(reference.getSimpleName())) {
ts.add(reference);
}
}
@Override
public <T> void visitCtTypeReference(CtTypeReference<T> reference) {
if (c.contains(reference.getSimpleName())) {
ts.add(reference);
}
}
}.scan(templateType);
return ts;
}
use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.
the class CtTypeImpl method isSameParameter.
private boolean isSameParameter(CtMethod<?> method, CtTypeReference<?> ctParameterType, CtTypeReference<?> expectedType) {
if (expectedType instanceof CtTypeParameterReference) {
/*
* the expectedType is a generic parameter whose declaration should be searched in scope of method
* (not in scope of it's parent, where it can found another/wrong type parameter declaration of same name.
*/
CtTypeParameterReference tpr = (CtTypeParameterReference) expectedType;
expectedType = tpr.clone();
expectedType.setParent(method);
if (expectedType.getDeclaration() == null) {
return false;
}
}
if (expectedType instanceof CtTypeParameterReference && ctParameterType instanceof CtTypeParameterReference) {
// both types are generic
if (!ctParameterType.equals(expectedType)) {
return false;
}
} else if (expectedType instanceof CtTypeParameterReference) {
// expectedType type is generic, ctParameterType is real type
if (!expectedType.getTypeErasure().getQualifiedName().equals(ctParameterType.getQualifiedName())) {
return false;
}
} else if (ctParameterType instanceof CtTypeParameterReference) {
// ctParameterType is generic, expectedType type is real type
CtTypeParameter declaration = (CtTypeParameter) ctParameterType.getDeclaration();
if (declaration != null && declaration.getSuperclass() instanceof CtIntersectionTypeReference) {
for (CtTypeReference<?> ctTypeReference : declaration.getSuperclass().asCtIntersectionTypeReference().getBounds()) {
if (ctTypeReference.equals(expectedType)) {
return true;
}
}
} else if (declaration != null && declaration.getSuperclass() != null) {
return declaration.getSuperclass().equals(expectedType);
} else {
return getFactory().Type().objectType().equals(expectedType);
}
} else if (!expectedType.getQualifiedName().equals(ctParameterType.getQualifiedName())) {
// both are real types
return false;
}
return true;
}
use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.
the class AbstractTypingContext method adaptType.
@Override
public CtTypeReference<?> adaptType(CtTypeInformation type) {
CtTypeReference<?> result;
boolean isCopy = false;
if (type instanceof CtTypeReference<?>) {
if (type instanceof CtTypeParameterReference) {
return adaptTypeParameterReference((CtTypeParameterReference) type);
}
result = (CtTypeReference<?>) type;
} else {
if (type instanceof CtTypeParameter) {
return adaptTypeParameter((CtTypeParameter) type);
}
CtType<?> t = (CtType<?>) type;
result = t.getFactory().Type().createReference(t, true);
isCopy = true;
}
if (result.getActualTypeArguments().size() > 0) {
// we have to adapt actual type arguments recursive too
if (isCopy == false) {
CtElement parent = result.getParent();
result = result.clone();
result.setParent(parent);
List<CtTypeReference<?>> actTypeArgs = new ArrayList<>(result.getActualTypeArguments());
for (int i = 0; i < actTypeArgs.size(); i++) {
CtTypeReference adaptedTypeArgs = adaptType(actTypeArgs.get(i));
// for some type argument we might return null to avoid recursive calls
if (adaptedTypeArgs != null) {
actTypeArgs.set(i, adaptedTypeArgs.clone());
}
}
result.setActualTypeArguments(actTypeArgs);
}
}
return result;
}
use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.
the class MethodTypingContext method adaptTypeForNewMethod.
private CtTypeReference<?> adaptTypeForNewMethod(CtTypeReference<?> typeRef) {
if (typeRef == null) {
return null;
}
if (typeRef instanceof CtTypeParameterReference) {
CtTypeParameterReference typeParamRef = (CtTypeParameterReference) typeRef;
CtTypeParameter typeParam = typeParamRef.getDeclaration();
if (typeParam == null) {
throw new SpoonException("Declaration of the CtTypeParameter should not be null.");
}
if (typeParam.getTypeParameterDeclarer() instanceof CtExecutable) {
// the parameter is declared in scope of Method or Constructor
return typeRef.clone();
}
}
// it is not type reference of scopeMethod. Adapt it using classTypingContext
return classTypingContext.adaptType(typeRef);
}
Aggregations