KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > bytecode > hook > impl > ClassProcessorHelperJDK15


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.bytecode.hook.impl;
5
6 import java.nio.ByteBuffer JavaDoc;
7 import java.security.ProtectionDomain JavaDoc;
8
9 /**
10  * The only purpose of this class is to localize the references to the java.nio.ByteBuffer type so that we don't have to
11  * use reflection
12  */

13 public class ClassProcessorHelperJDK15 {
14
15   public static ByteBuffer JavaDoc defineClass0Pre(ClassLoader JavaDoc caller, String JavaDoc name, ByteBuffer JavaDoc buffer, int off, int len,
16                                            ProtectionDomain JavaDoc pd) {
17
18     final byte[] origBytes;
19     final int offset;
20
21     if (buffer.hasArray()) {
22       origBytes = buffer.array();
23       offset = buffer.arrayOffset() + buffer.position();
24     } else {
25       origBytes = new byte[len];
26       offset = 0;
27       int origPos = buffer.position();
28       try {
29         buffer.get(origBytes);
30       } finally {
31         buffer.position(origPos);
32       }
33     }
34
35     byte[] possiblyTransformed = ClassProcessorHelper.defineClass0Pre(caller, name, origBytes, offset, len, pd);
36
37     if (possiblyTransformed == origBytes) { return buffer; }
38
39     ByteBuffer JavaDoc returnValue;
40     if (buffer.isDirect()) {
41       returnValue = ByteBuffer.allocateDirect(possiblyTransformed.length);
42     } else {
43       returnValue = ByteBuffer.allocate(possiblyTransformed.length);
44     }
45
46     returnValue.put(possiblyTransformed);
47     returnValue.position(0);
48     return returnValue;
49   }
50
51 }
52
Popular Tags