Fixed performance issues with implementation of BA-to-BI mapping
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / codegen / util / JavassistUtils.xtend
1 package org.opendaylight.controller.sal.binding.codegen.util
2
3 import javassist.CtClass
4 import javassist.CtMethod
5 import javassist.ClassPool
6 import java.util.Arrays
7 import static com.google.common.base.Preconditions.*;
8 import javassist.CtField
9 import javassist.Modifier
10 import javassist.NotFoundException
11 import javassist.LoaderClassPath
12 import javassist.ClassClassPath
13
14 class JavassistUtils {
15
16     ClassPool classPool
17
18     new(ClassPool pool) {
19         classPool = pool;
20     }
21
22     def void method(CtClass it, Class<?> returnType, String name, Class<?> parameter, MethodGenerator function1) {
23         val method = new CtMethod(returnType.asCtClass, name, Arrays.asList(parameter.asCtClass), it);
24         function1.process(method);
25         it.addMethod(method);
26     }
27     
28         def void method(CtClass it, Class<?> returnType, String name, Class<?> parameter1, Class<?> parameter2,  MethodGenerator function1) {
29         val method = new CtMethod(returnType.asCtClass, name, Arrays.asList(parameter1.asCtClass,parameter2.asCtClass), it);
30         function1.process(method);
31         it.addMethod(method);
32     }
33     
34     
35     def void staticMethod(CtClass it, Class<?> returnType, String name, Class<?> parameter, MethodGenerator function1) {
36         val method = new CtMethod(returnType.asCtClass, name, Arrays.asList(parameter.asCtClass), it);
37         function1.process(method);
38         it.addMethod(method);
39     }
40
41     def void implementMethodsFrom(CtClass target, CtClass source, MethodGenerator function1) {
42         for (method : source.methods) {
43             if (method.declaringClass == source) {
44                 val redeclaredMethod = new CtMethod(method, target, null);
45                 function1.process(redeclaredMethod);
46                 target.addMethod(redeclaredMethod);
47             }
48         }
49     }
50
51     def CtClass createClass(String fqn, ClassGenerator cls) {
52         val target = classPool.makeClass(fqn);
53         cls.process(target);
54         return target;
55     }
56
57     def CtClass createClass(String fqn, CtClass superInterface, ClassGenerator cls) {
58         val target = classPool.makeClass(fqn);
59         target.implementsType(superInterface);
60         cls.process(target);
61         return target;
62     }
63
64     def void implementsType(CtClass it, CtClass supertype) {
65         checkArgument(supertype.interface, "Supertype must be interface");
66         addInterface(supertype);
67     }
68
69     def asCtClass(Class<?> class1) {
70         classPool.get(class1);
71     }
72
73     def CtField field(CtClass it, String name, Class<?> returnValue) {
74         val field = new CtField(returnValue.asCtClass, name, it);
75         field.modifiers = Modifier.PUBLIC
76         addField(field);
77         return field;
78     }
79
80     def get(ClassPool pool, Class<?> cls) {
81         try {
82             return pool.get(cls.name)
83         } catch (NotFoundException e) {
84             pool.appendClassPath(new LoaderClassPath(cls.classLoader));
85             try {
86                 return pool.get(cls.name)
87
88             } catch (NotFoundException ef) {
89                 pool.appendClassPath(new ClassClassPath(cls));
90                 return pool.get(cls.name)
91             }
92         }
93     }
94 }