use of spoon.reflect.declaration.CtTypedElement 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.declaration.CtTypedElement in project dspot by STAMP-project.
the class AssertGeneratorHelper method isStmtToLog.
private static boolean isStmtToLog(String filter, CtStatement statement) {
if (!(statement.getParent() instanceof CtBlock)) {
return false;
}
// contract: for now, we do not log values inside loop
if (statement.getParent(CtLoop.class) != null) {
return false;
}
if (statement instanceof CtInvocation) {
CtInvocation invocation = (CtInvocation) statement;
// type tested by the test class
String targetType = "";
if (invocation.getTarget() != null && invocation.getTarget().getType() != null) {
targetType = invocation.getTarget().getType().getQualifiedName();
}
if (targetType.startsWith(filter)) {
return (!isVoidReturn(invocation) && !isGetter(invocation));
} else {
return !isVoidReturn(invocation);
}
}
if (statement instanceof CtLocalVariable || statement instanceof CtAssignment || statement instanceof CtVariableWrite) {
if (statement instanceof CtNamedElement) {
if (((CtNamedElement) statement).getSimpleName().startsWith("__DSPOT_")) {
return false;
}
}
final CtTypeReference type = ((CtTypedElement) statement).getType();
if (type.getQualifiedName().startsWith(filter)) {
return true;
} else {
try {
return type.getActualClass() == String.class;
} catch (SpoonClassNotFoundException e) {
return false;
}
}
} else {
return false;
}
}
use of spoon.reflect.declaration.CtTypedElement 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.declaration.CtTypedElement in project spoon by INRIA.
the class ContextBuilder method enter.
@SuppressWarnings("unchecked")
void enter(CtElement e, ASTNode node) {
stack.push(new ASTPair(e, node));
if (!(e instanceof CtPackage) || (compilationUnitSpoon.getFile() != null && compilationUnitSpoon.getFile().getName().equals(DefaultJavaPrettyPrinter.JAVA_PACKAGE_DECLARATION))) {
if (compilationunitdeclaration != null && !e.isImplicit()) {
e.setPosition(this.jdtTreeBuilder.getPositionBuilder().buildPositionCtElement(e, node));
}
}
ASTPair pair = stack.peek();
CtElement current = pair.element;
if (current instanceof CtExpression) {
while (!casts.isEmpty()) {
((CtExpression<?>) current).addTypeCast(casts.remove(0));
}
}
if (current instanceof CtStatement && !this.label.isEmpty()) {
((CtStatement) current).setLabel(this.label.pop());
}
try {
if (e instanceof CtTypedElement && !(e instanceof CtConstructorCall) && !(e instanceof CtCatchVariable) && node instanceof Expression) {
if (((CtTypedElement<?>) e).getType() == null) {
((CtTypedElement<Object>) e).setType(this.jdtTreeBuilder.getReferencesBuilder().getTypeReference(((Expression) node).resolvedType));
}
}
} catch (UnsupportedOperationException ignore) {
// For some element, we throw an UnsupportedOperationException when we call setType().
}
}
use of spoon.reflect.declaration.CtTypedElement in project spoon by INRIA.
the class GenericsTest method testClassTypingContext.
@Test
public void testClassTypingContext() throws Exception {
// contract: a ClassTypingContext enables one to perform type resolution of generic types
Factory factory = build(new File("src/test/java/spoon/test/generics/testclasses"));
CtClass<?> ctClassCelebrationLunch = factory.Class().get(CelebrationLunch.class);
CtTypeReference<?> typeReferenceOfDisgust = ctClassCelebrationLunch.filterChildren(new NamedElementFilter<>(CtNamedElement.class, "disgust")).map((CtTypedElement te) -> {
return te.getType();
}).first();
assertEquals("spoon.test.generics.testclasses.CelebrationLunch<java.lang.Integer, java.lang.Long, java.lang.Double>.WeddingLunch<spoon.test.generics.testclasses.Mole>", typeReferenceOfDisgust.toString());
// method WeddingLunch#eatMe
CtMethod<?> tWeddingLunch_eatMe = typeReferenceOfDisgust.getDeclaration().filterChildren((CtNamedElement e) -> "eatMe".equals(e.getSimpleName())).first();
CtClass<?> ctClassLunch = factory.Class().get(Lunch.class);
// method Lunch#eatMe
CtMethod<?> ctClassLunch_eatMe = ctClassLunch.filterChildren((CtNamedElement e) -> "eatMe".equals(e.getSimpleName())).first();
// type of first parameter of method WeddingLunch#eatMe
CtTypeReference<?> ctWeddingLunch_X = tWeddingLunch_eatMe.getParameters().get(0).getType();
// X is the type parameter of WeddingLunch
assertEquals("X", ctWeddingLunch_X.getSimpleName());
// type of first parameter of method Lunch#eatMe
CtTypeReference<?> ctClassLunch_A = ctClassLunch_eatMe.getParameters().get(0).getType();
assertEquals("A", ctClassLunch_A.getSimpleName());
// are these two types same?
ClassTypingContext typingContextOfDisgust = new ClassTypingContext(typeReferenceOfDisgust);
// contract: the class typing context provides its scope
assertSame(typeReferenceOfDisgust.getTypeDeclaration(), typingContextOfDisgust.getAdaptationScope());
// in disgust, X of WeddingLunch is bound to "Model"
assertEquals("spoon.test.generics.testclasses.Mole", typingContextOfDisgust.adaptType(ctWeddingLunch_X).getQualifiedName());
// adapt A to scope of CelebrationLunch<Integer,Long,Double>.WeddingLunch<Mole>
// in disgust, the A of Lunch is bound to "Mole"
assertEquals("spoon.test.generics.testclasses.Mole", typingContextOfDisgust.adaptType(ctClassLunch_A).getQualifiedName());
// I don't understand the goal and utility of this one
assertEquals("java.lang.Double", typingContextOfDisgust.getEnclosingGenericTypeAdapter().adaptType(ctClassLunch_A).getQualifiedName());
// now we resolve those types, but in the context of the declaration, where no concrete types exist
// are these two types same in scope of CelebrationLunch<K,L,M>.WddingLunch<X> class itself
ClassTypingContext sthOftWeddingLunch_X = new ClassTypingContext(typeReferenceOfDisgust.getDeclaration());
// contract: the class typing context provides its scope
assertSame(typeReferenceOfDisgust.getDeclaration(), sthOftWeddingLunch_X.getAdaptationScope());
// in WeddingLunch "X" is still "X"
assertEquals("X", sthOftWeddingLunch_X.adaptType(ctWeddingLunch_X).getQualifiedName());
// in WeddingLunch the "A" from Lunch of is called "X"
assertEquals("X", sthOftWeddingLunch_X.adaptType(ctClassLunch_A).getQualifiedName());
// ?????
// adapt A to scope of enclosing class of CelebrationLunch<K,L,M>.WddingLunch<X>, which is CelebrationLunch<K,L,M>
assertEquals("M", sthOftWeddingLunch_X.getEnclosingGenericTypeAdapter().adaptType(ctClassLunch_A).getQualifiedName());
}
Aggregations