use of spoon.support.visitor.java.JavaReflectionTreeBuilder 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;
}
Aggregations