Search in sources :

Example 6 with CtPackage

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;
}
Also used : CtPackage(spoon.reflect.declaration.CtPackage) CtPackageImpl(spoon.support.reflect.declaration.CtPackageImpl)

Example 7 with CtPackage

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().
    }
}
Also used : CtExpression(spoon.reflect.code.CtExpression) CtStatement(spoon.reflect.code.CtStatement) CtConstructorCall(spoon.reflect.code.CtConstructorCall) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) CtExpression(spoon.reflect.code.CtExpression) CtElement(spoon.reflect.declaration.CtElement) CtPackage(spoon.reflect.declaration.CtPackage) CtTypedElement(spoon.reflect.declaration.CtTypedElement) CtCatchVariable(spoon.reflect.code.CtCatchVariable)

Example 8 with CtPackage

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);
}
Also used : DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) CtPackage(spoon.reflect.declaration.CtPackage) Test(org.junit.Test)

Example 9 with CtPackage

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());
}
Also used : DefaultCoreFactory(spoon.support.DefaultCoreFactory) CtStatement(spoon.reflect.code.CtStatement) Factory(spoon.reflect.factory.Factory) DefaultCoreFactory(spoon.support.DefaultCoreFactory) CtPackage(spoon.reflect.declaration.CtPackage) FactoryImpl(spoon.reflect.factory.FactoryImpl) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Example 10 with CtPackage

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;
}
Also used : ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) CtElement(spoon.reflect.declaration.CtElement) CtFieldReference(spoon.reflect.reference.CtFieldReference) LinkedList(java.util.LinkedList) CtInvocation(spoon.reflect.code.CtInvocation) StringTokenizer(java.util.StringTokenizer) CtLiteral(spoon.reflect.code.CtLiteral) CtReference(spoon.reflect.reference.CtReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtField(spoon.reflect.declaration.CtField) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtPackage(spoon.reflect.declaration.CtPackage) CtMethod(spoon.reflect.declaration.CtMethod) HashSet(java.util.HashSet)

Aggregations

CtPackage (spoon.reflect.declaration.CtPackage)53 Test (org.junit.Test)29 Launcher (spoon.Launcher)21 Factory (spoon.reflect.factory.Factory)13 CtType (spoon.reflect.declaration.CtType)12 File (java.io.File)8 CtClass (spoon.reflect.declaration.CtClass)6 CtElement (spoon.reflect.declaration.CtElement)6 CtTypeReference (spoon.reflect.reference.CtTypeReference)6 ArrayList (java.util.ArrayList)4 CtAnnotation (spoon.reflect.declaration.CtAnnotation)4 CtMethod (spoon.reflect.declaration.CtMethod)4 CtModule (spoon.reflect.declaration.CtModule)4 HashSet (java.util.HashSet)3 StringTokenizer (java.util.StringTokenizer)3 SpoonAPI (spoon.SpoonAPI)3 CtStatement (spoon.reflect.code.CtStatement)3 CtField (spoon.reflect.declaration.CtField)3 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)3 Annotation (java.lang.annotation.Annotation)2