Search in sources :

Example 86 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.

the class ReplaceScanner method createListenerClass.

private CtClass createListenerClass(Factory factory, String listenerName, CtTypeReference getterType, Type type) {
    CtClass listener;
    listener = factory.Class().get(TARGET_REPLACE_PACKAGE + ".CtListener").clone();
    listener.setSimpleName(listenerName);
    target.addNestedType(listener);
    final List<CtTypeReference> references = listener.getElements(new TypeFilter<CtTypeReference>(CtTypeReference.class) {

        @Override
        public boolean matches(CtTypeReference reference) {
            return (TARGET_REPLACE_PACKAGE + ".CtListener").equals(reference.getQualifiedName());
        }
    });
    for (CtTypeReference reference : references) {
        reference.setPackage(listener.getPackage().getReference());
    }
    final CtTypeReference<Object> theInterface = factory.Class().createReference(TARGET_REPLACE_PACKAGE + "." + type.name);
    theInterface.addActualTypeArgument(getterType);
    final Set<CtTypeReference<?>> interfaces = new HashSet<>();
    interfaces.add(theInterface);
    listener.setSuperInterfaces(interfaces);
    return listener;
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtTypeReference(spoon.reflect.reference.CtTypeReference) HashSet(java.util.HashSet)

Example 87 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.

the class AstCheckerTest method testAvoidSetCollectionSavedOnAST.

@Test
public void testAvoidSetCollectionSavedOnAST() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("src/main/java");
    launcher.buildModel();
    final Factory factory = launcher.getFactory();
    final List<CtTypeReference<?>> collectionsRef = // 
    Arrays.asList(// 
    factory.Type().createReference(Collection.class), // 
    factory.Type().createReference(List.class), // 
    factory.Type().createReference(Set.class), factory.Type().createReference(Map.class));
    final List<CtInvocation<?>> invocations = Query.getElements(factory, new TypeFilter<CtInvocation<?>>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation<?> element) {
            if (!(element.getParent() instanceof CtInvocation)) {
                return false;
            }
            final CtInvocation<?> parent = (CtInvocation<?>) element.getParent();
            if (parent.getTarget() == null || !parent.getTarget().equals(element)) {
                return false;
            }
            if (!element.getExecutable().getDeclaringType().getSimpleName().startsWith("Ct")) {
                return false;
            }
            boolean isDataStructure = false;
            for (int i = 0; i < collectionsRef.size(); i++) {
                CtTypeReference<?> ctTypeReference = collectionsRef.get(i);
                if (element.getType().isSubtypeOf(ctTypeReference)) {
                    isDataStructure = true;
                    break;
                }
            }
            if (!isDataStructure) {
                return false;
            }
            final String simpleName = parent.getExecutable().getSimpleName();
            return simpleName.startsWith("add") || simpleName.startsWith("remove") || simpleName.startsWith("put");
        }
    });
    if (invocations.size() > 0) {
        final String error = // 
        invocations.stream().sorted(// 
        new CtLineElementComparator()).map(// 
        i -> "see " + i.getPosition().getFile().getAbsoluteFile() + " at " + i.getPosition().getLine()).collect(Collectors.joining(",\n"));
        throw new AssertionError(error);
    }
}
Also used : Arrays(java.util.Arrays) CtIf(spoon.reflect.code.CtIf) Launcher(spoon.Launcher) CtScanner(spoon.reflect.visitor.CtScanner) CtThrow(spoon.reflect.code.CtThrow) FineModelChangeListener(spoon.experimental.modelobs.FineModelChangeListener) Map(java.util.Map) CtExecutable(spoon.reflect.declaration.CtExecutable) Query(spoon.reflect.visitor.Query) DerivedProperty(spoon.support.DerivedProperty) CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtInvocation(spoon.reflect.code.CtInvocation) Collection(java.util.Collection) Set(java.util.Set) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) Collectors(java.util.stream.Collectors) CtTypeReference(spoon.reflect.reference.CtTypeReference) List(java.util.List) ModifierKind(spoon.reflect.declaration.ModifierKind) CtBlock(spoon.reflect.code.CtBlock) CtReturn(spoon.reflect.code.CtReturn) UnsettableProperty(spoon.support.UnsettableProperty) CtMethod(spoon.reflect.declaration.CtMethod) Set(java.util.Set) Factory(spoon.reflect.factory.Factory) CtInvocation(spoon.reflect.code.CtInvocation) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) Collection(java.util.Collection) CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) List(java.util.List) Map(java.util.Map) Test(org.junit.Test)

