Skip to content

Commit

Permalink
code format (apache#2554)
Browse files Browse the repository at this point in the history
  • Loading branch information
x-ultimate authored and jerrick-zhu committed Sep 26, 2018
1 parent 755bbf9 commit 00b718c
Showing 1 changed file with 101 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package org.apache.dubbo.common.bytecode;

import org.apache.dubbo.common.utils.ClassHelper;
import org.apache.dubbo.common.utils.ReflectUtils;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
Expand All @@ -26,8 +29,6 @@
import javassist.CtNewMethod;
import javassist.LoaderClassPath;
import javassist.NotFoundException;
import org.apache.dubbo.common.utils.ClassHelper;
import org.apache.dubbo.common.utils.ReflectUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
Expand All @@ -46,14 +47,18 @@
* ClassGenerator
*/
public final class ClassGenerator {

private static final AtomicLong CLASS_NAME_COUNTER = new AtomicLong(0);
private static final String SIMPLE_NAME_TAG = "<init>";
private static final Map<ClassLoader, ClassPool> POOL_MAP = new ConcurrentHashMap<ClassLoader, ClassPool>(); //ClassLoader - ClassPool
private ClassPool mPool;
private CtClass mCtc;
private String mClassName, mSuperClass;
private String mClassName;
private String mSuperClass;
private Set<String> mInterfaces;
private List<String> mFields, mConstructors, mMethods;
private List<String> mFields;
private List<String> mConstructors;
private List<String> mMethods;
private Map<String, Method> mCopyMethods; // <method desc,method instance>
private Map<String, Constructor<?>> mCopyConstructors; // <constructor desc,constructor instance>
private boolean mDefaultConstructor = false;
Expand All @@ -78,8 +83,9 @@ public static boolean isDynamicClass(Class<?> cl) {
}

public static ClassPool getClassPool(ClassLoader loader) {
if (loader == null)
if (loader == null) {
return ClassPool.getDefault();
}

ClassPool pool = POOL_MAP.get(loader);
if (pool == null) {
Expand All @@ -92,12 +98,22 @@ public static ClassPool getClassPool(ClassLoader loader) {

private static String modifier(int mod) {
StringBuilder modifier = new StringBuilder();
if (Modifier.isPublic(mod)) modifier.append("public");
if (Modifier.isProtected(mod)) modifier.append("protected");
if (Modifier.isPrivate(mod)) modifier.append("private");
if (Modifier.isPublic(mod)) {
modifier.append("public");
}
if (Modifier.isProtected(mod)) {
modifier.append("protected");
}
if (Modifier.isPrivate(mod)) {
modifier.append("private");
}

if (Modifier.isStatic(mod)) modifier.append(" static");
if (Modifier.isVolatile(mod)) modifier.append(" volatile");
if (Modifier.isStatic(mod)) {
modifier.append(" static");
}
if (Modifier.isVolatile(mod)) {
modifier.append(" volatile");
}

return modifier.toString();
}
Expand All @@ -112,8 +128,9 @@ public ClassGenerator setClassName(String name) {
}

public ClassGenerator addInterface(String cn) {
if (mInterfaces == null)
if (mInterfaces == null) {
mInterfaces = new HashSet<String>();
}
mInterfaces.add(cn);
return this;
}
Expand All @@ -133,8 +150,9 @@ public ClassGenerator setSuperClass(Class<?> cl) {
}

public ClassGenerator addField(String code) {
if (mFields == null)
if (mFields == null) {
mFields = new ArrayList<String>();
}
mFields.add(code);
return this;
}
Expand All @@ -156,8 +174,9 @@ public ClassGenerator addField(String name, int mod, Class<?> type, String def)
}

public ClassGenerator addMethod(String code) {
if (mMethods == null)
if (mMethods == null) {
mMethods = new ArrayList<String>();
}
mMethods.add(code);
return this;
}
Expand All @@ -166,22 +185,25 @@ public ClassGenerator addMethod(String name, int mod, Class<?> rt, Class<?>[] pt
return addMethod(name, mod, rt, pts, null, body);
}

public ClassGenerator addMethod(String name, int mod, Class<?> rt, Class<?>[] pts, Class<?>[] ets, String body) {
public ClassGenerator addMethod(String name, int mod, Class<?> rt, Class<?>[] pts, Class<?>[] ets,
String body) {
StringBuilder sb = new StringBuilder();
sb.append(modifier(mod)).append(' ').append(ReflectUtils.getName(rt)).append(' ').append(name);
sb.append('(');
for (int i = 0; i < pts.length; i++) {
if (i > 0)
if (i > 0) {
sb.append(',');
}
sb.append(ReflectUtils.getName(pts[i]));
sb.append(" arg").append(i);
}
sb.append(')');
if (ets != null && ets.length > 0) {
sb.append(" throws ");
for (int i = 0; i < ets.length; i++) {
if (i > 0)
if (i > 0) {
sb.append(',');
}
sb.append(ReflectUtils.getName(ets[i]));
}
}
Expand All @@ -197,15 +219,17 @@ public ClassGenerator addMethod(Method m) {
public ClassGenerator addMethod(String name, Method m) {
String desc = name + ReflectUtils.getDescWithoutMethodName(m);
addMethod(':' + desc);
if (mCopyMethods == null)
if (mCopyMethods == null) {
mCopyMethods = new ConcurrentHashMap<String, Method>(8);
}
mCopyMethods.put(desc, m);
return this;
}

public ClassGenerator addConstructor(String code) {
if (mConstructors == null)
if (mConstructors == null) {
mConstructors = new LinkedList<String>();
}
mConstructors.add(code);
return this;
}
Expand All @@ -219,17 +243,19 @@ public ClassGenerator addConstructor(int mod, Class<?>[] pts, Class<?>[] ets, St
sb.append(modifier(mod)).append(' ').append(SIMPLE_NAME_TAG);
sb.append('(');
for (int i = 0; i < pts.length; i++) {
if (i > 0)
if (i > 0) {
sb.append(',');
}
sb.append(ReflectUtils.getName(pts[i]));
sb.append(" arg").append(i);
}
sb.append(')');
if (ets != null && ets.length > 0) {
sb.append(" throws ");
for (int i = 0; i < ets.length; i++) {
if (i > 0)
if (i > 0) {
sb.append(',');
}
sb.append(ReflectUtils.getName(ets[i]));
}
}
Expand All @@ -240,8 +266,9 @@ public ClassGenerator addConstructor(int mod, Class<?>[] pts, Class<?>[] ets, St
public ClassGenerator addConstructor(Constructor<?> c) {
String desc = ReflectUtils.getDesc(c);
addConstructor(":" + desc);
if (mCopyConstructors == null)
if (mCopyConstructors == null) {
mCopyConstructors = new ConcurrentHashMap<String, Constructor<?>>(4);
}
mCopyConstructors.put(desc, c);
return this;
}
Expand All @@ -256,43 +283,58 @@ public ClassPool getClassPool() {
}

public Class<?> toClass() {
return toClass(ClassHelper.getClassLoader(ClassGenerator.class), getClass().getProtectionDomain());
return toClass(ClassHelper.getClassLoader(ClassGenerator.class),
getClass().getProtectionDomain());
}

public Class<?> toClass(ClassLoader loader, ProtectionDomain pd) {
if (mCtc != null)
if (mCtc != null) {
mCtc.detach();
}
long id = CLASS_NAME_COUNTER.getAndIncrement();
try {
CtClass ctcs = mSuperClass == null ? null : mPool.get(mSuperClass);
if (mClassName == null)
if (mClassName == null) {
mClassName = (mSuperClass == null || javassist.Modifier.isPublic(ctcs.getModifiers())
? ClassGenerator.class.getName() : mSuperClass + "$sc") + id;
}
mCtc = mPool.makeClass(mClassName);
if (mSuperClass != null)
if (mSuperClass != null) {
mCtc.setSuperclass(ctcs);
}
mCtc.addInterface(mPool.get(DC.class.getName())); // add dynamic class tag.
if (mInterfaces != null)
for (String cl : mInterfaces) mCtc.addInterface(mPool.get(cl));
if (mFields != null)
for (String code : mFields) mCtc.addField(CtField.make(code, mCtc));
if (mInterfaces != null) {
for (String cl : mInterfaces) {
mCtc.addInterface(mPool.get(cl));
}
}
if (mFields != null) {
for (String code : mFields) {
mCtc.addField(CtField.make(code, mCtc));
}
}
if (mMethods != null) {
for (String code : mMethods) {
if (code.charAt(0) == ':')
mCtc.addMethod(CtNewMethod.copy(getCtMethod(mCopyMethods.get(code.substring(1))), code.substring(1, code.indexOf('(')), mCtc, null));
else
if (code.charAt(0) == ':') {
mCtc.addMethod(CtNewMethod.copy(getCtMethod(mCopyMethods.get(code.substring(1))),
code.substring(1, code.indexOf('(')), mCtc, null));
} else {
mCtc.addMethod(CtNewMethod.make(code, mCtc));
}
}
}
if (mDefaultConstructor)
if (mDefaultConstructor) {
mCtc.addConstructor(CtNewConstructor.defaultConstructor(mCtc));
}
if (mConstructors != null) {
for (String code : mConstructors) {
if (code.charAt(0) == ':') {
mCtc.addConstructor(CtNewConstructor.copy(getCtConstructor(mCopyConstructors.get(code.substring(1))), mCtc, null));
mCtc.addConstructor(CtNewConstructor
.copy(getCtConstructor(mCopyConstructors.get(code.substring(1))), mCtc, null));
} else {
String[] sn = mCtc.getSimpleName().split("\\$+"); // inner class name include $.
mCtc.addConstructor(CtNewConstructor.make(code.replaceFirst(SIMPLE_NAME_TAG, sn[sn.length - 1]), mCtc));
mCtc.addConstructor(
CtNewConstructor.make(code.replaceFirst(SIMPLE_NAME_TAG, sn[sn.length - 1]), mCtc));
}
}
}
Expand All @@ -307,27 +349,43 @@ public Class<?> toClass(ClassLoader loader, ProtectionDomain pd) {
}

public void release() {
if (mCtc != null) mCtc.detach();
if (mInterfaces != null) mInterfaces.clear();
if (mFields != null) mFields.clear();
if (mMethods != null) mMethods.clear();
if (mConstructors != null) mConstructors.clear();
if (mCopyMethods != null) mCopyMethods.clear();
if (mCopyConstructors != null) mCopyConstructors.clear();
if (mCtc != null) {
mCtc.detach();
}
if (mInterfaces != null) {
mInterfaces.clear();
}
if (mFields != null) {
mFields.clear();
}
if (mMethods != null) {
mMethods.clear();
}
if (mConstructors != null) {
mConstructors.clear();
}
if (mCopyMethods != null) {
mCopyMethods.clear();
}
if (mCopyConstructors != null) {
mCopyConstructors.clear();
}
}

private CtClass getCtClass(Class<?> c) throws NotFoundException {
return mPool.get(c.getName());
}

private CtMethod getCtMethod(Method m) throws NotFoundException {
return getCtClass(m.getDeclaringClass()).getMethod(m.getName(), ReflectUtils.getDescWithoutMethodName(m));
return getCtClass(m.getDeclaringClass())
.getMethod(m.getName(), ReflectUtils.getDescWithoutMethodName(m));
}

private CtConstructor getCtConstructor(Constructor<?> c) throws NotFoundException {
return getCtClass(c.getDeclaringClass()).getConstructor(ReflectUtils.getDesc(c));
}

public static interface DC {

} // dynamic class tag interface.
}

0 comments on commit 00b718c

Please sign in to comment.