use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.
the class TypeFactory method createTypeParameterReference.
/**
* Creates a type parameter reference with no bounds.
*
* @param name
* the name of the formal parameter
*/
public CtTypeParameterReference createTypeParameterReference(String name) {
CtTypeParameterReference typeParam = factory.Core().createTypeParameterReference();
typeParam.setSimpleName(name);
return typeParam;
}
use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.
the class ParentExiter method scanCtElement.
@Override
public void scanCtElement(CtElement e) {
if (child instanceof CtAnnotation && this.jdtTreeBuilder.getContextBuilder().annotationValueName.isEmpty()) {
// we check if the current element can have the annotation attached
CtAnnotatedElementType annotatedElementType = CtAnnotation.getAnnotatedElementTypeForCtElement(e);
annotatedElementType = (e instanceof CtTypeParameter || e instanceof CtTypeParameterReference) ? CtAnnotatedElementType.TYPE_USE : annotatedElementType;
// in case of noclasspath, we cannot be 100% sure, so we guess it must be attached...
if (this.jdtTreeBuilder.getFactory().getEnvironment().getNoClasspath() || (annotatedElementType != null && JDTTreeBuilderQuery.hasAnnotationWithType((Annotation) childJDT, annotatedElementType))) {
e.addAnnotation((CtAnnotation<?>) child);
}
// in this case the annotation should be (also) attached to the type
if (e instanceof CtTypedElement && JDTTreeBuilderQuery.hasAnnotationWithType((Annotation) childJDT, CtAnnotatedElementType.TYPE_USE)) {
List<CtAnnotation> annotations = new ArrayList<>();
if (!annotationsMap.containsKey(e)) {
annotationsMap.put((CtTypedElement<?>) e, annotations);
} else {
annotations = annotationsMap.get(e);
}
annotations.add((CtAnnotation) child.clone());
annotationsMap.put((CtTypedElement<?>) e, annotations);
}
}
}
use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.
the class ReferenceBuilder method getTypeParameterReference.
CtTypeReference<Object> getTypeParameterReference(TypeBinding binding, TypeReference ref) {
CtTypeReference<Object> ctRef = getTypeReference(binding);
if (ctRef != null && isCorrectTypeReference(ref)) {
if (!(ctRef instanceof CtTypeParameterReference)) {
CtTypeParameterReference typeParameterRef = this.jdtTreeBuilder.getFactory().Core().createTypeParameterReference();
typeParameterRef.setSimpleName(ctRef.getSimpleName());
typeParameterRef.setDeclaringType(ctRef.getDeclaringType());
typeParameterRef.setPackage(ctRef.getPackage());
ctRef = typeParameterRef;
}
insertGenericTypesInNoClasspathFromJDTInSpoon(ref, ctRef);
return ctRef;
}
return getTypeParameterReference(CharOperation.toString(ref.getParameterizedTypeName()));
}
use of spoon.reflect.reference.CtTypeParameterReference 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.CtTypeParameterReference in project spoon by INRIA.
the class ConstructorTest method assertIntersectionTypeInConstructor.
private void assertIntersectionTypeInConstructor(CtTypeReference<?> boundingType1) {
assertTrue(boundingType1 instanceof CtIntersectionTypeReference);
CtIntersectionTypeReference<?> boundingType = boundingType1.asCtIntersectionTypeReference();
final List<CtTypeReference<?>> bounds = boundingType.getBounds().stream().collect(Collectors.toList());
CtTypeReference<?> genericTacos = bounds.get(0);
assertEquals("Tacos", genericTacos.getSimpleName());
assertEquals(1, genericTacos.getAnnotations().size());
assertEquals(1, genericTacos.getActualTypeArguments().size());
CtTypeParameterReference wildcard = (CtTypeParameterReference) genericTacos.getActualTypeArguments().get(0);
assertEquals("?", wildcard.getSimpleName());
assertEquals(1, wildcard.getAnnotations().size());
assertEquals("C", wildcard.getBoundingType().getSimpleName());
assertEquals(1, wildcard.getBoundingType().getAnnotations().size());
assertEquals("Serializable", bounds.get(1).getSimpleName());
assertEquals(1, bounds.get(1).getAnnotations().size());
}
Aggregations