Search in sources :

Example 1 with SpoonException

use of spoon.SpoonException in project spoon by INRIA.

the class AnnotationFactory method annotate.

/**
 * Creates/updates an element's annotation value.
 *
 * @param element
 * 		the program element to annotate
 * @param annotationType
 * 		the annotation type
 * @param annotationElementName
 * 		the annotation element name
 * @param value
 * 		the value of the annotation element
 * @return the created/updated annotation
 */
public <A extends Annotation> CtAnnotation<A> annotate(CtElement element, CtTypeReference<A> annotationType, String annotationElementName, Object value) {
    final CtAnnotation<A> annotation = annotate(element, annotationType);
    boolean isArray;
    // try with CT reflection
    CtAnnotationType<A> ctAnnotationType = ((CtAnnotationType<A>) annotation.getAnnotationType().getDeclaration());
    boolean newValue = annotation.getValue(annotationElementName) == null;
    if (ctAnnotationType != null) {
        CtMethod<?> e = ctAnnotationType.getMethod(annotationElementName);
        isArray = (e.getType() instanceof CtArrayTypeReference);
    } else {
        Method m;
        try {
            m = annotation.getAnnotationType().getActualClass().getMethod(annotationElementName, new Class[0]);
        } catch (Exception ex) {
            annotation.addValue(annotationElementName, value);
            return annotation;
        }
        isArray = m.getReturnType().isArray();
    }
    if (isArray || newValue) {
        annotation.addValue(annotationElementName, value);
    } else {
        throw new SpoonException("cannot assign an array to a non-array annotation element");
    }
    return annotation;
}
Also used : SpoonException(spoon.SpoonException) Method(java.lang.reflect.Method) CtMethod(spoon.reflect.declaration.CtMethod) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference) SpoonException(spoon.SpoonException)

Example 2 with SpoonException

use of spoon.SpoonException in project spoon by INRIA.

the class CompilationUnitFactory method getOrCreate.

public CompilationUnit getOrCreate(CtType type) {
    if (type == null) {
        return null;
    }
    if (type.getPosition() != null && type.getPosition().getCompilationUnit() != null) {
        return type.getPosition().getCompilationUnit();
    }
    if (type.isTopLevel()) {
        CtModule module;
        if (type.getPackage() != null && factory.getEnvironment().getComplianceLevel() > 8) {
            module = type.getPackage().getParent(CtModule.class);
        } else {
            module = null;
        }
        File file = this.factory.getEnvironment().getOutputDestinationHandler().getOutputPath(module, type.getPackage(), type).toFile();
        try {
            String path = file.getCanonicalPath();
            CompilationUnit result = this.getOrCreate(path);
            result.setDeclaredPackage(type.getPackage());
            result.addDeclaredType(type);
            type.setPosition(this.factory.createPartialSourcePosition(result));
            return result;
        } catch (IOException e) {
            throw new SpoonException("Cannot get path for file: " + file.getAbsolutePath(), e);
        }
    } else {
        return getOrCreate(type.getTopLevelType());
    }
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) SpoonException(spoon.SpoonException) IOException(java.io.IOException) File(java.io.File) CtModule(spoon.reflect.declaration.CtModule)

Example 3 with SpoonException

use of spoon.SpoonException in project spoon by INRIA.

the class CompilationUnitFactory method getOrCreate.

public CompilationUnit getOrCreate(CtModule module) {
    if (module.getPosition() != null && module.getPosition().getCompilationUnit() != null) {
        return module.getPosition().getCompilationUnit();
    } else {
        File file = this.factory.getEnvironment().getOutputDestinationHandler().getOutputPath(module, null, null).toFile();
        try {
            String path = file.getCanonicalPath();
            CompilationUnit result = this.getOrCreate(path);
            result.setDeclaredModule(module);
            module.setPosition(this.factory.createPartialSourcePosition(result));
            return result;
        } catch (IOException e) {
            throw new SpoonException("Cannot get path for file: " + file.getAbsolutePath(), e);
        }
    }
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) SpoonException(spoon.SpoonException) IOException(java.io.IOException) File(java.io.File)

Example 4 with SpoonException

use of spoon.SpoonException in project spoon by INRIA.

the class CompilationUnitFactory method getOrCreate.

