提交 c702db81 编写于 作者: A alanb

8009645: ClassFileTransformer hooks in ClassLoader no longer required

Reviewed-by: mchung, iris
上级 f63c3865
...@@ -51,7 +51,6 @@ import java.util.Vector; ...@@ -51,7 +51,6 @@ import java.util.Vector;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.WeakHashMap; import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import sun.misc.ClassFileTransformer;
import sun.misc.CompoundEnumeration; import sun.misc.CompoundEnumeration;
import sun.misc.Resource; import sun.misc.Resource;
import sun.misc.URLClassPath; import sun.misc.URLClassPath;
...@@ -669,41 +668,6 @@ public abstract class ClassLoader { ...@@ -669,41 +668,6 @@ public abstract class ClassLoader {
return source; return source;
} }
private Class<?> defineTransformedClass(String name, byte[] b, int off, int len,
ProtectionDomain pd,
ClassFormatError cfe, String source)
throws ClassFormatError
{
// Class format error - try to transform the bytecode and
// define the class again
//
ClassFileTransformer[] transformers =
ClassFileTransformer.getTransformers();
Class<?> c = null;
if (transformers != null) {
for (ClassFileTransformer transformer : transformers) {
try {
// Transform byte code using transformer
byte[] tb = transformer.transform(b, off, len);
c = defineClass1(name, tb, 0, tb.length,
pd, source);
break;
} catch (ClassFormatError cfe2) {
// If ClassFormatError occurs, try next transformer
}
}
}
// Rethrow original ClassFormatError if unable to transform
// bytecode to well-formed
//
if (c == null)
throw cfe;
return c;
}
private void postDefineClass(Class<?> c, ProtectionDomain pd) private void postDefineClass(Class<?> c, ProtectionDomain pd)
{ {
if (pd.getCodeSource() != null) { if (pd.getCodeSource() != null) {
...@@ -783,17 +747,8 @@ public abstract class ClassLoader { ...@@ -783,17 +747,8 @@ public abstract class ClassLoader {
throws ClassFormatError throws ClassFormatError
{ {
protectionDomain = preDefineClass(name, protectionDomain); protectionDomain = preDefineClass(name, protectionDomain);
Class<?> c = null;
String source = defineClassSourceLocation(protectionDomain); String source = defineClassSourceLocation(protectionDomain);
Class<?> c = defineClass1(name, b, off, len, protectionDomain, source);
try {
c = defineClass1(name, b, off, len, protectionDomain, source);
} catch (ClassFormatError cfe) {
c = defineTransformedClass(name, b, off, len, protectionDomain, cfe,
source);
}
postDefineClass(c, protectionDomain); postDefineClass(c, protectionDomain);
return c; return c;
} }
...@@ -881,20 +836,8 @@ public abstract class ClassLoader { ...@@ -881,20 +836,8 @@ public abstract class ClassLoader {
} }
protectionDomain = preDefineClass(name, protectionDomain); protectionDomain = preDefineClass(name, protectionDomain);
Class<?> c = null;
String source = defineClassSourceLocation(protectionDomain); String source = defineClassSourceLocation(protectionDomain);
Class<?> c = defineClass2(name, b, b.position(), len, protectionDomain, source);
try {
c = defineClass2(name, b, b.position(), len, protectionDomain,
source);
} catch (ClassFormatError cfe) {
byte[] tb = new byte[len];
b.get(tb); // get bytes out of byte buffer.
c = defineTransformedClass(name, tb, 0, len, protectionDomain, cfe,
source);
}
postDefineClass(c, protectionDomain); postDefineClass(c, protectionDomain);
return c; return c;
} }
......
...@@ -25,43 +25,31 @@ ...@@ -25,43 +25,31 @@
package sun.misc; package sun.misc;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
/** /**
* This is an abstract base class which is called by java.lang.ClassLoader * This is an abstract base class originally intended to be called by
* when ClassFormatError is thrown inside defineClass(). * {@code java.lang.ClassLoader} when {@code ClassFormatError} is
* * thrown inside {@code defineClass()}. It is no longer hooked into
* The purpose of this class is to allow applications (e.g. Java Plug-in) * {@code ClassLoader} and will be removed in a future release.
* to have a chance to transform the byte code from one form to another
* if necessary.
*
* One application of this class is used by Java Plug-in to transform
* malformed JDK 1.1 class file into a well-formed Java 2 class file
* on-the-fly, so JDK 1.1 applets with malformed class file in the
* Internet may run in Java 2 after transformation.
* *
* @author Stanley Man-Kit Ho * @author Stanley Man-Kit Ho
*/ */
public abstract class ClassFileTransformer @Deprecated
{ public abstract class ClassFileTransformer {
// Singleton of ClassFileTransformer
// private static final List<ClassFileTransformer> transformers
private static ArrayList<ClassFileTransformer> transformerList
= new ArrayList<ClassFileTransformer>(); = new ArrayList<ClassFileTransformer>();
private static ClassFileTransformer[] transformers
= new ClassFileTransformer[0];
/** /**
* Add the class file transformer object. * Add the class file transformer object.
* *
* @param t Class file transformer instance * @param t Class file transformer instance
*/ */
public static void add(ClassFileTransformer t) public static void add(ClassFileTransformer t) {
{ synchronized (transformers) {
synchronized(transformerList) transformers.add(t);
{
transformerList.add(t);
transformers = transformerList.toArray(new ClassFileTransformer[0]);
} }
} }
...@@ -70,13 +58,11 @@ public abstract class ClassFileTransformer ...@@ -70,13 +58,11 @@ public abstract class ClassFileTransformer
* *
* @return ClassFileTransformer object array * @return ClassFileTransformer object array
*/ */
public static ClassFileTransformer[] getTransformers() public static ClassFileTransformer[] getTransformers() {
{ synchronized (transformers) {
// transformers is not intended to be changed frequently, ClassFileTransformer[] result = new ClassFileTransformer[transformers.size()];
// so it is okay to not put synchronized block here return transformers.toArray(result);
// to speed up performance. }
//
return transformers;
} }
...@@ -89,5 +75,5 @@ public abstract class ClassFileTransformer ...@@ -89,5 +75,5 @@ public abstract class ClassFileTransformer
* @return Transformed byte array * @return Transformed byte array
*/ */
public abstract byte[] transform(byte[] b, int off, int len) public abstract byte[] transform(byte[] b, int off, int len)
throws ClassFormatError; throws ClassFormatError;
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册