Skip to content

Commit

Permalink
Formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanSweet committed Jan 26, 2014
1 parent d918153 commit e1f72f3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 40 deletions.
46 changes: 22 additions & 24 deletions src/com/esotericsoftware/reflectasm/AccessClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,23 @@
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);
}
}
Expand All @@ -35,10 +32,12 @@ static AccessClassLoader get (Class type) {
// 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));
Expand All @@ -50,18 +49,17 @@ public static void remove (ClassLoader parent) {
// 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;
}

Expand Down Expand Up @@ -90,8 +88,8 @@ Class<?> defineClass (String name, byte[] bytes) throws ClassFormatError {
}
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;
Expand Down
8 changes: 4 additions & 4 deletions src/com/esotericsoftware/reflectasm/ConstructorAccess.java
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down Expand Up @@ -69,7 +68,8 @@ static public <T> ConstructorAccess<T> get (Class<T> type) {
+ 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());
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/com/esotericsoftware/reflectasm/MethodAccess.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,8 +14,6 @@
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;
Expand All @@ -28,7 +28,7 @@ public Object invoke (Object object, String methodName, Class[] paramTypes, Obje

/** 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. */
Expand All @@ -48,7 +48,7 @@ public int getIndex (String methodName, Class... paramTypes) {
/** 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.");
}

Expand All @@ -59,7 +59,7 @@ public String[] getMethodNames () {
public Class[][] getParameterTypes () {
return parameterTypes;
}

public Class[] getReturnTypes () {
return returnTypes;
}
Expand All @@ -73,8 +73,7 @@ static public MethodAccess get (Class type) {
addDeclaredMethodsToList(nextClass, methods);
nextClass = nextClass.getSuperclass();
}
}
else {
} else {
recursiveAddInterfaceMethodsToList(type, methods);
}

Expand Down Expand Up @@ -197,7 +196,8 @@ static public MethodAccess get (Class type) {

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:
Expand Down Expand Up @@ -264,8 +264,8 @@ static public MethodAccess get (Class type) {
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];
Expand All @@ -275,8 +275,8 @@ private static void addDeclaredMethodsToList(Class type, ArrayList<Method> metho
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);
Expand Down

0 comments on commit e1f72f3

Please sign in to comment.