Search in sources :

Example 21 with ReloadableType

use of org.springsource.loaded.ReloadableType in project spring-loaded by spring-projects.

the class ReflectiveInterceptor method jlrFieldGetBoolean.

public static boolean jlrFieldGetBoolean(Field field, Object target) throws IllegalAccessException {
    Class<?> clazz = field.getDeclaringClass();
    ReloadableType rtype = getRType(clazz);
    if (rtype == null) {
        field = asAccessibleField(field, target, true);
        return field.getBoolean(target);
    } else {
        asAccessibleField(field, target, false);
        typeCheckFieldGet(field, boolean.class);
        Object value = rtype.getField(target, field.getName(), Modifier.isStatic(field.getModifiers()));
        return ((Boolean) value).booleanValue();
    }
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) AccessibleObject(java.lang.reflect.AccessibleObject)

Example 22 with ReloadableType

use of org.springsource.loaded.ReloadableType in project spring-loaded by spring-projects.

the class ReflectiveInterceptor method getRType.

/**
	 * Access and return the ReloadableType field on a specified class.
	 *
	 * @param clazz the class for which to discover the reloadable type
	 * @return the reloadable type for the class, or null if not reloadable
	 */
public static ReloadableType getRType(Class<?> clazz) {
    //		ReloadableType rtype = null;
    WeakReference<ReloadableType> ref = classToRType.get(clazz);
    ReloadableType rtype = null;
    if (ref != null) {
        rtype = ref.get();
    }
    if (rtype == null) {
        if (!theOldWay) {
            // 'theOldWay' attempts to grab the field from the type via reflection.  This usually works except
            // in cases where the class is not resolved yet since it can cause the class to resolve and its
            // static initializer to run.  This was happening on a grails compile where the compiler is
            // loading dependencies (but not initializing them).  Instead we can use this route of
            // discovering the type registry and locating the reloadable type.  This does some map lookups
            // which may be a problem, but once discovered, it is cached in the weak ref so that shouldn't
            // be an ongoing perf problem.
            // TODO testcases for something that is reloaded without having been resolved
            ClassLoader cl = clazz.getClassLoader();
            TypeRegistry tr = TypeRegistry.getTypeRegistryFor(cl);
            if (tr == null) {
                classToRType.put(clazz, ReloadableType.NOT_RELOADABLE_TYPE_REF);
            } else {
                rtype = tr.getReloadableType(clazz.getName().replace('.', '/'));
                if (rtype == null) {
                    classToRType.put(clazz, ReloadableType.NOT_RELOADABLE_TYPE_REF);
                } else {
                    classToRType.put(clazz, new WeakReference<ReloadableType>(rtype));
                }
            }
        } else {
            // need to work it out
            Field rtypeField;
            try {
                //				System.out.println("discovering field for " + clazz.getName());
                // TODO cache somewhere - will need a clazz>Field cache
                rtypeField = clazz.getDeclaredField(Constants.fReloadableTypeFieldName);
            } catch (NoSuchFieldException nsfe) {
                classToRType.put(clazz, ReloadableType.NOT_RELOADABLE_TYPE_REF);
                // expensive if constantly discovering this
                return null;
            }
            try {
                rtypeField.setAccessible(true);
                rtype = (ReloadableType) rtypeField.get(null);
                if (rtype == null) {
                    classToRType.put(clazz, ReloadableType.NOT_RELOADABLE_TYPE_REF);
                    throw new ReloadException("ReloadableType field '" + Constants.fReloadableTypeFieldName + "' is 'null' on type " + clazz.getName());
                } else {
                    classToRType.put(clazz, new WeakReference<ReloadableType>(rtype));
                }
            } catch (Exception e) {
                throw new ReloadException("Unable to access ReloadableType field '" + Constants.fReloadableTypeFieldName + "' on type " + clazz.getName(), e);
            }
        }
    } else if (rtype == ReloadableType.NOT_RELOADABLE_TYPE) {
        return null;
    }
    return rtype;
}
Also used : Field(java.lang.reflect.Field) ReloadableType(org.springsource.loaded.ReloadableType) ReloadException(org.springsource.loaded.ReloadException) TypeRegistry(org.springsource.loaded.TypeRegistry) ReloadException(org.springsource.loaded.ReloadException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 23 with ReloadableType

use of org.springsource.loaded.ReloadableType in project spring-loaded by spring-projects.

the class ReflectiveInterceptor method jlClassGetDeclaredMethods.

public static Method[] jlClassGetDeclaredMethods(Class<?> clazz) {
    ReloadableType rtype = getRType(clazz);
    if (rtype == null) {
        // Not reloadable...
        return clazz.getDeclaredMethods();
    } else {
        MethodProvider methods = MethodProvider.create(rtype);
        List<Invoker> invokers = methods.getDeclaredMethods();
        Method[] javaMethods = new Method[invokers.size()];
        for (int i = 0; i < javaMethods.length; i++) {
            javaMethods[i] = invokers.get(i).createJavaMethod();
        }
        return javaMethods;
    }
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) Method(java.lang.reflect.Method)

Example 24 with ReloadableType

use of org.springsource.loaded.ReloadableType in project spring-loaded by spring-projects.

the class ReflectiveInterceptor method jlrMethodGetDeclaredAnnotations.

public static Annotation[] jlrMethodGetDeclaredAnnotations(Method method) {
    ReloadableType rtype = getReloadableTypeIfHasBeenReloaded(method.getDeclaringClass());
    if (rtype == null) {
        //Nothing special to be done
        return method.getDeclaredAnnotations();
    } else {
        // Method could have changed...
        CurrentLiveVersion clv = rtype.getLiveVersion();
        MethodMember methodMember = rtype.getCurrentMethod(method.getName(), Type.getMethodDescriptor(method));
        if (MethodMember.isCatcher(methodMember)) {
            if (clv.getExecutorMethod(methodMember) != null) {
                throw new IllegalStateException();
            }
            return method.getDeclaredAnnotations();
        }
        Method executor = clv.getExecutorMethod(methodMember);
        return executor.getAnnotations();
    }
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) CurrentLiveVersion(org.springsource.loaded.CurrentLiveVersion) Method(java.lang.reflect.Method) MethodMember(org.springsource.loaded.MethodMember)

Example 25 with ReloadableType

use of org.springsource.loaded.ReloadableType in project spring-loaded by spring-projects.

the class ReflectiveInterceptor method jlrFieldGetDeclaredAnnotations.

public static Annotation[] jlrFieldGetDeclaredAnnotations(Field field) {
    ReloadableType rtype = getReloadableTypeIfHasBeenReloaded(field.getDeclaringClass());
    if (rtype == null) {
        //Nothing special to be done
        return field.getDeclaredAnnotations();
    } else {
        // Field could have changed...
        CurrentLiveVersion clv = rtype.getLiveVersion();
        Field executor;
        try {
            executor = clv.getExecutorField(field.getName());
            return executor.getAnnotations();
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
}
Also used : Field(java.lang.reflect.Field) ReloadableType(org.springsource.loaded.ReloadableType) CurrentLiveVersion(org.springsource.loaded.CurrentLiveVersion) ReloadException(org.springsource.loaded.ReloadException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

ReloadableType (org.springsource.loaded.ReloadableType)375 Test (org.junit.Test)320 TypeRegistry (org.springsource.loaded.TypeRegistry)287 Result (org.springsource.loaded.test.infra.Result)106 Method (java.lang.reflect.Method)37 InvocationTargetException (java.lang.reflect.InvocationTargetException)26 TestClassloaderWithRewriting (org.springsource.loaded.test.infra.TestClassloaderWithRewriting)22 Ignore (org.junit.Ignore)17 CurrentLiveVersion (org.springsource.loaded.CurrentLiveVersion)10 AccessibleObject (java.lang.reflect.AccessibleObject)9 ResultException (org.springsource.loaded.test.infra.ResultException)9 MethodMember (org.springsource.loaded.MethodMember)8 Field (java.lang.reflect.Field)6 TypeDescriptor (org.springsource.loaded.TypeDescriptor)6 IOException (java.io.IOException)5 Annotation (java.lang.annotation.Annotation)4 ZipEntry (java.util.zip.ZipEntry)4 ZipFile (java.util.zip.ZipFile)4 LoadtimeInstrumentationPlugin (org.springsource.loaded.LoadtimeInstrumentationPlugin)4 File (java.io.File)3