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;
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations