mirror of
https://github.com/EsotericSoftware/reflectasm.git
synced 2025-02-06 01:00:28 +08:00
Formatting.
This commit is contained in:
parent
d918153ffe
commit
e1f72f3303
@ -7,26 +7,23 @@ import java.security.ProtectionDomain;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
class AccessClassLoader extends ClassLoader {
|
||||
// Weak-references to ClassLoaders, to avoid PermGen memory leaks for example
|
||||
// in AppServers/WebContainters if the reflectasm framework (including this class)
|
||||
// is loaded outside the deployed applications (WAR/EAR) using ReflectASM/Kryo
|
||||
// (exts, user classpath, etc).
|
||||
//
|
||||
// The key is the parent ClassLoader and the value is the AccessClassLoader
|
||||
// Both are weak-referenced in the HashTable.
|
||||
static private final WeakHashMap<ClassLoader, WeakReference<AccessClassLoader>> accessClassLoaders = new WeakHashMap<ClassLoader, WeakReference<AccessClassLoader>>();
|
||||
// Fast-path for classes loaded in the same ClassLoader than this Class
|
||||
// Weak-references to class loaders, to avoid perm gen memory leaks, for example in app servers/web containters if the
|
||||
// reflectasm library (including this class) is loaded outside the deployed applications (WAR/EAR) using ReflectASM/Kryo (exts,
|
||||
// user classpath, etc).
|
||||
// The key is the parent class loader and the value is the AccessClassLoader, both are weak-referenced in the hash table.
|
||||
static private final WeakHashMap<ClassLoader, WeakReference<AccessClassLoader>> accessClassLoaders = new WeakHashMap();
|
||||
|
||||
// Fast-path for classes loaded in the same ClassLoader as this class.
|
||||
static private final ClassLoader selfContextParentClassLoader = getParentClassLoader(AccessClassLoader.class);
|
||||
static private volatile AccessClassLoader selfContextAccessClassLoader = new AccessClassLoader(selfContextParentClassLoader);
|
||||
|
||||
|
||||
static AccessClassLoader get (Class type) {
|
||||
ClassLoader parent = getParentClassLoader(type);
|
||||
// 1. fast-path:
|
||||
if (selfContextParentClassLoader.equals(parent)) {
|
||||
if (selfContextAccessClassLoader==null) {
|
||||
// DCL with volatile semantics
|
||||
synchronized (accessClassLoaders) {
|
||||
if (selfContextAccessClassLoader==null)
|
||||
if (selfContextAccessClassLoader == null) {
|
||||
synchronized (accessClassLoaders) { // DCL with volatile semantics
|
||||
if (selfContextAccessClassLoader == null)
|
||||
selfContextAccessClassLoader = new AccessClassLoader(selfContextParentClassLoader);
|
||||
}
|
||||
}
|
||||
@ -35,10 +32,12 @@ class AccessClassLoader extends ClassLoader {
|
||||
// 2. normal search:
|
||||
synchronized (accessClassLoaders) {
|
||||
WeakReference<AccessClassLoader> ref = accessClassLoaders.get(parent);
|
||||
if (ref!=null) {
|
||||
if (ref != null) {
|
||||
AccessClassLoader accessClassLoader = ref.get();
|
||||
if (accessClassLoader!=null) return accessClassLoader;
|
||||
else accessClassLoaders.remove(parent); // the value has been GC-reclaimed, but still not the key (defensive sanity)
|
||||
if (accessClassLoader != null)
|
||||
return accessClassLoader;
|
||||
else
|
||||
accessClassLoaders.remove(parent); // the value has been GC-reclaimed, but still not the key (defensive sanity)
|
||||
}
|
||||
AccessClassLoader accessClassLoader = new AccessClassLoader(parent);
|
||||
accessClassLoaders.put(parent, new WeakReference<AccessClassLoader>(accessClassLoader));
|
||||
@ -50,18 +49,17 @@ class AccessClassLoader extends ClassLoader {
|
||||
// 1. fast-path:
|
||||
if (selfContextParentClassLoader.equals(parent)) {
|
||||
selfContextAccessClassLoader = null;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// 2. normal search:
|
||||
synchronized (accessClassLoaders) {
|
||||
accessClassLoaders.remove(parent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static int activeAccessClassLoaders() {
|
||||
|
||||
public static int activeAccessClassLoaders () {
|
||||
int sz = accessClassLoaders.size();
|
||||
if (selfContextAccessClassLoader!=null) sz++;
|
||||
if (selfContextAccessClassLoader != null) sz++;
|
||||
return sz;
|
||||
}
|
||||
|
||||
@ -90,8 +88,8 @@ class AccessClassLoader extends ClassLoader {
|
||||
}
|
||||
return defineClass(name, bytes, 0, bytes.length, getClass().getProtectionDomain());
|
||||
}
|
||||
|
||||
private static ClassLoader getParentClassLoader(Class type) {
|
||||
|
||||
private static ClassLoader getParentClassLoader (Class type) {
|
||||
ClassLoader parent = type.getClassLoader();
|
||||
if (parent == null) parent = ClassLoader.getSystemClassLoader();
|
||||
return parent;
|
||||
|
@ -1,16 +1,15 @@
|
||||
|
||||
package com.esotericsoftware.reflectasm;
|
||||
|
||||
import static org.objectweb.asm.Opcodes.*;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import static org.objectweb.asm.Opcodes.*;
|
||||
|
||||
public abstract class ConstructorAccess<T> {
|
||||
|
||||
boolean isNonStaticMemberClass;
|
||||
|
||||
public boolean isNonStaticMemberClass () {
|
||||
@ -69,7 +68,8 @@ public abstract class ConstructorAccess<T> {
|
||||
+ type.getName(), ex);
|
||||
}
|
||||
if (isPrivate) {
|
||||
throw new RuntimeException("Non-static member class cannot be created (the enclosing class constructor is private): " + type.getName());
|
||||
throw new RuntimeException(
|
||||
"Non-static member class cannot be created (the enclosing class constructor is private): " + type.getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,8 @@
|
||||
|
||||
package com.esotericsoftware.reflectasm;
|
||||
|
||||
import static org.objectweb.asm.Opcodes.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
@ -12,8 +14,6 @@ import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
|
||||
import static org.objectweb.asm.Opcodes.*;
|
||||
|
||||
public abstract class MethodAccess {
|
||||
private String[] methodNames;
|
||||
private Class[][] parameterTypes;
|
||||
@ -28,7 +28,7 @@ public abstract class MethodAccess {
|
||||
|
||||
/** Invokes the first method with the specified name and the specified number of arguments. */
|
||||
public Object invoke (Object object, String methodName, Object... args) {
|
||||
return invoke(object, getIndex(methodName, args==null ? 0 : args.length), args);
|
||||
return invoke(object, getIndex(methodName, args == null ? 0 : args.length), args);
|
||||
}
|
||||
|
||||
/** Returns the index of the first method with the specified name. */
|
||||
@ -48,7 +48,7 @@ public abstract class MethodAccess {
|
||||
/** Returns the index of the first method with the specified name and the specified number of arguments. */
|
||||
public int getIndex (String methodName, int paramsCount) {
|
||||
for (int i = 0, n = methodNames.length; i < n; i++)
|
||||
if (methodNames[i].equals(methodName) && parameterTypes[i].length==paramsCount) return i;
|
||||
if (methodNames[i].equals(methodName) && parameterTypes[i].length == paramsCount) return i;
|
||||
throw new IllegalArgumentException("Unable to find public method: " + methodName + " with " + paramsCount + " params.");
|
||||
}
|
||||
|
||||
@ -59,7 +59,7 @@ public abstract class MethodAccess {
|
||||
public Class[][] getParameterTypes () {
|
||||
return parameterTypes;
|
||||
}
|
||||
|
||||
|
||||
public Class[] getReturnTypes () {
|
||||
return returnTypes;
|
||||
}
|
||||
@ -73,8 +73,7 @@ public abstract class MethodAccess {
|
||||
addDeclaredMethodsToList(nextClass, methods);
|
||||
nextClass = nextClass.getSuperclass();
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
recursiveAddInterfaceMethodsToList(type, methods);
|
||||
}
|
||||
|
||||
@ -197,7 +196,8 @@ public abstract class MethodAccess {
|
||||
|
||||
buffer.append(')');
|
||||
buffer.append(Type.getDescriptor(returnType));
|
||||
mv.visitMethodInsn(isInterface ? INVOKEINTERFACE : INVOKEVIRTUAL, classNameInternal, methodName, buffer.toString());
|
||||
mv.visitMethodInsn(isInterface ? INVOKEINTERFACE : INVOKEVIRTUAL, classNameInternal, methodName,
|
||||
buffer.toString());
|
||||
|
||||
switch (Type.getType(returnType).getSort()) {
|
||||
case Type.VOID:
|
||||
@ -264,8 +264,8 @@ public abstract class MethodAccess {
|
||||
throw new RuntimeException("Error constructing method access class: " + accessClassName, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static void addDeclaredMethodsToList(Class type, ArrayList<Method> methods) {
|
||||
|
||||
private static void addDeclaredMethodsToList (Class type, ArrayList<Method> methods) {
|
||||
Method[] declaredMethods = type.getDeclaredMethods();
|
||||
for (int i = 0, n = declaredMethods.length; i < n; i++) {
|
||||
Method method = declaredMethods[i];
|
||||
@ -275,8 +275,8 @@ public abstract class MethodAccess {
|
||||
methods.add(method);
|
||||
}
|
||||
}
|
||||
|
||||
private static void recursiveAddInterfaceMethodsToList(Class interfaceType, ArrayList<Method> methods) {
|
||||
|
||||
private static void recursiveAddInterfaceMethodsToList (Class interfaceType, ArrayList<Method> methods) {
|
||||
addDeclaredMethodsToList(interfaceType, methods);
|
||||
for (Class nextInterface : interfaceType.getInterfaces()) {
|
||||
recursiveAddInterfaceMethodsToList(nextInterface, methods);
|
||||
|
Loading…
Reference in New Issue
Block a user