From e1f72f3303d6814b12778636da76756a6c0c02f4 Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Sun, 26 Jan 2014 14:33:19 +0100 Subject: [PATCH] Formatting. --- .../reflectasm/AccessClassLoader.java | 46 +++++++++---------- .../reflectasm/ConstructorAccess.java | 8 ++-- .../reflectasm/MethodAccess.java | 24 +++++----- 3 files changed, 38 insertions(+), 40 deletions(-) diff --git a/src/com/esotericsoftware/reflectasm/AccessClassLoader.java b/src/com/esotericsoftware/reflectasm/AccessClassLoader.java index 202f168..96ff571 100644 --- a/src/com/esotericsoftware/reflectasm/AccessClassLoader.java +++ b/src/com/esotericsoftware/reflectasm/AccessClassLoader.java @@ -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> accessClassLoaders = new WeakHashMap>(); - // 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> 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 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)); @@ -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; diff --git a/src/com/esotericsoftware/reflectasm/ConstructorAccess.java b/src/com/esotericsoftware/reflectasm/ConstructorAccess.java index 6f1aa13..ed5638c 100644 --- a/src/com/esotericsoftware/reflectasm/ConstructorAccess.java +++ b/src/com/esotericsoftware/reflectasm/ConstructorAccess.java @@ -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 { - boolean isNonStaticMemberClass; public boolean isNonStaticMemberClass () { @@ -69,7 +68,8 @@ public abstract class ConstructorAccess { + 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()); } } diff --git a/src/com/esotericsoftware/reflectasm/MethodAccess.java b/src/com/esotericsoftware/reflectasm/MethodAccess.java index e02bd6f..9145812 100644 --- a/src/com/esotericsoftware/reflectasm/MethodAccess.java +++ b/src/com/esotericsoftware/reflectasm/MethodAccess.java @@ -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 methods) { + + private static void addDeclaredMethodsToList (Class type, ArrayList 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 methods) { + + private static void recursiveAddInterfaceMethodsToList (Class interfaceType, ArrayList methods) { addDeclaredMethodsToList(interfaceType, methods); for (Class nextInterface : interfaceType.getInterfaces()) { recursiveAddInterfaceMethodsToList(nextInterface, methods);