OPENJPA-2911 using BCClassWriter

The standard ASM ClassWriter uses Class.forName but does not leverage
our custom ClassLoader which we use to isolate away classes during enhancement.
BCClassWriter extends ClassWriter to use the given ClassLoader.
This commit is contained in:
Mark Struberg 2023-07-03 18:05:26 +02:00
parent 2b9b024f27
commit e718fc8b51
3 changed files with 60 additions and 36 deletions

View File

@ -173,40 +173,6 @@ public class AsmSpi9 implements AsmSpi {
return baos.toByteArray();
}
private static class BCClassWriter extends ClassWriter {
private final ClassLoader _loader;
BCClassWriter(int flags, ClassLoader loader) {
super(flags);
_loader = loader;
}
@Override
protected String getCommonSuperClass(String type1, String type2) {
Class<?> class1;
Class<?> class2;
try {
class1 = _loader.loadClass(type1.replace('/', '.'));
class2 = _loader.loadClass(type2.replace('/', '.'));
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
if (class1.isAssignableFrom(class2)) {
return type1;
}
if (class2.isAssignableFrom(class1)) {
return type2;
}
if (class1.isInterface() || class2.isInterface()) {
return "java/lang/Object";
}
do {
class1 = class1.getSuperclass();
} while (!class1.isAssignableFrom(class2));
return class1.getName().replace('.', '/');
}
}
private static class EnhancedStatusException extends RuntimeException {
private static final long serialVersionUID = 1L;

View File

@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.openjpa.enhance.asm;
import org.apache.xbean.asm9.ClassWriter;
/**
* @author <a href="mailto:struberg@apache.org">Mark Struberg</a>
*/
public class BCClassWriter extends ClassWriter {
private final ClassLoader _loader;
public BCClassWriter(int flags, ClassLoader loader) {
super(flags);
_loader = loader;
}
@Override
protected String getCommonSuperClass(String type1, String type2) {
Class<?> class1;
Class<?> class2;
try {
class1 = _loader.loadClass(type1.replace('/', '.'));
class2 = _loader.loadClass(type2.replace('/', '.'));
}
catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
if (class1.isAssignableFrom(class2)) {
return type1;
}
if (class2.isAssignableFrom(class1)) {
return type2;
}
if (class1.isInterface() || class2.isInterface()) {
return "java/lang/Object";
}
do {
class1 = class1.getSuperclass();
} while (!class1.isAssignableFrom(class2));
return class1.getName().replace('.', '/');
}
}

View File

@ -25,6 +25,7 @@ import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Optional;
import org.apache.openjpa.enhance.asm.BCClassWriter;
import org.apache.xbean.asm9.ClassReader;
import org.apache.xbean.asm9.ClassWriter;
import org.apache.xbean.asm9.Opcodes;
@ -94,7 +95,7 @@ public final class AsmHelper {
*/
public static ClassWriterTracker toClassWriter(BCClass bcClass) {
ClassReader cr = new ClassReader(bcClass.toByteArray());
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
ClassWriter cw = new BCClassWriter(ClassWriter.COMPUTE_FRAMES, bcClass.getClassLoader());
cr.accept(cw, 0); // 0 -> don't skip anything
ClassWriterTracker cwt = new ClassWriterTracker(cw, bcClass.getClassLoader());
cwt.setName(bcClass.getName());
@ -142,7 +143,7 @@ public final class AsmHelper {
Method readMethod = BCClass.class.getDeclaredMethod("read", InputStream.class, ClassLoader.class);
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
ClassWriter cw = new BCClassWriter(ClassWriter.COMPUTE_FRAMES, bcClass.getClassLoader());
cnt.getClassNode().accept(cw);
final byte[] classBytes = cw.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(classBytes);