From 94a033d83bcab3e8f00c9927533ae9889c3d11ec Mon Sep 17 00:00:00 2001 From: Mark Struberg Date: Thu, 23 Jul 2020 14:05:56 +0200 Subject: [PATCH] OPENJPA-2821 use AsmAdapter for subclassing to write proper java8 code --- .../org/apache/openjpa/enhance/AsmAdaptor.java | 14 ++++++++++++++ .../openjpa/enhance/ManagedClassSubclasser.java | 7 ++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/AsmAdaptor.java b/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/AsmAdaptor.java index 7bef1ef96..6bb26d928 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/AsmAdaptor.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/AsmAdaptor.java @@ -81,6 +81,20 @@ public final class AsmAdaptor { } } + public static void write(BCClass bc, OutputStream os) throws IOException { + if (bc.getMajorVersion() < Java7_MajorVersion) { + bc.write(os); + } + else { + try { + writeJava7(bc, os); + } finally { + os.flush(); + os.close(); + } + } + } + public static byte[] toByteArray(BCClass bc, byte[] returnBytes) throws IOException { if (bc.getMajorVersion() >= Java7_MajorVersion) { returnBytes = toJava7ByteArray(bc, returnBytes); diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/ManagedClassSubclasser.java b/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/ManagedClassSubclasser.java index fab02ab37..0a7d3769c 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/ManagedClassSubclasser.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/ManagedClassSubclasser.java @@ -18,6 +18,7 @@ */ package org.apache.openjpa.enhance; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -142,6 +143,8 @@ public class ManagedClassSubclasser { if (redefine) { enhancer.setRedefine(true); } + + // we need to create subclasses because class retransform doesn't allow to change the interfaces of a previously loaded class enhancer.setCreateSubclass(true); enhancer.setAddDefaultConstructor(true); @@ -276,7 +279,9 @@ public class ManagedClassSubclasser { if (enhancer.isAlreadyRedefined()) ints.add(bc.getType()); else { - map.put(bc.getType(), bc.toByteArray()); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + AsmAdaptor.write(bc, baos); + map.put(bc.getType(), baos.toByteArray()); debugBytecodes(bc); } } else {