> primitiveTypeNames = new HashSet<>(32);
- primitiveTypeNames.addAll(PRIMITIVE_WRAPPER_TYPE_MAP.values());
- primitiveTypeNames.addAll(Arrays.asList(boolean[].class, byte[].class, char[].class, double[].class,
- float[].class, int[].class, long[].class, short[].class));
- for (Class> primitiveTypeName : primitiveTypeNames) {
- PRIMITIVE_TYPE_NAME_MAP.put(primitiveTypeName.getName(), primitiveTypeName);
- }
- }
-
- public static Class> forNameWithThreadContextClassLoader(String name) throws ClassNotFoundException {
- return forName(name, Thread.currentThread().getContextClassLoader());
- }
-
- public static Class> forNameWithCallerClassLoader(String name, Class> caller) throws ClassNotFoundException {
- return forName(name, caller.getClassLoader());
- }
-
- public static ClassLoader getCallerClassLoader(Class> caller) {
- return caller.getClassLoader();
- }
-
- /**
- * get class loader
- *
- * @param clazz
- * @return class loader
- */
- public static ClassLoader getClassLoader(Class> clazz) {
- ClassLoader cl = null;
- try {
- cl = Thread.currentThread().getContextClassLoader();
- } catch (Throwable ex) {
- // Cannot access thread context ClassLoader - falling back to system class
- // loader...
- }
- if (cl == null) {
- // No thread context class loader -> use class loader of this class.
- cl = clazz.getClassLoader();
- if (cl == null) {
- // getClassLoader() returning null indicates the bootstrap ClassLoader
- try {
- cl = ClassLoader.getSystemClassLoader();
- } catch (Throwable ex) {
- // Cannot access system ClassLoader - oh well, maybe the caller can live with
- // null...
- }
- }
- }
-
- return cl;
- }
-
- /**
- * Return the default ClassLoader to use: typically the thread context
- * ClassLoader, if available; the ClassLoader that loaded the ClassUtils class
- * will be used as fallback.
- *
- * Call this method if you intend to use the thread context ClassLoader in a
- * scenario where you absolutely need a non-null ClassLoader reference: for
- * example, for class path resource loading (but not necessarily for
- * Class.forName, which accepts a null ClassLoader
- * reference as well).
- *
- * @return the default ClassLoader (never null)
- * @see Thread#getContextClassLoader()
- */
- public static ClassLoader getClassLoader() {
- return getClassLoader(ClassLoaderUtils.class);
- }
-
- /**
- * Same as Class.forName(), except that it works for primitive
- * types.
- */
- public static Class> forName(String name) throws ClassNotFoundException {
- return forName(name, getClassLoader());
- }
-
- /**
- * Replacement for Class.forName() that also returns Class
- * instances for primitives (like "int") and array class names (like
- * "String[]").
- *
- * @param name the name of the Class
- * @param classLoader the class loader to use (may be null, which
- * indicates the default class loader)
- * @return Class instance for the supplied name
- * @throws ClassNotFoundException if the class was not found
- * @throws LinkageError if the class file could not be loaded
- * @see Class#forName(String, boolean, ClassLoader)
- */
- public static Class> forName(String name, ClassLoader classLoader) throws ClassNotFoundException, LinkageError {
-
- Class> clazz = resolvePrimitiveClassName(name);
- if (clazz != null) {
- return clazz;
- }
-
- // "java.lang.String[]" style arrays
- if (name.endsWith(ARRAY_SUFFIX)) {
- String elementClassName = name.substring(0, name.length() - ARRAY_SUFFIX.length());
- Class> elementClass = forName(elementClassName, classLoader);
- return Array.newInstance(elementClass, 0).getClass();
- }
-
- // "[Ljava.lang.String;" style arrays
- int internalArrayMarker = name.indexOf(INTERNAL_ARRAY_PREFIX);
- if (internalArrayMarker != -1 && name.endsWith(";")) {
- String elementClassName = null;
- if (internalArrayMarker == 0) {
- elementClassName = name.substring(INTERNAL_ARRAY_PREFIX.length(), name.length() - 1);
- } else if (name.startsWith("[")) {
- elementClassName = name.substring(1);
- }
- Class> elementClass = forName(elementClassName, classLoader);
- return Array.newInstance(elementClass, 0).getClass();
- }
-
- ClassLoader classLoaderToUse = classLoader;
- if (classLoaderToUse == null) {
- classLoaderToUse = getClassLoader();
- }
- return classLoaderToUse.loadClass(name);
- }
-
- /**
- * Resolve the given class name as primitive class, if appropriate, according to
- * the JVM's naming rules for primitive classes.
- *
- * Also supports the JVM's internal class names for primitive arrays. Does
- * not support the "[]" suffix notation for primitive arrays; this is
- * only supported by {@link #forName}.
- *
- * @param name the name of the potentially primitive class
- * @return the primitive class, or null if the name does not denote
- * a primitive class or primitive array class
- */
- public static Class> resolvePrimitiveClassName(String name) {
- Class> result = null;
- // Most class names will be quite long, considering that they
- // SHOULD sit in a package, so a length check is worthwhile.
- if (name != null && name.length() <= 8) {
- // Could be a primitive - likely.
- result = (Class>) PRIMITIVE_TYPE_NAME_MAP.get(name);
- }
- return result;
- }
-
- public static String toShortString(Object obj) {
- if (obj == null) {
- return "null";
- }
- return obj.getClass().getSimpleName() + "@" + System.identityHashCode(obj);
-
- }
-
- public static String simpleClassName(Class> clazz) {
- if (clazz == null) {
- throw new NullPointerException("clazz");
- }
- String className = clazz.getName();
- final int lastDotIdx = className.lastIndexOf(PACKAGE_SEPARATOR_CHAR);
- if (lastDotIdx > -1) {
- return className.substring(lastDotIdx + 1);
- }
- return className;
- }
-
- public static Object convertPrimitive(Class> type, String value) {
- if (value == null) {
- return null;
- } else if (type == char.class || type == Character.class) {
- return value.length() > 0 ? value.charAt(0) : '\0';
- } else if (type == boolean.class || type == Boolean.class) {
- return Boolean.valueOf(value);
- }
- try {
- if (type == byte.class || type == Byte.class) {
- return Byte.valueOf(value);
- } else if (type == short.class || type == Short.class) {
- return Short.valueOf(value);
- } else if (type == int.class || type == Integer.class) {
- return Integer.valueOf(value);
- } else if (type == long.class || type == Long.class) {
- return Long.valueOf(value);
- } else if (type == float.class || type == Float.class) {
- return Float.valueOf(value);
- } else if (type == double.class || type == Double.class) {
- return Double.valueOf(value);
- }
- } catch (NumberFormatException e) {
- return null;
- }
- return value;
- }
-
- /**
- * We only check boolean value at this moment.
- *
- * @param type
- * @param value
- * @return
- */
- public static boolean isTypeMatch(Class> type, String value) {
- if ((type == boolean.class || type == Boolean.class) && !("true".equals(value) || "false".equals(value))) {
- return false;
- }
- return true;
- }
-
- /**
- * the semantics is same as {@link Class#isAssignableFrom(Class)}
- *
- * @param superType the super type
- * @param targetType the target type
- * @return see {@link Class#isAssignableFrom(Class)}
- * @since 2.7.6
- */
- public static boolean isAssignableFrom(Class> superType, Class> targetType) {
- // any argument is null
- if (superType == null || targetType == null) {
- return false;
- }
- // equals
- if (Objects.equals(superType, targetType)) {
- return true;
- }
- // isAssignableFrom
- return superType.isAssignableFrom(targetType);
- }
-
- /**
- * Test the specified class name is present in the {@link ClassLoader}
- *
- * @param className the name of {@link Class}
- * @param classLoader {@link ClassLoader}
- * @return If found, return true
- * @since 2.7.6
- */
- public static boolean isPresent(String className, ClassLoader classLoader) {
- try {
- forName(className, classLoader);
- } catch (Throwable ignored) { // Ignored
- return false;
- }
- return true;
- }
-
- /**
- * Resolve the {@link Class} by the specified name and {@link ClassLoader}
- *
- * @param className the name of {@link Class}
- * @param classLoader {@link ClassLoader}
- * @return If can't be resolved , return null
- * @since 2.7.6
- */
- public static Class> resolveClass(String className, ClassLoader classLoader) {
- Class> targetClass = null;
- try {
- targetClass = forName(className, classLoader);
- } catch (Throwable ignored) { // Ignored
- }
- return targetClass;
- }
-
- /**
- * Is generic class or not?
- *
- * @param type the target type
- * @return if the target type is not null or void or Void.class,
- * return true, or false
- * @since 2.7.6
- */
- public static boolean isGenericClass(Class> type) {
- return type != null && !void.class.equals(type) && !Void.class.equals(type);
- }
-}
diff --git a/dorive-proxy/src/main/java/com/gitee/dorive/proxy/ClassUtils.java b/dorive-proxy/src/main/java/com/gitee/dorive/proxy/ClassUtils.java
deleted file mode 100644
index 0f7d81c6..00000000
--- a/dorive-proxy/src/main/java/com/gitee/dorive/proxy/ClassUtils.java
+++ /dev/null
@@ -1,434 +0,0 @@
-/*
- * 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 com.gitee.dorive.proxy;
-
-import org.apache.commons.lang3.StringUtils;
-
-import java.io.PrintWriter;
-import java.io.StringWriter;
-import java.lang.reflect.*;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * ClassUtils. (Tool, Static, ThreadSafe)
- */
-public class ClassUtils {
-
- public static final String CLASS_EXTENSION = ".class";
-
- public static final String JAVA_EXTENSION = ".java";
- private static final int JIT_LIMIT = 5 * 1024;
-
- private ClassUtils() {
- }
-
- public static Object newInstance(String name) {
- try {
- return forName(name).newInstance();
- } catch (InstantiationException e) {
- throw new IllegalStateException(e.getMessage(), e);
- } catch (IllegalAccessException e) {
- throw new IllegalStateException(e.getMessage(), e);
- }
- }
-
- public static Class> forName(String[] packages, String className) {
- try {
- return classForName(className);
- } catch (ClassNotFoundException e) {
- if (packages != null && packages.length > 0) {
- for (String pkg : packages) {
- try {
- return classForName(pkg + "." + className);
- } catch (ClassNotFoundException e2) {
- }
- }
- }
- throw new IllegalStateException(e.getMessage(), e);
- }
- }
-
- public static Class> forName(String className) {
- try {
- return classForName(className);
- } catch (ClassNotFoundException e) {
- throw new IllegalStateException(e.getMessage(), e);
- }
- }
-
- public static Class> classForName(String className) throws ClassNotFoundException {
- switch (className) {
- case "boolean":
- return boolean.class;
- case "byte":
- return byte.class;
- case "char":
- return char.class;
- case "short":
- return short.class;
- case "int":
- return int.class;
- case "long":
- return long.class;
- case "float":
- return float.class;
- case "double":
- return double.class;
- case "boolean[]":
- return boolean[].class;
- case "byte[]":
- return byte[].class;
- case "char[]":
- return char[].class;
- case "short[]":
- return short[].class;
- case "int[]":
- return int[].class;
- case "long[]":
- return long[].class;
- case "float[]":
- return float[].class;
- case "double[]":
- return double[].class;
- default:
- }
- try {
- return arrayForName(className);
- } catch (ClassNotFoundException e) {
- // try to load from java.lang package
- if (className.indexOf('.') == -1) {
- try {
- return arrayForName("java.lang." + className);
- } catch (ClassNotFoundException e2) {
- // ignore, let the original exception be thrown
- }
- }
- throw e;
- }
- }
-
- private static Class> arrayForName(String className) throws ClassNotFoundException {
- return Class.forName(
- className.endsWith("[]") ? "[L" + className.substring(0, className.length() - 2) + ";" : className,
- true, Thread.currentThread().getContextClassLoader());
- }
-
- public static Class> getBoxedClass(Class> type) {
- if (type == boolean.class) {
- return Boolean.class;
- } else if (type == char.class) {
- return Character.class;
- } else if (type == byte.class) {
- return Byte.class;
- } else if (type == short.class) {
- return Short.class;
- } else if (type == int.class) {
- return Integer.class;
- } else if (type == long.class) {
- return Long.class;
- } else if (type == float.class) {
- return Float.class;
- } else if (type == double.class) {
- return Double.class;
- } else {
- return type;
- }
- }
-
- public static Boolean boxed(boolean v) {
- return Boolean.valueOf(v);
- }
-
- public static Character boxed(char v) {
- return Character.valueOf(v);
- }
-
- public static Byte boxed(byte v) {
- return Byte.valueOf(v);
- }
-
- public static Short boxed(short v) {
- return Short.valueOf(v);
- }
-
- public static Integer boxed(int v) {
- return Integer.valueOf(v);
- }
-
- public static Long boxed(long v) {
- return Long.valueOf(v);
- }
-
- public static Float boxed(float v) {
- return Float.valueOf(v);
- }
-
- public static Double boxed(double v) {
- return Double.valueOf(v);
- }
-
- public static Object boxed(Object v) {
- return v;
- }
-
- public static boolean unboxed(Boolean v) {
- return v == null ? false : v.booleanValue();
- }
-
- public static char unboxed(Character v) {
- return v == null ? '\0' : v.charValue();
- }
-
- public static byte unboxed(Byte v) {
- return v == null ? 0 : v.byteValue();
- }
-
- public static short unboxed(Short v) {
- return v == null ? 0 : v.shortValue();
- }
-
- public static int unboxed(Integer v) {
- return v == null ? 0 : v.intValue();
- }
-
- public static long unboxed(Long v) {
- return v == null ? 0 : v.longValue();
- }
-
- public static float unboxed(Float v) {
- return v == null ? 0 : v.floatValue();
- }
-
- public static double unboxed(Double v) {
- return v == null ? 0 : v.doubleValue();
- }
-
- public static Object unboxed(Object v) {
- return v;
- }
-
- public static boolean isNotEmpty(Object object) {
- return getSize(object) > 0;
- }
-
- public static int getSize(Object object) {
- if (object == null) {
- return 0;
- }
- if (object instanceof Collection>) {
- return ((Collection>) object).size();
- } else if (object instanceof Map, ?>) {
- return ((Map, ?>) object).size();
- } else if (object.getClass().isArray()) {
- return Array.getLength(object);
- } else {
- return -1;
- }
- }
-
- public static URI toURI(String name) {
- try {
- return new URI(name);
- } catch (URISyntaxException e) {
- throw new RuntimeException(e);
- }
- }
-
- public static Class> getGenericClass(Class> cls) {
- return getGenericClass(cls, 0);
- }
-
- public static Class> getGenericClass(Class> cls, int i) {
- try {
- ParameterizedType parameterizedType = ((ParameterizedType) cls.getGenericInterfaces()[0]);
- Object genericClass = parameterizedType.getActualTypeArguments()[i];
- if (genericClass instanceof ParameterizedType) {
- return (Class>) ((ParameterizedType) genericClass).getRawType();
- } else if (genericClass instanceof GenericArrayType) {
- Type type = ((GenericArrayType) genericClass).getGenericComponentType();
- if (type instanceof TypeVariable) {
- return type.getClass();
- }
- return (((GenericArrayType) genericClass).getGenericComponentType() instanceof Class>)
- ? (Class>) ((GenericArrayType) genericClass).getGenericComponentType()
- : ((GenericArrayType) genericClass).getGenericComponentType().getClass();
- } else if (genericClass != null) {
- if (genericClass instanceof TypeVariable) {
- return genericClass.getClass();
- }
- return (Class>) genericClass;
- }
- } catch (Throwable e) {
-
- }
- if (cls.getSuperclass() != null) {
- return getGenericClass(cls.getSuperclass(), i);
- } else {
- throw new IllegalArgumentException(cls.getName() + " generic type undefined!");
- }
- }
-
- public static boolean isBeforeJava5(String javaVersion) {
- return (StringUtils.isEmpty(javaVersion) || "1.0".equals(javaVersion) || "1.1".equals(javaVersion)
- || "1.2".equals(javaVersion) || "1.3".equals(javaVersion) || "1.4".equals(javaVersion));
- }
-
- public static boolean isBeforeJava6(String javaVersion) {
- return isBeforeJava5(javaVersion) || "1.5".equals(javaVersion);
- }
-
- public static String toString(Throwable e) {
- StringWriter w = new StringWriter();
- PrintWriter p = new PrintWriter(w);
- p.print(e.getClass().getName() + ": ");
- if (e.getMessage() != null) {
- p.print(e.getMessage() + "\n");
- }
- p.println();
- try {
- e.printStackTrace(p);
- return w.toString();
- } finally {
- p.close();
- }
- }
-
- public static void checkBytecode(String name, byte[] bytecode) {
- if (bytecode.length > JIT_LIMIT) {
- System.err
- .println("The template bytecode too long, may be affect the JIT compiler. template class: " + name);
- }
- }
-
- public static String getSizeMethod(Class> cls) {
- try {
- return cls.getMethod("size", new Class>[0]).getName() + "()";
- } catch (NoSuchMethodException e) {
- try {
- return cls.getMethod("length", new Class>[0]).getName() + "()";
- } catch (NoSuchMethodException e2) {
- try {
- return cls.getMethod("getSize", new Class>[0]).getName() + "()";
- } catch (NoSuchMethodException e3) {
- try {
- return cls.getMethod("getLength", new Class>[0]).getName() + "()";
- } catch (NoSuchMethodException e4) {
- return null;
- }
- }
- }
- }
- }
-
- public static String getMethodName(Method method, Class>[] parameterClasses, String rightCode) {
- if (method.getParameterTypes().length > parameterClasses.length) {
- Class>[] types = method.getParameterTypes();
- StringBuilder buf = new StringBuilder(rightCode);
- for (int i = parameterClasses.length; i < types.length; i++) {
- if (buf.length() > 0) {
- buf.append(",");
- }
- Class> type = types[i];
- String def;
- if (type == boolean.class) {
- def = "false";
- } else if (type == char.class) {
- def = "\'\\0\'";
- } else if (type == byte.class || type == short.class || type == int.class || type == long.class
- || type == float.class || type == double.class) {
- def = "0";
- } else {
- def = "null";
- }
- buf.append(def);
- }
- }
- return method.getName() + "(" + rightCode + ")";
- }
-
- public static Method searchMethod(Class> currentClass, String name, Class>[] parameterTypes)
- throws NoSuchMethodException {
- if (currentClass == null) {
- throw new NoSuchMethodException("class == null");
- }
- try {
- return currentClass.getMethod(name, parameterTypes);
- } catch (NoSuchMethodException e) {
- for (Method method : currentClass.getMethods()) {
- if (method.getName().equals(name) && parameterTypes.length == method.getParameterTypes().length
- && Modifier.isPublic(method.getModifiers())) {
- if (parameterTypes.length > 0) {
- Class>[] types = method.getParameterTypes();
- boolean match = true;
- for (int i = 0; i < parameterTypes.length; i++) {
- if (!types[i].isAssignableFrom(parameterTypes[i])) {
- match = false;
- break;
- }
- }
- if (!match) {
- continue;
- }
- }
- return method;
- }
- }
- throw e;
- }
- }
-
- public static String getInitCode(Class> type) {
- if (byte.class.equals(type) || short.class.equals(type) || int.class.equals(type) || long.class.equals(type)
- || float.class.equals(type) || double.class.equals(type)) {
- return "0";
- } else if (char.class.equals(type)) {
- return "'\\0'";
- } else if (boolean.class.equals(type)) {
- return "false";
- } else {
- return "null";
- }
- }
-
- public static Map toMap(Map.Entry[] entries) {
- Map map = new HashMap();
- if (entries != null && entries.length > 0) {
- for (Map.Entry entry : entries) {
- map.put(entry.getKey(), entry.getValue());
- }
- }
- return map;
- }
-
- /**
- * get simple class name from qualified class name
- */
- public static String getSimpleClassName(String qualifiedName) {
- if (null == qualifiedName) {
- return null;
- }
-
- int i = qualifiedName.lastIndexOf('.');
- return i < 0 ? qualifiedName : qualifiedName.substring(i + 1);
- }
-
-}
diff --git a/dorive-proxy/src/main/java/com/gitee/dorive/proxy/CtClassBuilder.java b/dorive-proxy/src/main/java/com/gitee/dorive/proxy/CtClassBuilder.java
deleted file mode 100644
index f051458d..00000000
--- a/dorive-proxy/src/main/java/com/gitee/dorive/proxy/CtClassBuilder.java
+++ /dev/null
@@ -1,170 +0,0 @@
-/*
- * 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 com.gitee.dorive.proxy;
-
-import javassist.*;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * CtClassBuilder is builder for CtClass
- *
- * contains all the information, including:
- *
- * class name, imported packages, super class name, implemented interfaces,
- * constructors, fields, methods.
- */
-public class CtClassBuilder {
-
- private String className;
-
- private String superClassName = "java.lang.Object";
-
- private List imports = new ArrayList<>();
-
- private Map fullNames = new HashMap<>();
-
- private List ifaces = new ArrayList<>();
-
- private List constructors = new ArrayList<>();
-
- private List fields = new ArrayList<>();
-
- private List 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 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 getInterfaces() {
- return ifaces;
- }
-
- public void addInterface(String iface) {
- this.ifaces.add(getQualifiedClassName(iface));
- }
-
- public List getConstructors() {
- return constructors;
- }
-
- public void addConstructor(String constructor) {
- this.constructors.add(constructor);
- }
-
- public List getFields() {
- return fields;
- }
-
- public void addField(String field) {
- this.fields.add(field);
- }
-
- public List 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(ClassLoader classLoader) throws NotFoundException, CannotCompileException {
- ClassPool pool = new ClassPool(true);
- pool.appendClassPath(new LoaderClassPath(classLoader));
-
- // create class
- CtClass ctClass = pool.makeClass(className, pool.get(superClassName));
-
- // add imported packages
- imports.stream().forEach(pool::importPackage);
-
- // add implemented interfaces
- for (String iface : ifaces) {
- ctClass.addInterface(pool.get(iface));
- }
-
- // dubbo didn't consider the order,so we fixed it.
- // add fields
- for (String field : fields) {
- ctClass.addField(CtField.make(field, ctClass));
- }
-
- // add constructors
- for (String constructor : constructors) {
- ctClass.addConstructor(CtNewConstructor.make(constructor, ctClass));
- }
-
- // add methods
- for (String method : methods) {
- ctClass.addMethod(CtNewMethod.make(method, ctClass));
- }
-
- return ctClass;
- }
-
-}
diff --git a/dorive-proxy/src/main/java/com/gitee/dorive/proxy/JavassistCompiler.java b/dorive-proxy/src/main/java/com/gitee/dorive/proxy/JavassistCompiler.java
deleted file mode 100644
index da7c086b..00000000
--- a/dorive-proxy/src/main/java/com/gitee/dorive/proxy/JavassistCompiler.java
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * 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 com.gitee.dorive.proxy;
-
-import javassist.CtClass;
-
-import java.util.Arrays;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-/**
- * JavassistCompiler. (SPI, Singleton, ThreadSafe)
- */
-public class JavassistCompiler extends AbstractCompiler {
-
- private static final Pattern IMPORT_PATTERN = Pattern.compile("import\\s+([\\w\\.\\*]+);\n");
-
- private static final Pattern EXTENDS_PATTERN = Pattern.compile("\\s+extends\\s+([\\w\\.]+)[^\\{]*\\{\n");
-
- private static final Pattern IMPLEMENTS_PATTERN = Pattern.compile("\\s+implements\\s+([\\w\\.]+)\\s*\\{\n");
- // dubbo don't have (\t)?
- private static final Pattern METHODS_PATTERN = Pattern.compile("\n(\t)?(private|public|protected)\\s+");
-
- private static final Pattern FIELD_PATTERN = Pattern.compile("[^\n]+=[^\n]+;");
-
- @Override
- public Class> doCompile(String name, String source) throws Throwable {
- CtClassBuilder builder = new CtClassBuilder();
- builder.setClassName(name);
-
- // process imported classes
- Matcher matcher = IMPORT_PATTERN.matcher(source);
- while (matcher.find()) {
- builder.addImports(matcher.group(1).trim());
- }
-
- // process extended super class
- matcher = EXTENDS_PATTERN.matcher(source);
- if (matcher.find()) {
- builder.setSuperClassName(matcher.group(1).trim());
- }
-
- // process implemented interfaces
- matcher = IMPLEMENTS_PATTERN.matcher(source);
- if (matcher.find()) {
- String[] ifaces = matcher.group(1).trim().split("\\,");
- Arrays.stream(ifaces).forEach(i -> builder.addInterface(i.trim()));
- }
-
- // process constructors, fields, methods
- String body = source.substring(source.indexOf('{') + 1, source.length() - 1);
- String[] methods = METHODS_PATTERN.split(body);
- String className = ClassUtils.getSimpleClassName(name);
- Arrays.stream(methods).map(String::trim).filter(m -> !m.isEmpty()).forEach(method -> {
- if (method.startsWith(className)) {
- builder.addConstructor("public " + method);
- } else if (FIELD_PATTERN.matcher(method).matches()) {
- builder.addField("private " + method);
- } else {
- builder.addMethod("public " + method);
- }
- });
-
- // compile
- ClassLoader classLoader = ClassLoaderUtils.getCallerClassLoader(getClass());
- CtClass cls = builder.build(classLoader);
- return cls.toClass(classLoader, JavassistCompiler.class.getProtectionDomain());
- }
-
-}
diff --git a/dorive-proxy/src/main/java/com/gitee/dorive/proxy/JdkCompiler.java b/dorive-proxy/src/main/java/com/gitee/dorive/proxy/JdkCompiler.java
deleted file mode 100644
index 7c3f282d..00000000
--- a/dorive-proxy/src/main/java/com/gitee/dorive/proxy/JdkCompiler.java
+++ /dev/null
@@ -1,273 +0,0 @@
-/*
- * 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 com.gitee.dorive.proxy;
-
-import javax.tools.*;
-import javax.tools.JavaFileObject.Kind;
-import java.io.*;
-import java.net.URI;
-import java.net.URL;
-import java.net.URLClassLoader;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
-import java.util.*;
-
-/**
- * JdkCompiler. (SPI, Singleton, ThreadSafe)
- */
-public class JdkCompiler extends AbstractCompiler {
-
- private final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
-
- private final DiagnosticCollector diagnosticCollector = new DiagnosticCollector();
-
- private final ClassLoaderImpl classLoader;
-
- private final JavaFileManagerImpl javaFileManager;
-
- private volatile List options;
-
- public JdkCompiler() {
- options = new ArrayList();
- options.add("-source");
- options.add("1.6");
- options.add("-target");
- options.add("1.6");
- StandardJavaFileManager manager = compiler.getStandardFileManager(diagnosticCollector, null, null);
- final ClassLoader loader = Thread.currentThread().getContextClassLoader();
- if (loader instanceof URLClassLoader
- && (!"sun.misc.Launcher$AppClassLoader".equals(loader.getClass().getName()))) {
- try {
- @SuppressWarnings("resource")
- URLClassLoader urlClassLoader = (URLClassLoader) loader;
- List files = new ArrayList();
- for (URL url : urlClassLoader.getURLs()) {
- files.add(new File(url.getFile()));
- }
- manager.setLocation(StandardLocation.CLASS_PATH, files);
- } catch (IOException e) {
- throw new IllegalStateException(e.getMessage(), e);
- }
- }
- classLoader = AccessController.doPrivileged(new PrivilegedAction() {
- @Override
- public ClassLoaderImpl run() {
- return new ClassLoaderImpl(loader);
- }
- });
- javaFileManager = new JavaFileManagerImpl(manager, classLoader);
- }
-
- @Override
- public Class> doCompile(String name, String sourceCode) throws Throwable {
- int i = name.lastIndexOf('.');
- String packageName = i < 0 ? "" : name.substring(0, i);
- String className = i < 0 ? name : name.substring(i + 1);
- JavaFileObjectImpl javaFileObject = new JavaFileObjectImpl(className, sourceCode);
- javaFileManager.putFileForInput(StandardLocation.SOURCE_PATH, packageName,
- className + ClassUtils.JAVA_EXTENSION, javaFileObject);
- Boolean result = compiler
- .getTask(null, javaFileManager, diagnosticCollector, options, null, Arrays.asList(javaFileObject))
- .call();
- if (result == null || !result) {
- throw new IllegalStateException(
- "Compilation failed. class: " + name + ", diagnostics: " + diagnosticCollector);
- }
- return classLoader.loadClass(name);
- }
-
- private static final class JavaFileObjectImpl extends SimpleJavaFileObject {
-
- private final CharSequence source;
- private ByteArrayOutputStream bytecode;
-
- public JavaFileObjectImpl(final String baseName, final CharSequence source) {
- super(ClassUtils.toURI(baseName + ClassUtils.JAVA_EXTENSION), Kind.SOURCE);
- this.source = source;
- }
-
- JavaFileObjectImpl(final String name, final Kind kind) {
- super(ClassUtils.toURI(name), kind);
- source = null;
- }
-
- public JavaFileObjectImpl(URI uri, Kind kind) {
- super(uri, kind);
- source = null;
- }
-
- @Override
- public CharSequence getCharContent(final boolean ignoreEncodingErrors) throws UnsupportedOperationException {
- if (source == null) {
- throw new UnsupportedOperationException("source == null");
- }
- return source;
- }
-
- @Override
- public InputStream openInputStream() {
- return new ByteArrayInputStream(getByteCode());
- }
-
- @Override
- public OutputStream openOutputStream() {
- return bytecode = new ByteArrayOutputStream();
- }
-
- public byte[] getByteCode() {
- return bytecode.toByteArray();
- }
- }
-
- private static final class JavaFileManagerImpl extends ForwardingJavaFileManager {
-
- private final ClassLoaderImpl classLoader;
-
- private final Map fileObjects = new HashMap();
-
- public JavaFileManagerImpl(JavaFileManager fileManager, ClassLoaderImpl classLoader) {
- super(fileManager);
- this.classLoader = classLoader;
- }
-
- @Override
- public FileObject getFileForInput(Location location, String packageName, String relativeName)
- throws IOException {
- FileObject o = fileObjects.get(uri(location, packageName, relativeName));
- if (o != null) {
- return o;
- }
- return super.getFileForInput(location, packageName, relativeName);
- }
-
- public void putFileForInput(StandardLocation location, String packageName, String relativeName,
- JavaFileObject file) {
- fileObjects.put(uri(location, packageName, relativeName), file);
- }
-
- private URI uri(Location location, String packageName, String relativeName) {
- return ClassUtils.toURI(location.getName() + '/' + packageName + '/' + relativeName);
- }
-
- @Override
- public JavaFileObject getJavaFileForOutput(Location location, String qualifiedName, Kind kind,
- FileObject outputFile) throws IOException {
- JavaFileObject file = new JavaFileObjectImpl(qualifiedName, kind);
- classLoader.add(qualifiedName, file);
- return file;
- }
-
- @Override
- public ClassLoader getClassLoader(Location location) {
- return classLoader;
- }
-
- @Override
- public String inferBinaryName(Location loc, JavaFileObject file) {
- if (file instanceof JavaFileObjectImpl) {
- return file.getName();
- }
- return super.inferBinaryName(loc, file);
- }
-
- @Override
- public Iterable list(Location location, String packageName, Set kinds, boolean recurse)
- throws IOException {
- Iterable result = super.list(location, packageName, kinds, recurse);
-
- @SuppressWarnings("unused")
- ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
-
- ArrayList files = new ArrayList();
-
- if (location == StandardLocation.CLASS_PATH && kinds.contains(Kind.CLASS)) {
- for (JavaFileObject file : fileObjects.values()) {
- if (file.getKind() == Kind.CLASS && file.getName().startsWith(packageName)) {
- files.add(file);
- }
- }
-
- files.addAll(classLoader.files());
- } else if (location == StandardLocation.SOURCE_PATH && kinds.contains(Kind.SOURCE)) {
- for (JavaFileObject file : fileObjects.values()) {
- if (file.getKind() == Kind.SOURCE && file.getName().startsWith(packageName)) {
- files.add(file);
- }
- }
- }
-
- for (JavaFileObject file : result) {
- files.add(file);
- }
-
- return files;
- }
- }
-
- private final class ClassLoaderImpl extends ClassLoader {
-
- private final Map classes = new HashMap();
-
- ClassLoaderImpl(final ClassLoader parentClassLoader) {
- super(parentClassLoader);
- }
-
- Collection files() {
- return Collections.unmodifiableCollection(classes.values());
- }
-
- @Override
- protected Class> findClass(final String qualifiedClassName) throws ClassNotFoundException {
- JavaFileObject file = classes.get(qualifiedClassName);
- if (file != null) {
- byte[] bytes = ((JavaFileObjectImpl) file).getByteCode();
- return defineClass(qualifiedClassName, bytes, 0, bytes.length);
- }
- try {
- return ClassLoaderUtils.forNameWithCallerClassLoader(qualifiedClassName,
- getClass());
- } catch (ClassNotFoundException nf) {
- return super.findClass(qualifiedClassName);
- }
- }
-
- void add(final String qualifiedClassName, final JavaFileObject javaFile) {
- classes.put(qualifiedClassName, javaFile);
- }
-
- @Override
- protected synchronized Class> loadClass(final String name, final boolean resolve)
- throws ClassNotFoundException {
- return super.loadClass(name, resolve);
- }
-
- @Override
- public InputStream getResourceAsStream(final String name) {
- if (name.endsWith(ClassUtils.CLASS_EXTENSION)) {
- String qualifiedClassName = name.substring(0, name.length() - ClassUtils.CLASS_EXTENSION.length())
- .replace('/', '.');
- JavaFileObjectImpl file = (JavaFileObjectImpl) classes.get(qualifiedClassName);
- if (file != null) {
- return new ByteArrayInputStream(file.getByteCode());
- }
- }
- return super.getResourceAsStream(name);
- }
- }
-
-}
diff --git a/dorive-proxy/src/main/java/com/gitee/dorive/proxy/ProxyCompiler.java b/dorive-proxy/src/main/java/com/gitee/dorive/proxy/ProxyCompiler.java
deleted file mode 100644
index e5ef294d..00000000
--- a/dorive-proxy/src/main/java/com/gitee/dorive/proxy/ProxyCompiler.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * 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 com.gitee.dorive.proxy;
-
-public interface ProxyCompiler {
-
- /**
- * Compile java source code.
- *
- * @param code Java source code
- * @param classLoader classloader
- * @return Compiled class
- */
- Class> compile(String code, ClassLoader classLoader);
-
-}
diff --git a/pom.xml b/pom.xml
index 2b5aca38..f18b0865 100644
--- a/pom.xml
+++ b/pom.xml
@@ -32,7 +32,6 @@
dorive-inject
dorive-env
dorive-web
- dorive-proxy
dorive-api
dorive-core
dorive-event
@@ -83,11 +82,6 @@
hutool-all
5.8.25