public CompilationUnit getOrCreate(CtPackage ctPackage) {
    if (ctPackage.getPosition() != null && ctPackage.getPosition().getCompilationUnit() != null) {
        return ctPackage.getPosition().getCompilationUnit();
    } else {
        CtModule module;
        if (factory.getEnvironment().getComplianceLevel() > 8) {
            module = ctPackage.getParent(CtModule.class);
        } else {
            module = null;
        }
        File file = this.factory.getEnvironment().getOutputDestinationHandler().getOutputPath(module, ctPackage, null).toFile();
        try {
            String path = file.getCanonicalPath();
            CompilationUnit result = this.getOrCreate(path);
            result.setDeclaredPackage(ctPackage);
            ctPackage.setPosition(this.factory.createPartialSourcePosition(result));
            return result;
        } catch (IOException e) {
            throw new SpoonException("Cannot get path for file: " + file.getAbsolutePath(), e);
        }
    }
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) SpoonException(spoon.SpoonException) IOException(java.io.IOException) File(java.io.File) CtModule(spoon.reflect.declaration.CtModule)

Example 5 with SpoonException

use of spoon.SpoonException in project spoon by INRIA.

the class Refactoring method copyType.

/**
 * See doc in {@link CtType#copyType()}
 */
public static CtType<?> copyType(final CtType<?> type) {
    CtType<?> clone = type.clone();
    String tentativeTypeName = type.getSimpleName() + "Copy";
    while (type.getFactory().Type().get(type.getPackage().getQualifiedName() + "." + tentativeTypeName) != null) {
        tentativeTypeName += "X";
    }
    final String cloneTypeName = tentativeTypeName;
    clone.setSimpleName(cloneTypeName);
    type.getPackage().addType(clone);
    new CtScanner() {

        @Override
        public <T> void visitCtTypeReference(CtTypeReference<T> reference) {
            if (reference.getDeclaration() == null) {
                return;
            }
            if (reference.getDeclaration() == type) {
                reference.setSimpleName(cloneTypeName);
            }
            if (reference.getDeclaration() != clone) {
                throw new SpoonException("post condition broken " + reference);
            }
            super.visitCtTypeReference(reference);
        }

        @Override
        public <T> void visitCtExecutableReference(CtExecutableReference<T> reference) {
            CtExecutable<T> declaration = reference.getDeclaration();
            if (declaration == null) {
                return;
            }
            if (declaration.hasParent(type)) {
                reference.getDeclaringType().setSimpleName(cloneTypeName);
            }
            if (!reference.getDeclaration().hasParent(clone)) {
                throw new SpoonException("post condition broken " + reference);
            }
            super.visitCtExecutableReference(reference);
        }

        @Override
        public <T> void visitCtFieldReference(CtFieldReference<T> reference) {
            CtField<T> declaration = reference.getDeclaration();
            if (declaration == null) {
                return;
            }
            if (declaration.hasParent(type)) {
                reference.getDeclaringType().setSimpleName(cloneTypeName);
            }
            if (reference.getDeclaration() == null || !reference.getDeclaration().hasParent(clone)) {
                throw new SpoonException("post condition broken " + reference);
            }
            super.visitCtFieldReference(reference);
        }
    }.scan(clone);
    return clone;
}
Also used : SpoonException(spoon.SpoonException) CtField(spoon.reflect.declaration.CtField) CtScanner(spoon.reflect.visitor.CtScanner) CtExecutable(spoon.reflect.declaration.CtExecutable)

Aggregations

SpoonException (spoon.SpoonException)57 Test (org.junit.Test)15 Launcher (spoon.Launcher)12 CtMethod (spoon.reflect.declaration.CtMethod)9 CtType (spoon.reflect.declaration.CtType)9 CtElement (spoon.reflect.declaration.CtElement)8 Factory (spoon.reflect.factory.Factory)8 File (java.io.File)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 CtField (spoon.reflect.declaration.CtField)6 URL (java.net.URL)4 CtTypeReference (spoon.reflect.reference.CtTypeReference)4 Collection (java.util.Collection)3 CompilationUnit (spoon.reflect.cu.CompilationUnit)3 CtExecutable (spoon.reflect.declaration.CtExecutable)3 CtParameter (spoon.reflect.declaration.CtParameter)3 CtScanner (spoon.reflect.visitor.CtScanner)3 Filter (spoon.reflect.visitor.Filter)3 FileNotFoundException (java.io.FileNotFoundException)2