|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.dubbo.common.compiler.support; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import javassist.CannotCompileException; |
| 25 | +import javassist.ClassPool; |
| 26 | +import javassist.CtClass; |
| 27 | +import javassist.CtField; |
| 28 | +import javassist.CtNewConstructor; |
| 29 | +import javassist.CtNewMethod; |
| 30 | +import javassist.LoaderClassPath; |
| 31 | +import javassist.NotFoundException; |
| 32 | + |
| 33 | +/** |
| 34 | + * CtClassBuilder is builder for CtClass |
| 35 | + * <p> |
| 36 | + * contains all the information, including: |
| 37 | + * <p> |
| 38 | + * class name, imported packages, super class name, implemented interfaces, constructors, fields, methods. |
| 39 | + */ |
| 40 | +public class CtClassBuilder { |
| 41 | + |
| 42 | + private String className; |
| 43 | + |
| 44 | + private String superClassName = "java.lang.Object"; |
| 45 | + |
| 46 | + private List<String> imports = new ArrayList<>(); |
| 47 | + |
| 48 | + private Map<String, String> fullNames = new HashMap<>(); |
| 49 | + |
| 50 | + private List<String> ifaces = new ArrayList<>(); |
| 51 | + |
| 52 | + private List<String> constructors = new ArrayList<>(); |
| 53 | + |
| 54 | + private List<String> fields = new ArrayList<>(); |
| 55 | + |
| 56 | + private List<String> methods = new ArrayList<>(); |
| 57 | + |
| 58 | + public String getClassName() { |
| 59 | + return className; |
| 60 | + } |
| 61 | + |
| 62 | + public void setClassName(String className) { |
| 63 | + this.className = className; |
| 64 | + } |
| 65 | + |
| 66 | + public String getSuperClassName() { |
| 67 | + return superClassName; |
| 68 | + } |
| 69 | + |
| 70 | + public void setSuperClassName(String superClassName) { |
| 71 | + this.superClassName = getQualifiedClassName(superClassName); |
| 72 | + } |
| 73 | + |
| 74 | + public List<String> getImports() { |
| 75 | + return imports; |
| 76 | + } |
| 77 | + |
| 78 | + public void addImports(String pkg) { |
| 79 | + int pi = pkg.lastIndexOf('.'); |
| 80 | + if (pi > 0) { |
| 81 | + String pkgName = pkg.substring(0, pi); |
| 82 | + this.imports.add(pkgName); |
| 83 | + if (!pkg.endsWith(".*")) { |
| 84 | + fullNames.put(pkg.substring(pi + 1), pkg); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public List<String> getInterfaces() { |
| 90 | + return ifaces; |
| 91 | + } |
| 92 | + |
| 93 | + public void addInterface(String iface) { |
| 94 | + this.ifaces.add(getQualifiedClassName(iface)); |
| 95 | + } |
| 96 | + |
| 97 | + public List<String> getConstructors() { |
| 98 | + return constructors; |
| 99 | + } |
| 100 | + |
| 101 | + public void addConstructor(String constructor) { |
| 102 | + this.constructors.add(constructor); |
| 103 | + } |
| 104 | + |
| 105 | + public List<String> getFields() { |
| 106 | + return fields; |
| 107 | + } |
| 108 | + |
| 109 | + public void addField(String field) { |
| 110 | + this.fields.add(field); |
| 111 | + } |
| 112 | + |
| 113 | + public List<String> getMethods() { |
| 114 | + return methods; |
| 115 | + } |
| 116 | + |
| 117 | + public void addMethod(String method) { |
| 118 | + this.methods.add(method); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * get full qualified class name |
| 123 | + * |
| 124 | + * @param className super class name, maybe qualified or not |
| 125 | + */ |
| 126 | + protected String getQualifiedClassName(String className) { |
| 127 | + if (className.contains(".")) { |
| 128 | + return className; |
| 129 | + } |
| 130 | + |
| 131 | + if (fullNames.containsKey(className)) { |
| 132 | + return fullNames.get(className); |
| 133 | + } |
| 134 | + |
| 135 | + return ClassUtils.forName(imports.toArray(new String[0]), className).getName(); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * build CtClass object |
| 140 | + */ |
| 141 | + public CtClass build(ClassLoader classLoader) throws NotFoundException, CannotCompileException { |
| 142 | + ClassPool pool = new ClassPool(true); |
| 143 | + pool.appendClassPath(new LoaderClassPath(classLoader)); |
| 144 | + |
| 145 | + // create class |
| 146 | + CtClass ctClass = pool.makeClass(className, pool.get(superClassName)); |
| 147 | + |
| 148 | + // add imported packages |
| 149 | + imports.stream().forEach(pool::importPackage); |
| 150 | + |
| 151 | + // add implemented interfaces |
| 152 | + for (String iface : ifaces) { |
| 153 | + ctClass.addInterface(pool.get(iface)); |
| 154 | + } |
| 155 | + |
| 156 | + // add constructors |
| 157 | + for (String constructor : constructors) { |
| 158 | + ctClass.addConstructor(CtNewConstructor.make(constructor, ctClass)); |
| 159 | + } |
| 160 | + |
| 161 | + // add fields |
| 162 | + for (String field : fields) { |
| 163 | + ctClass.addField(CtField.make(field, ctClass)); |
| 164 | + } |
| 165 | + |
| 166 | + // add methods |
| 167 | + for (String method : methods) { |
| 168 | + ctClass.addMethod(CtNewMethod.make(method, ctClass)); |
| 169 | + } |
| 170 | + |
| 171 | + return ctClass; |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments