Search in sources :

Example 6 with CtTypeReference

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

the class TypeFactory method createWildcardStaticTypeMemberReference.

/**
 * Create a wildcard reference to a simple type
 */
public CtTypeReference createWildcardStaticTypeMemberReference(CtTypeReference typeReference) {
    CtTypeReference ref = factory.Core().createWildcardStaticTypeMemberReference();
    ref.setFactory(this.factory);
    if (typeReference.getDeclaringType() != null) {
        ref.setDeclaringType(typeReference.getDeclaringType().clone());
    }
    if (typeReference.getPackage() != null) {
        ref.setPackage(typeReference.getPackage().clone());
    }
    ref.setSimpleName(typeReference.getSimpleName());
    return ref;
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference)

Example 7 with CtTypeReference

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

the class ExecutableFactory method createReference.

/**
 * Creates an executable reference from its signature, as defined by the
 * executable reference's toString.
 */
public <T> CtExecutableReference<T> createReference(String signature) {
    CtExecutableReference<T> executableRef = factory.Core().createExecutableReference();
    String type = signature.substring(0, signature.indexOf(" "));
    String declaringType = signature.substring(signature.indexOf(" ") + 1, signature.indexOf(CtExecutable.EXECUTABLE_SEPARATOR));
    String executableName = signature.substring(signature.indexOf(CtExecutable.EXECUTABLE_SEPARATOR) + 1, signature.indexOf("("));
    executableRef.setSimpleName(executableName);
    executableRef.setDeclaringType(factory.Type().createReference(declaringType));
    CtTypeReference<T> typeRef = factory.Type().createReference(type);
    executableRef.setType(typeRef);
    String parameters = signature.substring(signature.indexOf("(") + 1, signature.indexOf(")"));
    List<CtTypeReference<?>> params = new ArrayList<>(PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
    StringTokenizer t = new StringTokenizer(parameters, ",");
    while (t.hasMoreTokens()) {
        String paramType = t.nextToken();
        params.add(factory.Type().createReference(paramType));
    }
    executableRef.setParameters(params);
    return executableRef;
}
Also used : StringTokenizer(java.util.StringTokenizer) CtTypeReference(spoon.reflect.reference.CtTypeReference) ArrayList(java.util.ArrayList)

Example 8 with CtTypeReference

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

the class JDTTreeBuilderHelper method createModuleProvidedService.

CtProvidedService createModuleProvidedService(ProvidesStatement providesStatement) {
    int sourceStart = providesStatement.sourceStart;
    int sourceEnd = providesStatement.sourceEnd;
    CtTypeReference provideService = this.jdtTreeBuilder.references.getTypeReference(providesStatement.serviceInterface);
    List<CtTypeReference> implementations = new ArrayList<>();
    for (TypeReference typeReference : providesStatement.implementations) {
        implementations.add(this.jdtTreeBuilder.references.getTypeReference(typeReference));
    }
    CtProvidedService providedService = this.jdtTreeBuilder.getFactory().Module().createProvidedService(provideService);
    providedService.setImplementationTypes(implementations);
    providedService.setPosition(this.jdtTreeBuilder.getPositionBuilder().buildPosition(sourceStart, sourceEnd));
    return providedService;
}
Also used : CtProvidedService(spoon.reflect.declaration.CtProvidedService) CtTypeReference(spoon.reflect.reference.CtTypeReference) ArrayList(java.util.ArrayList) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference) UnionTypeReference(org.eclipse.jdt.internal.compiler.ast.UnionTypeReference) CtTypeReference(spoon.reflect.reference.CtTypeReference)

Example 9 with CtTypeReference

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

the class JDTTreeBuilderHelper method createUsedService.

CtUsedService createUsedService(UsesStatement usesStatement) {
    CtTypeReference typeReference = this.jdtTreeBuilder.references.getTypeReference(usesStatement.serviceInterface);
    CtUsedService usedService = this.jdtTreeBuilder.getFactory().Module().createUsedService(typeReference);
    usedService.setPosition(this.jdtTreeBuilder.getPositionBuilder().buildPosition(usesStatement.sourceStart, usesStatement.sourceEnd));
    return usedService;
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) CtUsedService(spoon.reflect.declaration.CtUsedService)

Example 10 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference 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)

Aggregations

CtTypeReference (spoon.reflect.reference.CtTypeReference)121 Test (org.junit.Test)56 Launcher (spoon.Launcher)43 Factory (spoon.reflect.factory.Factory)32 CtMethod (spoon.reflect.declaration.CtMethod)26 CtType (spoon.reflect.declaration.CtType)24 ArrayList (java.util.ArrayList)22 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)17 CtClass (spoon.reflect.declaration.CtClass)16 CtField (spoon.reflect.declaration.CtField)16 List (java.util.List)15 CtElement (spoon.reflect.declaration.CtElement)14 CtInvocation (spoon.reflect.code.CtInvocation)13 CtReference (spoon.reflect.reference.CtReference)13 CtParameter (spoon.reflect.declaration.CtParameter)12 CtExpression (spoon.reflect.code.CtExpression)11 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)11 CtTypeParameterReference (spoon.reflect.reference.CtTypeParameterReference)11 CtStatement (spoon.reflect.code.CtStatement)9 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)9