--- old/src/share/classes/java/lang/reflect/Method.java 2011-03-21 15:50:27.000000000 -0700 +++ new/src/share/classes/java/lang/reflect/Method.java 2011-03-21 15:50:26.000000000 -0700 @@ -83,11 +83,6 @@ // potentially many Method objects pointing to it.) private Method root; - // More complicated security check cache needed here than for - // Class.newInstance() and Constructor.newInstance() - private Class securityCheckCache; - private Class securityCheckTargetClassCache; - // Generics infrastructure private String getGenericSignature() {return signature;} @@ -402,28 +397,28 @@ */ public String toString() { try { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); int mod = getModifiers() & Modifier.methodModifiers(); if (mod != 0) { - sb.append(Modifier.toString(mod) + " "); + sb.append(Modifier.toString(mod)).append(' '); } - sb.append(Field.getTypeName(getReturnType()) + " "); - sb.append(Field.getTypeName(getDeclaringClass()) + "."); - sb.append(getName() + "("); + sb.append(Field.getTypeName(getReturnType())).append(' '); + sb.append(Field.getTypeName(getDeclaringClass())).append('.'); + sb.append(getName()).append('('); Class[] params = parameterTypes; // avoid clone for (int j = 0; j < params.length; j++) { sb.append(Field.getTypeName(params[j])); if (j < (params.length - 1)) - sb.append(","); + sb.append(','); } - sb.append(")"); + sb.append(')'); Class[] exceptions = exceptionTypes; // avoid clone if (exceptions.length > 0) { sb.append(" throws "); for (int k = 0; k < exceptions.length; k++) { sb.append(exceptions[k].getName()); if (k < (exceptions.length - 1)) - sb.append(","); + sb.append(','); } } return sb.toString(); @@ -475,15 +470,15 @@ StringBuilder sb = new StringBuilder(); int mod = getModifiers() & Modifier.methodModifiers(); if (mod != 0) { - sb.append(Modifier.toString(mod) + " "); + sb.append(Modifier.toString(mod)).append(' '); } TypeVariable[] typeparms = getTypeParameters(); if (typeparms.length > 0) { boolean first = true; - sb.append("<"); + sb.append('<'); for(TypeVariable typeparm: typeparms) { if (!first) - sb.append(","); + sb.append(','); // Class objects can't occur here; no need to test // and call Class.getName(). sb.append(typeparm.toString()); @@ -494,10 +489,11 @@ Type genRetType = getGenericReturnType(); sb.append( ((genRetType instanceof Class)? - Field.getTypeName((Class)genRetType):genRetType.toString()) + " "); + Field.getTypeName((Class)genRetType):genRetType.toString())) + .append(' '); - sb.append(Field.getTypeName(getDeclaringClass()) + "."); - sb.append(getName() + "("); + sb.append(Field.getTypeName(getDeclaringClass())).append('.'); + sb.append(getName()).append('('); Type[] params = getGenericParameterTypes(); for (int j = 0; j < params.length; j++) { String param = (params[j] instanceof Class)? @@ -507,9 +503,9 @@ param = param.replaceFirst("\\[\\]$", "..."); sb.append(param); if (j < (params.length - 1)) - sb.append(","); + sb.append(','); } - sb.append(")"); + sb.append(')'); Type[] exceptions = getGenericExceptionTypes(); if (exceptions.length > 0) { sb.append(" throws "); @@ -518,7 +514,7 @@ ((Class)exceptions[k]).getName(): exceptions[k].toString()); if (k < (exceptions.length - 1)) - sb.append(","); + sb.append(','); } } return sb.toString(); @@ -591,26 +587,15 @@ if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { Class caller = Reflection.getCallerClass(1); - Class targetClass = ((obj == null || !Modifier.isProtected(modifiers)) - ? clazz - : obj.getClass()); - - boolean cached; - synchronized (this) { - cached = (securityCheckCache == caller) - && (securityCheckTargetClassCache == targetClass); - } - if (!cached) { - Reflection.ensureMemberAccess(caller, clazz, obj, modifiers); - synchronized (this) { - securityCheckCache = caller; - securityCheckTargetClassCache = targetClass; - } - } + + checkAccess(caller, clazz, obj, modifiers); } } - if (methodAccessor == null) acquireMethodAccessor(); - return methodAccessor.invoke(obj, args); + MethodAccessor ma = methodAccessor; // read volatile + if (ma == null) { + ma = acquireMethodAccessor(); + } + return ma.invoke(obj, args); } /** @@ -654,18 +639,20 @@ // (though not efficient) to generate more than one MethodAccessor // for a given Method. However, avoiding synchronization will // probably make the implementation more scalable. - private void acquireMethodAccessor() { + private MethodAccessor acquireMethodAccessor() { // First check to see if one has been created yet, and take it // if so MethodAccessor tmp = null; if (root != null) tmp = root.getMethodAccessor(); if (tmp != null) { methodAccessor = tmp; - return; + } else { + // Otherwise fabricate one and propagate it up to the root + tmp = reflectionFactory.newMethodAccessor(this); + setMethodAccessor(tmp); } - // Otherwise fabricate one and propagate it up to the root - tmp = reflectionFactory.newMethodAccessor(this); - setMethodAccessor(tmp); + + return tmp; } // Returns MethodAccessor for this Method object, not looking up