use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class TypeFactory method get.
/**
* Gets a type from its runtime Java class. If the class isn't in the spoon path,
* the class will be build from the Java reflection and will be marked as
* shadow (see {@link spoon.reflect.declaration.CtShadowable}).
*
* @param <T>
* actual type of the class
* @param cl
* the java class: note that this class should be Class<T> but it
* then poses problem when T is a generic type itself
*/
@SuppressWarnings("unchecked")
public <T> CtType<T> get(Class<?> cl) {
final CtType<T> aType = get(cl.getName());
if (aType == null) {
final CtType<T> shadowClass = (CtType<T>) this.shadowCache.get(cl);
if (shadowClass == null) {
CtType<T> newShadowClass;
try {
newShadowClass = new JavaReflectionTreeBuilder(createFactory()).scan((Class<T>) cl);
} catch (Throwable e) {
throw new SpoonClassNotFoundException("cannot create shadow class: " + cl.getName(), e);
}
newShadowClass.setFactory(factory);
newShadowClass.accept(new CtScanner() {
@Override
public void scan(CtElement element) {
if (element != null) {
element.setFactory(factory);
}
}
});
this.shadowCache.put(cl, newShadowClass);
return newShadowClass;
} else {
return shadowClass;
}
}
return aType;
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class RoleHandlerHelper method getRoleHandlerWrtParent.
/**
* @param element the {@link CtElement} whose relation from `element.getParent()` to `element` is needed.
* @return {@link RoleHandler} handling relation from `element.getParent()` to `element`
*/
public static RoleHandler getRoleHandlerWrtParent(CtElement element) {
if (element.isParentInitialized() == false) {
return null;
}
CtElement parent = element.getParent();
CtRole roleInParent = element.getRoleInParent();
if (roleInParent == null) {
return null;
}
return RoleHandlerHelper.getRoleHandler(parent.getClass(), roleInParent);
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class QueueProcessingManager method process.
public void process(Collection<? extends CtElement> elements) {
Processor<?> p;
// copy so that one can reuse the processing manager
// among different processing steps
Queue<Processor<?>> processors = new LinkedList<>(getProcessors());
while ((p = processors.poll()) != null) {
try {
getFactory().getEnvironment().reportProgressMessage(p.getClass().getName());
current = p;
// load the properties
p.init();
p.process();
for (CtElement e : new ArrayList<>(elements)) {
getVisitor().setProcessor(p);
getVisitor().scan(e);
}
} catch (ProcessInterruption ignore) {
} finally {
p.processingDone();
}
}
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class RuntimeProcessingManager method process.
/**
* Recursively processes elements and their children with a given processor.
*/
public void process(Collection<? extends CtElement> elements, Processor<?> processor) {
try {
getFactory().getEnvironment().debugMessage("processing with '" + processor.getClass().getName() + "'...");
current = processor;
Timer.start(processor.getClass().getName());
for (CtElement e : elements) {
process(e, processor);
}
Timer.stop(processor.getClass().getName());
} catch (ProcessInterruption ignored) {
}
}
use of spoon.reflect.declaration.CtElement 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().
}
}
Aggregations