diff --git a/core/src/main/java/hudson/util/SubClassGenerator.java b/core/src/main/java/hudson/util/SubClassGenerator.java new file mode 100644 index 0000000000000000000000000000000000000000..bd919b4e30d70b1feb7fb9887a7dac08585df260 --- /dev/null +++ b/core/src/main/java/hudson/util/SubClassGenerator.java @@ -0,0 +1,87 @@ +/* + * The MIT License + * + * Copyright (c) 2010, InfraDNA, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package hudson.util; + +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Type; + +import java.lang.reflect.Constructor; + +import static org.objectweb.asm.Opcodes.*; + +/** + * Generates a new class that just defines constructors into the super types. + * + * @author Kohsuke Kawaguchi + */ +public class SubClassGenerator extends ClassLoader { + public SubClassGenerator(ClassLoader parent) { + super(parent); + } + + public Class generate(Class base, String name) { + ClassWriter cw = new ClassWriter(false,false);//? + cw.visit(49, ACC_PUBLIC, name, null, + Type.getInternalName(base),null); + + for (Constructor c : base.getDeclaredConstructors()) { + Class[] et = c.getExceptionTypes(); + String[] exceptions = new String[et.length]; + for (int i = 0; i < et.length; i++) + exceptions[i] = Type.getInternalName(et[i]); + + String methodDescriptor = getMethodDescriptor(c); + MethodVisitor m = cw.visitMethod(c.getModifiers(), "", methodDescriptor, null, exceptions); + m.visitCode(); + + int index=1; + m.visitVarInsn(ALOAD,0); + for (Class param : c.getParameterTypes()) { + Type t = Type.getType(param); + m.visitVarInsn(t.getOpcode(ILOAD), index); + index += t.getSize(); + } + m.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(base), "", methodDescriptor); + m.visitInsn(RETURN); + m.visitMaxs(index,index); + m.visitEnd(); + } + + cw.visitEnd(); + byte[] image = cw.toByteArray(); + + return defineClass(name, image, 0, image.length).asSubclass(base); + } + + private String getMethodDescriptor(Constructor c) { + StringBuilder buf = new StringBuilder(); + buf.append('('); + for (Class p : c.getParameterTypes()) + buf.append(Type.getDescriptor(p)); + + buf.append(")V"); + return buf.toString(); + } +} diff --git a/core/src/test/java/hudson/util/SubClassGeneratorTest.java b/core/src/test/java/hudson/util/SubClassGeneratorTest.java new file mode 100644 index 0000000000000000000000000000000000000000..b9fe3d3ada3c73e49e03d49528ca14cdc93d3bef --- /dev/null +++ b/core/src/test/java/hudson/util/SubClassGeneratorTest.java @@ -0,0 +1,54 @@ +/* + * The MIT License + * + * Copyright (c) 2010, InfraDNA, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package hudson.util; + +import junit.framework.TestCase; + +/** + * + * @author Kohsuke Kawaguchi + */ +public class SubClassGeneratorTest extends TestCase { + public static class Foo { + String s; + double x; + int y; + public Foo() {} + public Foo(String s) {this.s=s;} + public Foo(double x, int y) {this.x=x;this.y=y;} + } + public void testFoo() throws Exception { + Class c = new SubClassGenerator(getClass().getClassLoader()).generate(Foo.class, "12345"); + assertEquals("12345",c.getName()); + + c.newInstance(); + + Foo f = c.getConstructor(String.class).newInstance("aaa"); + assertEquals("aaa",f.s); + + f = c.getConstructor(double.class,int.class).newInstance(1.0,3); + assertEquals(1.0,f.x); + assertEquals(3,f.y); + } +}