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;
}
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());
}
}
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);
}
}
}
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);
}
}
}
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;
}
Aggregations