-
Notifications
You must be signed in to change notification settings - Fork 26.5k
refactor javassist compiler: extract class CtClassBuilder #3424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
632a754
850248e
1f4e34b
7102eea
13a3038
b744dca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| /* | ||
| * 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.dubbo.common.compiler.support; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import javassist.CannotCompileException; | ||
| import javassist.ClassPool; | ||
| import javassist.CtClass; | ||
| import javassist.CtField; | ||
| import javassist.CtNewConstructor; | ||
| import javassist.CtNewMethod; | ||
| import javassist.NotFoundException; | ||
|
|
||
| /** | ||
| * Javassist class info contains all the information used for JavassistCompiler, including: | ||
| * <p> | ||
| * class name, imported packages, super class name, implemented interfaces, constructors, fields, methods. | ||
| */ | ||
| public class JavassistClassInfo { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As once this class gets initialized then it is not getting modified, I would recommend to use builder for this and then have only getter or make it immutable. have addInterface, addConstructor keep it the door open for accidental bug. What do you say?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you give more detailed information what shall i do, could be some pseudo code or java code.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it was saying having setter for collections e.g. setImports(List imports), setInterfaces(List ifaces) .
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks |
||
|
|
||
| private String className; | ||
|
|
||
| private String superClassName = "java.lang.Object"; | ||
|
|
||
| private List<String> imports = new ArrayList<>(); | ||
|
|
||
| private Map<String, String> fullNames = new HashMap<>(); | ||
|
|
||
| private List<String> ifaces = new ArrayList<>(); | ||
|
|
||
| private List<String> constructors = new ArrayList<>(); | ||
|
|
||
| private List<String> fields = new ArrayList<>(); | ||
|
|
||
| private List<String> methods = new ArrayList<>(); | ||
|
|
||
| public String getClassName() { | ||
| return className; | ||
| } | ||
|
|
||
| public void setClassName(String className) { | ||
| this.className = className; | ||
| } | ||
|
|
||
| public String getSuperClassName() { | ||
| return superClassName; | ||
| } | ||
|
|
||
| public void setSuperClassName(String superClassName) { | ||
| this.superClassName = getQualifiedClassName(superClassName); | ||
| } | ||
|
|
||
| public List<String> getImports() { | ||
| return imports; | ||
| } | ||
|
|
||
| public void addImports(String pkg) { | ||
| int pi = pkg.lastIndexOf('.'); | ||
| if (pi > 0) { | ||
| String pkgName = pkg.substring(0, pi); | ||
| this.imports.add(pkgName); | ||
| if (!pkg.endsWith(".*")) { | ||
| fullNames.put(pkg.substring(pi + 1), pkg); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public List<String> getInterfaces() { | ||
| return ifaces; | ||
| } | ||
|
|
||
| public void addInterface(String iface) { | ||
| this.ifaces.add(getQualifiedClassName(iface)); | ||
| } | ||
|
|
||
| public List<String> getConstructors() { | ||
| return constructors; | ||
| } | ||
|
|
||
| public void addConstructor(String constructor) { | ||
| this.constructors.add(constructor); | ||
| } | ||
|
|
||
| public List<String> getFields() { | ||
| return fields; | ||
| } | ||
|
|
||
| public void addField(String field) { | ||
| this.fields.add(field); | ||
| } | ||
|
|
||
| public List<String> getMethods() { | ||
| return methods; | ||
| } | ||
|
|
||
| public void addMethod(String method) { | ||
| this.methods.add(method); | ||
| } | ||
|
|
||
| /** | ||
| * get full qualified class name | ||
| * | ||
| * @param className super class name, maybe qualified or not | ||
| */ | ||
| protected String getQualifiedClassName(String className) { | ||
| if (className.contains(".")) { | ||
| return className; | ||
| } | ||
|
|
||
| if (fullNames.containsKey(className)) { | ||
| return fullNames.get(className); | ||
| } | ||
|
|
||
| return ClassUtils.forName(imports.toArray(new String[0]), className).getName(); | ||
| } | ||
|
|
||
| /** | ||
| * build CtClass object | ||
| */ | ||
| public CtClass build(ClassPool context) throws NotFoundException, CannotCompileException { | ||
| CtClass ctClass = context.makeClass(className, context.get(superClassName)); | ||
| imports.stream().forEach(context::importPackage); | ||
| for (String iface : ifaces) { | ||
| ctClass.addInterface(context.get(iface)); | ||
| } | ||
|
|
||
| for (String constructor : constructors) { | ||
| ctClass.addConstructor(CtNewConstructor.make("public " + constructor, ctClass)); | ||
| } | ||
|
|
||
| for (String field : fields) { | ||
| ctClass.addField(CtField.make("private " + field, ctClass)); | ||
| } | ||
|
|
||
| for (String method : methods) { | ||
| ctClass.addMethod(CtNewMethod.make("public " + method, ctClass)); | ||
| } | ||
|
|
||
| return ctClass; | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.