Search in sources :

Example 1 with CtPackageReference

use of spoon.reflect.reference.CtPackageReference in project dspot by STAMP-project.

the class AmplificationHelper method cloneTestMethod.

/**
 * Clones a test method.
 *
 * Performs necessary integration with JUnit and adds timeout.
 *
 * @param method Method to be cloned
 * @param suffix Suffix for the cloned method's name
 * @return The cloned method
 */
private static CtMethod cloneTestMethod(CtMethod method, String suffix) {
    CtMethod cloned_method = cloneMethod(method, suffix);
    CtAnnotation testAnnotation = cloned_method.getAnnotations().stream().filter(annotation -> annotation.toString().contains("Test")).findFirst().orElse(null);
    if (testAnnotation != null) {
        cloned_method.removeAnnotation(testAnnotation);
    }
    testAnnotation = method.getFactory().Core().createAnnotation();
    CtTypeReference<Object> ref = method.getFactory().Core().createTypeReference();
    ref.setSimpleName("Test");
    CtPackageReference refPackage = method.getFactory().Core().createPackageReference();
    refPackage.setSimpleName("org.junit");
    ref.setPackage(refPackage);
    testAnnotation.setAnnotationType(ref);
    Map<String, Object> elementValue = new HashMap<>();
    elementValue.put("timeout", timeOutInMs);
    testAnnotation.setElementValues(elementValue);
    cloned_method.addAnnotation(testAnnotation);
    cloned_method.addThrownType(method.getFactory().Type().createReference(Exception.class));
    return cloned_method;
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtPackageReference(spoon.reflect.reference.CtPackageReference) CtMethod(spoon.reflect.declaration.CtMethod)

Example 2 with CtPackageReference

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

the class JDTTreeBuilderHelper method createModuleExport.

CtPackageExport createModuleExport(ExportsStatement exportsStatement) {
    String packageName = new String(exportsStatement.pkgName);
    int sourceStart = exportsStatement.sourceStart;
    int sourceEnd = exportsStatement.sourceEnd;
    CtPackageReference ctPackageReference = jdtTreeBuilder.references.getPackageReference(packageName);
    CtPackageExport moduleExport = jdtTreeBuilder.getFactory().Module().createPackageExport(ctPackageReference);
    if (exportsStatement.targets != null && exportsStatement.targets.length > 0) {
        List<CtModuleReference> moduleReferences = new ArrayList<>();
        for (ModuleReference moduleReference : exportsStatement.targets) {
            moduleReferences.add(this.jdtTreeBuilder.references.getModuleReference(moduleReference));
        }
        moduleExport.setTargetExport(moduleReferences);
    }
    moduleExport.setPosition(this.jdtTreeBuilder.getPositionBuilder().buildPosition(sourceStart, sourceEnd));
    return moduleExport;
}
Also used : CtPackageExport(spoon.reflect.declaration.CtPackageExport) CtPackageReference(spoon.reflect.reference.CtPackageReference) ModuleReference(org.eclipse.jdt.internal.compiler.ast.ModuleReference) CtModuleReference(spoon.reflect.reference.CtModuleReference) CtModuleReference(spoon.reflect.reference.CtModuleReference) ArrayList(java.util.ArrayList)

Example 3 with CtPackageReference

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

the class ReferenceBuilder method getTypeReference.

/**
 * JDT doesn't return a correct AST with the resolved type of the reference.
 * This method try to build a correct Spoon AST from the name of the JDT
 * reference, thanks to the parsing of the string, the name parameterized from
 * the JDT reference and java convention.
 * Returns a complete Spoon AST when the name is correct, otherwise a spoon type
 * reference with a name that correspond to the name of the JDT type reference.
 */
<T> CtTypeReference<T> getTypeReference(TypeReference ref) {
    CtTypeReference<T> res = null;
    CtTypeReference inner = null;
    final String[] namesParameterized = CharOperation.charArrayToStringArray(ref.getParameterizedTypeName());
    String nameParameterized = CharOperation.toString(ref.getParameterizedTypeName());
    String typeName = CharOperation.toString(ref.getTypeName());
    int index = namesParameterized.length - 1;
    for (; index >= 0; index--) {
        // Start at the end to get the class name first.
        CtTypeReference main = getTypeReference(namesParameterized[index]);
        if (main == null) {
            break;
        }
        if (res == null) {
            res = (CtTypeReference<T>) main;
        } else {
            inner.setDeclaringType((CtTypeReference<?>) main);
        }
        inner = main;
    }
    if (res == null) {
        return this.jdtTreeBuilder.getFactory().Type().createReference(nameParameterized);
    }
    if (inner.getPackage() == null) {
        PackageFactory packageFactory = this.jdtTreeBuilder.getFactory().Package();
        CtPackageReference packageReference = index >= 0 ? packageFactory.getOrCreate(concatSubArray(namesParameterized, index)).getReference() : packageFactory.topLevel();
        inner.setPackage(packageReference);
    }
    if (!res.toString().replace(", ?", ",?").endsWith(nameParameterized)) {
        // verify that we did not match a class that have the same name in a different package
        return this.jdtTreeBuilder.getFactory().Type().createReference(typeName);
    }
    return res;
}
Also used : PackageFactory(spoon.reflect.factory.PackageFactory) CtPackageReference(spoon.reflect.reference.CtPackageReference) CtTypeReference(spoon.reflect.reference.CtTypeReference)

Example 4 with CtPackageReference

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

the class ReferenceBuilder method getPackageReference.

public CtPackageReference getPackageReference(String name) {
    if (name.length() == 0) {
        return this.jdtTreeBuilder.getFactory().Package().topLevel();
    }
    CtPackageReference ref = this.jdtTreeBuilder.getFactory().Core().createPackageReference();
    ref.setSimpleName(name);
    return ref;
}
Also used : CtPackageReference(spoon.reflect.reference.CtPackageReference)

Example 5 with CtPackageReference

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

the class DefaultCoreFactory method createPackageReference.

public CtPackageReference createPackageReference() {
    CtPackageReference e = new CtPackageReferenceImpl();
    e.setFactory(getMainFactory());
    return e;
}
Also used : CtPackageReference(spoon.reflect.reference.CtPackageReference) CtPackageReferenceImpl(spoon.support.reflect.reference.CtPackageReferenceImpl)

Aggregations

CtPackageReference (spoon.reflect.reference.CtPackageReference)18 CtTypeReference (spoon.reflect.reference.CtTypeReference)7 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 Launcher (spoon.Launcher)4 CtClass (spoon.reflect.declaration.CtClass)3 CtField (spoon.reflect.declaration.CtField)3 CtReference (spoon.reflect.reference.CtReference)3 ModuleReference (org.eclipse.jdt.internal.compiler.ast.ModuleReference)2 CtInvocation (spoon.reflect.code.CtInvocation)2 CtElement (spoon.reflect.declaration.CtElement)2 CtImport (spoon.reflect.declaration.CtImport)2 CtPackageExport (spoon.reflect.declaration.CtPackageExport)2 Field (java.lang.reflect.Field)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 ArrayBinding (org.eclipse.jdt.internal.compiler.lookup.ArrayBinding)1