Search in sources :

Example 1 with JavaReflectionTreeBuilder

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&lt;T&gt; 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;
}
Also used : CtType(spoon.reflect.declaration.CtType) JavaReflectionTreeBuilder(spoon.support.visitor.java.JavaReflectionTreeBuilder) CtElement(spoon.reflect.declaration.CtElement) SpoonClassNotFoundException(spoon.support.SpoonClassNotFoundException) CtNewClass(spoon.reflect.code.CtNewClass) CtClass(spoon.reflect.declaration.CtClass) CtScanner(spoon.reflect.visitor.CtScanner)

Aggregations

CtNewClass (spoon.reflect.code.CtNewClass)1 CtClass (spoon.reflect.declaration.CtClass)1 CtElement (spoon.reflect.declaration.CtElement)1 CtType (spoon.reflect.declaration.CtType)1 CtScanner (spoon.reflect.visitor.CtScanner)1 SpoonClassNotFoundException (spoon.support.SpoonClassNotFoundException)1 JavaReflectionTreeBuilder (spoon.support.visitor.java.JavaReflectionTreeBuilder)1