8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.invoke.anon;
27
28 import java.io.IOException;
29 import java.lang.reflect.InvocationTargetException;
30 import java.lang.reflect.Method;
31 import sun.misc.IOUtils;
32
33 /**
34 * Anonymous class loader. Will load any valid classfile, producing
35 * a {@link Class} metaobject, without installing that class in the
36 * system dictionary. Therefore, {@link Class#forName(String)} will never
37 * produce a reference to an anonymous class.
38 * <p>
39 * The access permissions of the anonymous class are borrowed from
40 * a <em>host class</em>. The new class behaves as if it were an
41 * inner class of the host class. It can access the host's private
42 * members, if the creator of the class loader has permission to
43 * do so (or to create accessible reflective objects).
44 * <p>
45 * When the anonymous class is loaded, elements of its constant pool
46 * can be patched to new values. This provides a hook to pre-resolve
47 * named classes in the constant pool to other classes, including
48 * anonymous ones. Also, string constants can be pre-resolved to
49 * any reference. (The verifier treats non-string, non-class reference
50 * constants as plain objects.)
51 * <p>
208 }
209
210 private static void noJVMSupport() {
211 throw new UnsupportedOperationException("no JVM support for anonymous classes");
212 }
213
214
215 private static native Class<?> loadClassInternal(Class<?> hostClass,
216 byte[] classFile,
217 Object[] patchArray);
218
219 public static byte[] readClassFile(Class<?> templateClass) throws IOException {
220 String templateName = templateClass.getName();
221 int lastDot = templateName.lastIndexOf('.');
222 java.net.URL url = templateClass.getResource(templateName.substring(lastDot+1)+".class");
223 java.net.URLConnection connection = url.openConnection();
224 int contentLength = connection.getContentLength();
225 if (contentLength < 0)
226 throw new IOException("invalid content length "+contentLength);
227
228 return IOUtils.readFully(connection.getInputStream(), contentLength, true);
229 }
230 }
|
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.invoke.anon;
27
28 import java.io.EOFException;
29 import java.io.IOException;
30 import java.lang.reflect.InvocationTargetException;
31 import java.lang.reflect.Method;
32
33 /**
34 * Anonymous class loader. Will load any valid classfile, producing
35 * a {@link Class} metaobject, without installing that class in the
36 * system dictionary. Therefore, {@link Class#forName(String)} will never
37 * produce a reference to an anonymous class.
38 * <p>
39 * The access permissions of the anonymous class are borrowed from
40 * a <em>host class</em>. The new class behaves as if it were an
41 * inner class of the host class. It can access the host's private
42 * members, if the creator of the class loader has permission to
43 * do so (or to create accessible reflective objects).
44 * <p>
45 * When the anonymous class is loaded, elements of its constant pool
46 * can be patched to new values. This provides a hook to pre-resolve
47 * named classes in the constant pool to other classes, including
48 * anonymous ones. Also, string constants can be pre-resolved to
49 * any reference. (The verifier treats non-string, non-class reference
50 * constants as plain objects.)
51 * <p>
208 }
209
210 private static void noJVMSupport() {
211 throw new UnsupportedOperationException("no JVM support for anonymous classes");
212 }
213
214
215 private static native Class<?> loadClassInternal(Class<?> hostClass,
216 byte[] classFile,
217 Object[] patchArray);
218
219 public static byte[] readClassFile(Class<?> templateClass) throws IOException {
220 String templateName = templateClass.getName();
221 int lastDot = templateName.lastIndexOf('.');
222 java.net.URL url = templateClass.getResource(templateName.substring(lastDot+1)+".class");
223 java.net.URLConnection connection = url.openConnection();
224 int contentLength = connection.getContentLength();
225 if (contentLength < 0)
226 throw new IOException("invalid content length "+contentLength);
227
228 byte[] b = connection.getInputStream().readAllBytes();
229 if (b.length != contentLength)
230 throw new EOFException("Expected:" + contentLength + ", read:" + b.length);
231
232 return b;
233 }
234 }
|