File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\SubClassGenerator.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2010, InfraDNA, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util;
25
26 import hudson.PluginManager.UberClassLoader;
27 import jenkins.model.Jenkins;
28 import org.kohsuke.asm3.ClassWriter;
29 import org.kohsuke.asm3.MethodVisitor;
30 import org.kohsuke.asm3.Type;
31
32 import java.lang.reflect.Constructor;
33
34 import static org.kohsuke.asm3.Opcodes.*;
35
36 /**
37 * Generates a new class that just defines constructors into the super types.
38 *
39 * @author Kohsuke Kawaguchi
40 */
41 public class SubClassGenerator extends ClassLoader {
42 public SubClassGenerator(ClassLoader parent) {
43 super(parent);
44 }
45
46 public <T> Class<? extends T> generate(Class<T> base, String name) {
47 ClassWriter cw = new ClassWriter(0);//?
48 cw.visit(49, ACC_PUBLIC, name.replace('.', '/'), null,
49 Type.getInternalName(base),null);
50
51 for (Constructor c : base.getDeclaredConstructors()) {
52 Class[] et = c.getExceptionTypes();
53 String[] exceptions = new String[et.length];
54 for (int i = 0; i < et.length; i++)
55 exceptions[i] = Type.getInternalName(et[i]);
56
57 String methodDescriptor = getMethodDescriptor(c);
58 MethodVisitor m = cw.visitMethod(c.getModifiers(), "<init>", methodDescriptor, null, exceptions);
59 m.visitCode();
60
61 int index=1;
62 m.visitVarInsn(ALOAD,0);
63 for (Class param : c.getParameterTypes()) {
64 Type t = Type.getType(param);
65 m.visitVarInsn(t.getOpcode(ILOAD), index);
66 index += t.getSize();
67 }
68 m.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(base), "<init>", methodDescriptor);
69 m.visitInsn(RETURN);
70 m.visitMaxs(index,index);
71 m.visitEnd();
72 }
73
74 cw.visitEnd();
75 byte[] image = cw.toByteArray();
76
77 Class<? extends T> c = defineClass(name, image, 0, image.length).asSubclass(base);
78
79 Jenkins h = Jenkins.getInstance();
80 if (h!=null) // null only during tests.
81 ((UberClassLoader)h.pluginManager.uberClassLoader).addNamedClass(name,c); // can't change the field type as it breaks binary compatibility
82
83 return c;
84 }
85
86 private String getMethodDescriptor(Constructor c) {
87 StringBuilder buf = new StringBuilder();
88 buf.append('(');
89 for (Class p : c.getParameterTypes())
90 buf.append(Type.getDescriptor(p));
91
92 buf.append(")V");
93 return buf.toString();
94 }
95 }
96