use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class DefaultCoreFactory method createPackage.
public CtPackage createPackage() {
CtPackage e = new CtPackageImpl();
e.setFactory(getMainFactory());
e.setParent(getMainFactory().Package().getRootPackage());
return e;
}
use of spoon.reflect.declaration.CtPackage 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.CtPackage in project spoon by INRIA.
the class CommentTest method testCombinedPackageInfoComment.
@Test
public void testCombinedPackageInfoComment() {
Factory f = getSpoonFactory();
CtPackage p = f.Package().get("spoon.test.comment.testclasses");
String l_content = ((JavaOutputProcessor) f.getEnvironment().getDefaultFileGenerator()).getPrinter().printPackageInfo(p);
String EOL = System.getProperty("line.separator");
assertEquals("/* comment1 */" + EOL + "// comment2" + EOL + "/**" + EOL + " * Comment3" + EOL + " */" + EOL + "@java.lang.Deprecated" + EOL + "package spoon.test.comment.testclasses;" + EOL, l_content);
}
use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class SerializableTest method testSerialCtStatement.
@Test
public void testSerialCtStatement() throws Exception {
Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
CtStatement sta2 = (factory).Code().createCodeSnippetStatement("String hello =\"t1\"; System.out.println(hello)").compile();
byte[] ser = ByteSerialization.serialize(sta2);
CtStatement des = (CtStatement) ByteSerialization.deserialize(ser);
String sigBef = sta2.getShortRepresentation();
String sigAf = des.getShortRepresentation();
CtType<?> typeBef = sta2.getParent(CtType.class);
assertNotNull(typeBef);
assertEquals(sigBef, sigAf);
des.setFactory(factory);
String toSBef = sta2.toString();
String toSgAf = des.toString();
assertEquals(toSBef, toSgAf);
CtType<?> typeDes = des.getParent(CtType.class);
assertNotNull(typeDes);
// After deserialization, getDeclaringType throws an exception
CtType<?> decl = typeDes.getDeclaringType();
assertNull(decl);
CtPackage parentOriginal = (CtPackage) typeBef.getParent();
CtPackage parentDeser = (CtPackage) typeDes.getParent();
assertEquals(CtPackage.TOP_LEVEL_PACKAGE_NAME, parentOriginal.getSimpleName());
assertEquals(CtPackage.TOP_LEVEL_PACKAGE_NAME, parentDeser.getSimpleName());
}
use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class ImportScannerImpl method isTypeInCollision.
/**
* Test if the reference can be imported, i.e. test if the importation could lead to a collision.
* @param ref
* @return true if the ref should be imported.
*/
protected boolean isTypeInCollision(CtReference ref, boolean fqnMode) {
if (targetType != null && targetType.getSimpleName().equals(ref.getSimpleName()) && !targetType.equals(ref)) {
return true;
}
try {
CtElement parent;
if (ref instanceof CtTypeReference) {
parent = ref.getParent();
} else {
parent = ref;
}
// i.e. a string, an int, etc.
if (parent instanceof CtLiteral) {
return false;
}
Set<String> localVariablesOfBlock = new HashSet<>();
if (parent instanceof CtField) {
this.fieldAndMethodsNames.add(((CtField) parent).getSimpleName());
} else if (parent instanceof CtMethod) {
this.fieldAndMethodsNames.add(((CtMethod) parent).getSimpleName());
} else {
localVariablesOfBlock = this.lookForLocalVariables(parent);
}
while (!(parent instanceof CtPackage)) {
if ((parent instanceof CtFieldReference) || (parent instanceof CtExecutableReference) || (parent instanceof CtInvocation)) {
CtReference parentType;
if (parent instanceof CtInvocation) {
parentType = ((CtInvocation) parent).getExecutable();
} else {
parentType = (CtReference) parent;
}
LinkedList<String> qualifiedNameTokens = new LinkedList<>();
// we don't want to test the current ref name, as we risk to create field import and make autoreference
if (parentType != parent) {
qualifiedNameTokens.add(parentType.getSimpleName());
}
CtTypeReference typeReference;
if (parent instanceof CtFieldReference) {
typeReference = ((CtFieldReference) parent).getDeclaringType();
} else if (parent instanceof CtExecutableReference) {
typeReference = ((CtExecutableReference) parent).getDeclaringType();
} else {
typeReference = ((CtInvocation) parent).getExecutable().getDeclaringType();
}
if (typeReference != null) {
qualifiedNameTokens.addFirst(typeReference.getSimpleName());
if (typeReference.getPackage() != null) {
StringTokenizer token = new StringTokenizer(typeReference.getPackage().getSimpleName(), CtPackage.PACKAGE_SEPARATOR);
int index = 0;
while (token.hasMoreElements()) {
qualifiedNameTokens.add(index, token.nextToken());
index++;
}
}
}
if (!qualifiedNameTokens.isEmpty()) {
// if the first package name is a variable name somewhere, it could lead to a collision
if (fieldAndMethodsNames.contains(qualifiedNameTokens.getFirst()) || localVariablesOfBlock.contains(qualifiedNameTokens.getFirst())) {
qualifiedNameTokens.removeFirst();
if (fqnMode) {
// for example: spoon.Launcher if a field spoon and another one Launcher exists
if (ref instanceof CtTypeReference) {
if (qualifiedNameTokens.isEmpty()) {
return true;
}
// but if the other package names are not a variable name, it's ok to import
for (int i = 0; i < qualifiedNameTokens.size(); i++) {
String testedToken = qualifiedNameTokens.get(i);
if (!fieldAndMethodsNames.contains(testedToken) && !localVariablesOfBlock.contains(testedToken)) {
return true;
}
}
return false;
// However if it is a static method/field, we always accept to import them in this case
// It is the last possibility for managing import for us
} else {
return true;
}
} else {
// but if the other package names are not a variable name, it's ok to import
for (int i = 0; i < qualifiedNameTokens.size(); i++) {
String testedToken = qualifiedNameTokens.get(i);
if (!fieldAndMethodsNames.contains(testedToken) && !localVariablesOfBlock.contains(testedToken)) {
return false;
}
}
return true;
}
}
}
}
parent = parent.getParent();
}
} catch (ParentNotInitializedException e) {
return false;
}
return false;
}
Aggregations