use of spoon.reflect.reference.CtArrayTypeReference in project Ex2Amplifier by STAMP-project.
the class Utils method getRealTypeOfLiteral.
public static CtTypeReference<?> getRealTypeOfLiteral(CtLiteral<?> literal) {
if (literal.getValue() instanceof Number) {
final CtTypedElement typedParent = literal.getParent(CtTypedElement.class);
if (typedParent != null) {
// special treatment for int literal
if (typedParent instanceof CtAbstractInvocation) {
final CtExecutableReference<?> executable = ((CtAbstractInvocation) typedParent).getExecutable();
final int indexOf = ((CtAbstractInvocation) typedParent).getArguments().indexOf(literal);
final CtTypeReference<?> ctTypeReference = executable.getParameters().get(indexOf);
if (Number.class.isAssignableFrom(ctTypeReference.getActualClass())) {
return ctTypeReference;
} else {
return literal.getType();
}
} else if (typedParent.getType() instanceof CtArrayTypeReference) {
return ((CtArrayTypeReference) typedParent.getType()).getComponentType();
} else {
return typedParent.getType();
}
} else {
throw new IllegalArgumentException(literal.toString());
}
} else {
return literal.getType();
}
}
use of spoon.reflect.reference.CtArrayTypeReference in project dspot by STAMP-project.
the class ValueCreator method generateArray.
private static CtExpression generateArray(CtTypeReference type) {
CtArrayTypeReference arrayType = (CtArrayTypeReference) type;
CtTypeReference typeComponent = arrayType.getComponentType();
CtNewArray<?> newArray = type.getFactory().createNewArray();
final int size = AmplificationHelper.getRandom().nextInt(MAX_ARRAY_SIZE);
newArray.setType(arrayType);
if (size == 0) {
newArray.setDimensionExpressions(Collections.singletonList(type.getFactory().createLiteral(size)));
} else if (ValueCreatorHelper.canGenerateAValueForType(typeComponent)) {
IntStream.range(0, size).mapToObj(i -> generateRandomValue(typeComponent)).forEach(newArray::addElement);
}
return newArray;
}
use of spoon.reflect.reference.CtArrayTypeReference in project spoon by INRIA.
the class AnnotationFactory method annotate.
/**
* Creates/updates an element's annotation value.
*
* @param element
* the program element to annotate
* @param annotationType
* the annotation type
* @param annotationElementName
* the annotation element name
* @param value
* the value of the annotation element
* @return the created/updated annotation
*/
public <A extends Annotation> CtAnnotation<A> annotate(CtElement element, CtTypeReference<A> annotationType, String annotationElementName, Object value) {
final CtAnnotation<A> annotation = annotate(element, annotationType);
boolean isArray;
// try with CT reflection
CtAnnotationType<A> ctAnnotationType = ((CtAnnotationType<A>) annotation.getAnnotationType().getDeclaration());
boolean newValue = annotation.getValue(annotationElementName) == null;
if (ctAnnotationType != null) {
CtMethod<?> e = ctAnnotationType.getMethod(annotationElementName);
isArray = (e.getType() instanceof CtArrayTypeReference);
} else {
Method m;
try {
m = annotation.getAnnotationType().getActualClass().getMethod(annotationElementName, new Class[0]);
} catch (Exception ex) {
annotation.addValue(annotationElementName, value);
return annotation;
}
isArray = m.getReturnType().isArray();
}
if (isArray || newValue) {
annotation.addValue(annotationElementName, value);
} else {
throw new SpoonException("cannot assign an array to a non-array annotation element");
}
return annotation;
}
use of spoon.reflect.reference.CtArrayTypeReference 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.CtArrayTypeReference in project spoon by INRIA.
the class ConstructorCallTest method testConstructorCallWithGenericArray.
@Test
public void testConstructorCallWithGenericArray() throws Exception {
final CtConstructorCall<?> ctConstructorCall = constructorCallsPanini.get(0);
assertEquals(1, ctConstructorCall.getType().getActualTypeArguments().size());
final CtTypeReference<?> implicitArray = ctConstructorCall.getType().getActualTypeArguments().get(0);
assertTrue(implicitArray.isImplicit());
final CtArrayTypeReference implicitArrayTyped = (CtArrayTypeReference) implicitArray;
assertEquals("", implicitArrayTyped.toString());
assertEquals("AtomicLong[]", implicitArrayTyped.getSimpleName());
assertTrue(implicitArrayTyped.getComponentType().isImplicit());
assertEquals("", implicitArrayTyped.getComponentType().toString());
assertEquals("AtomicLong", implicitArrayTyped.getComponentType().getSimpleName());
}
Aggregations