Example 88 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.

the class JDTTreeBuilder method createParameterizedType.

private boolean createParameterizedType(TypeReference parameterizedTypeReference) {
    if (skipTypeInAnnotation) {
        return true;
    }
    CtTypeReference typeReference = references.buildTypeReference(parameterizedTypeReference, null);
    CtTypeAccess typeAccess = factory.Code().createTypeAccessWithoutCloningReference(typeReference);
    context.enter(typeAccess, parameterizedTypeReference);
    return true;
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) CtTypeAccess(spoon.reflect.code.CtTypeAccess)

Example 89 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.

the class JDTTreeBuilder method visit.

@Override
public boolean visit(SingleTypeReference singleTypeReference, BlockScope scope) {
    if (skipTypeInAnnotation) {
        return true;
    }
    if (context.stack.peekFirst().node instanceof UnionTypeReference) {
        if (singleTypeReference.resolvedType == null) {
            CtTypeReference typeReference = factory.Type().createReference(singleTypeReference.toString());
            CtReference ref = references.getDeclaringReferenceFromImports(singleTypeReference.getLastToken());
            references.setPackageOrDeclaringType(typeReference, ref);
            context.enter(typeReference, singleTypeReference);
        } else {
            context.enter(references.<Throwable>getTypeReference(singleTypeReference.resolvedType), singleTypeReference);
        }
        return true;
    } else if (context.stack.peekFirst().element instanceof CtCatch) {
        context.enter(helper.createCatchVariable(singleTypeReference), singleTypeReference);
        return true;
    }
    context.enter(factory.Code().createTypeAccessWithoutCloningReference(references.buildTypeReference(singleTypeReference, scope)), singleTypeReference);
    return true;
}
Also used : CtReference(spoon.reflect.reference.CtReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) UnionTypeReference(org.eclipse.jdt.internal.compiler.ast.UnionTypeReference) CtCatch(spoon.reflect.code.CtCatch)

Example 90 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.

the class JDTTreeBuilderHelper method createFieldAccessNoClasspath.

/**
 * In no classpath mode, when we build a field access, we have a binding typed by ProblemBinding.
 * This binding doesn't contain all information but we can get some of them.
 *
 * @param singleNameReference
 * 		Used to get the problem binding of the field access and the name of the declaring type.
 * @return a field access.
 */
<T> CtFieldAccess<T> createFieldAccessNoClasspath(SingleNameReference singleNameReference) {
    CtFieldAccess<T> va;
    if (isLhsAssignment(jdtTreeBuilder.getContextBuilder(), singleNameReference)) {
        va = jdtTreeBuilder.getFactory().Core().createFieldWrite();
    } else {
        va = jdtTreeBuilder.getFactory().Core().createFieldRead();
    }
    va.setVariable(jdtTreeBuilder.getReferencesBuilder().<T>getVariableReference((ProblemBinding) singleNameReference.binding));
    final CtReference declaring = jdtTreeBuilder.getReferencesBuilder().getDeclaringReferenceFromImports(singleNameReference.token);
    if (declaring instanceof CtTypeReference && va.getVariable() != null) {
        final CtTypeReference<Object> declaringRef = (CtTypeReference<Object>) declaring;
        va.setTarget(jdtTreeBuilder.getFactory().Code().createTypeAccess(declaringRef));
        va.getVariable().setDeclaringType(declaringRef);
        va.getVariable().setStatic(true);
    }
    return va;
}
Also used : CtReference(spoon.reflect.reference.CtReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) ProblemBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemBinding)

Aggregations

CtTypeReference (spoon.reflect.reference.CtTypeReference)121 Test (org.junit.Test)56 Launcher (spoon.Launcher)43 Factory (spoon.reflect.factory.Factory)32 CtMethod (spoon.reflect.declaration.CtMethod)26 CtType (spoon.reflect.declaration.CtType)24 ArrayList (java.util.ArrayList)22 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)17 CtClass (spoon.reflect.declaration.CtClass)16 CtField (spoon.reflect.declaration.CtField)16 List (java.util.List)15 CtElement (spoon.reflect.declaration.CtElement)14 CtInvocation (spoon.reflect.code.CtInvocation)13 CtReference (spoon.reflect.reference.CtReference)13 CtParameter (spoon.reflect.declaration.CtParameter)12 CtExpression (spoon.reflect.code.CtExpression)11 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)11 CtTypeParameterReference (spoon.reflect.reference.CtTypeParameterReference)11 CtStatement (spoon.reflect.code.CtStatement)9 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)9