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