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 java.lang.invoke;
27
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.lang.reflect.Method;
31 import java.security.ProtectionDomain;
32 import java.util.concurrent.atomic.AtomicInteger;
33 import java.util.logging.Level;
34 import java.util.logging.Logger;
35 import jdk.internal.org.objectweb.asm.*;
36 import static jdk.internal.org.objectweb.asm.Opcodes.*;
37 import sun.misc.Unsafe;
38
39 /**
40 * InnerClassLambdaMetafactory
41 */
42 /*non-public*/ final class InnerClassLambdaMetafactory extends AbstractValidatingLambdaMetafactory {
43 private static final int CLASSFILE_VERSION = 51;
44 private static final Type TYPE_VOID = Type.getType(void.class);
45 private static final String METHOD_DESCRIPTOR_VOID = Type.getMethodDescriptor(Type.VOID_TYPE);
46 private static final String NAME_MAGIC_ACCESSOR_IMPL = "java/lang/invoke/MagicLambdaImpl";
47 private static final String NAME_SERIALIZABLE = "java/io/Serializable";
48 private static final String NAME_CTOR = "<init>";
49
50 //Serialization support
51 private static final String NAME_SERIALIZED_LAMBDA = "com/oracle/java/lang/invoke/SerializedLambdaImpl";
52 private static final String DESCR_METHOD_WRITE_REPLACE = "()Ljava/lang/Object;";
53 private static final String NAME_METHOD_WRITE_REPLACE = "writeReplace";
54 private static final String NAME_OBJECT = "java/lang/Object";
175
176 /***** Serialization not yet supported
177 if (isSerializable) {
178 String samMethodName = samInfo.getName();
179 Type samType = Type.getType(samBase);
180 generateSerializationMethod(samType, samMethodName);
181 }
182 ******/
183
184 cw.visitEnd();
185
186 // Define the generated class in this VM.
187
188 final byte[] classBytes = cw.toByteArray();
189
190 if (System.getProperty("debug.dump.generated") != null) {
191 System.out.printf("Loaded: %s (%d bytes) %n", lambdaClassName, classBytes.length);
192 try (FileOutputStream fos = new FileOutputStream(lambdaClassName.replace('/', '.') + ".class")) {
193 fos.write(classBytes);
194 } catch (IOException ex) {
195 Logger.getLogger(InnerClassLambdaMetafactory.class.getName()).log(Level.SEVERE, null, ex);
196 }
197 }
198
199 ClassLoader loader = targetClass.getClassLoader();
200 ProtectionDomain pd = (loader == null) ? null : targetClass.getProtectionDomain();
201 return (Class<? extends T>) Unsafe.getUnsafe().defineClass(lambdaClassName, classBytes, 0, classBytes.length, loader, pd);
202 }
203
204 /**
205 * Generate the constructor for the class
206 */
207 private void generateConstructor() {
208 // Generate constructor
209 MethodVisitor ctor = cw.visitMethod(ACC_PUBLIC, NAME_CTOR, constructorDesc, null, null);
210 ctor.visitCode();
211 ctor.visitVarInsn(ALOAD, 0);
212 ctor.visitMethodInsn(INVOKESPECIAL, NAME_MAGIC_ACCESSOR_IMPL, NAME_CTOR, METHOD_DESCRIPTOR_VOID);
213 int lvIndex = 0;
214 for (int i = 0; i < argTypes.length; i++) {
215 ctor.visitVarInsn(ALOAD, 0);
|
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 java.lang.invoke;
27
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.lang.reflect.Method;
31 import java.security.ProtectionDomain;
32 import java.util.concurrent.atomic.AtomicInteger;
33 import sun.util.logging.PlatformLogger;
34 import jdk.internal.org.objectweb.asm.*;
35 import static jdk.internal.org.objectweb.asm.Opcodes.*;
36 import sun.misc.Unsafe;
37
38 /**
39 * InnerClassLambdaMetafactory
40 */
41 /*non-public*/ final class InnerClassLambdaMetafactory extends AbstractValidatingLambdaMetafactory {
42 private static final int CLASSFILE_VERSION = 51;
43 private static final Type TYPE_VOID = Type.getType(void.class);
44 private static final String METHOD_DESCRIPTOR_VOID = Type.getMethodDescriptor(Type.VOID_TYPE);
45 private static final String NAME_MAGIC_ACCESSOR_IMPL = "java/lang/invoke/MagicLambdaImpl";
46 private static final String NAME_SERIALIZABLE = "java/io/Serializable";
47 private static final String NAME_CTOR = "<init>";
48
49 //Serialization support
50 private static final String NAME_SERIALIZED_LAMBDA = "com/oracle/java/lang/invoke/SerializedLambdaImpl";
51 private static final String DESCR_METHOD_WRITE_REPLACE = "()Ljava/lang/Object;";
52 private static final String NAME_METHOD_WRITE_REPLACE = "writeReplace";
53 private static final String NAME_OBJECT = "java/lang/Object";
174
175 /***** Serialization not yet supported
176 if (isSerializable) {
177 String samMethodName = samInfo.getName();
178 Type samType = Type.getType(samBase);
179 generateSerializationMethod(samType, samMethodName);
180 }
181 ******/
182
183 cw.visitEnd();
184
185 // Define the generated class in this VM.
186
187 final byte[] classBytes = cw.toByteArray();
188
189 if (System.getProperty("debug.dump.generated") != null) {
190 System.out.printf("Loaded: %s (%d bytes) %n", lambdaClassName, classBytes.length);
191 try (FileOutputStream fos = new FileOutputStream(lambdaClassName.replace('/', '.') + ".class")) {
192 fos.write(classBytes);
193 } catch (IOException ex) {
194 PlatformLogger.getLogger(InnerClassLambdaMetafactory.class.getName()).severe(ex.getMessage(), ex);
195 }
196 }
197
198 ClassLoader loader = targetClass.getClassLoader();
199 ProtectionDomain pd = (loader == null) ? null : targetClass.getProtectionDomain();
200 return (Class<? extends T>) Unsafe.getUnsafe().defineClass(lambdaClassName, classBytes, 0, classBytes.length, loader, pd);
201 }
202
203 /**
204 * Generate the constructor for the class
205 */
206 private void generateConstructor() {
207 // Generate constructor
208 MethodVisitor ctor = cw.visitMethod(ACC_PUBLIC, NAME_CTOR, constructorDesc, null, null);
209 ctor.visitCode();
210 ctor.visitVarInsn(ALOAD, 0);
211 ctor.visitMethodInsn(INVOKESPECIAL, NAME_MAGIC_ACCESSOR_IMPL, NAME_CTOR, METHOD_DESCRIPTOR_VOID);
212 int lvIndex = 0;
213 for (int i = 0; i < argTypes.length; i++) {
214 ctor.visitVarInsn(ALOAD, 0);
|