Search in sources :

Example 1 with CtArrayTypeReference

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();
    }
}
Also used : CtAbstractInvocation(spoon.reflect.code.CtAbstractInvocation) CtTypedElement(spoon.reflect.declaration.CtTypedElement) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference)

Example 2 with CtArrayTypeReference

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;
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference)

Example 3 with CtArrayTypeReference

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;
}
Also used : SpoonException(spoon.SpoonException) Method(java.lang.reflect.Method) CtMethod(spoon.reflect.declaration.CtMethod) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference) SpoonException(spoon.SpoonException)

Example 4 with CtArrayTypeReference

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;
}
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 5 with CtArrayTypeReference

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());
}
Also used : CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference) Test(org.junit.Test)

Aggregations

CtArrayTypeReference (spoon.reflect.reference.CtArrayTypeReference)13 CtTypeReference (spoon.reflect.reference.CtTypeReference)5 Test (org.junit.Test)4 List (java.util.List)2 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)2 TypeVariableBinding (org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding)2 WildcardBinding (org.eclipse.jdt.internal.compiler.lookup.WildcardBinding)2 Launcher (spoon.Launcher)2 CtExpression (spoon.reflect.code.CtExpression)2 CtInvocation (spoon.reflect.code.CtInvocation)2 CtNewArray (spoon.reflect.code.CtNewArray)2 CtMethod (spoon.reflect.declaration.CtMethod)2 ParentNotInitializedException (spoon.reflect.declaration.ParentNotInitializedException)2 CtTypeParameterReference (spoon.reflect.reference.CtTypeParameterReference)2 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)1 ArrayAllocationExpression (org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression)1 ArrayInitializer (org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)1