Skip to content

Commit

Permalink
rename ClassHelper to ClassUtils, add MethodUtils (#3806)
Browse files Browse the repository at this point in the history
* rename ClassHelper to ClassUtils, add MethodUtils
* remove import with '*'
* ClassUtils add apache license
  • Loading branch information
LiZhenNet authored and ralf0131 committed Apr 30, 2019
1 parent d54633e commit e189048
Show file tree
Hide file tree
Showing 24 changed files with 633 additions and 405 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.ClassHelper;
import org.apache.dubbo.common.utils.ClassUtils;
import org.apache.dubbo.common.utils.StringUtils;

import java.io.IOException;
Expand Down Expand Up @@ -240,7 +240,7 @@ public static void checkDuplicate(String path, boolean failOnError) {
* search resources in caller's classloader
*/
private static Set<String> getResources(String path) throws IOException {
Enumeration<URL> urls = ClassHelper.getCallerClassLoader(Version.class).getResources(path);
Enumeration<URL> urls = ClassUtils.getCallerClassLoader(Version.class).getResources(path);
Set<String> files = new HashSet<String>();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
*/
package org.apache.dubbo.common.bytecode;

import org.apache.dubbo.common.utils.ArrayUtils;
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 @@ -30,6 +26,9 @@
import javassist.CtNewMethod;
import javassist.LoaderClassPath;
import javassist.NotFoundException;
import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.common.utils.ClassUtils;
import org.apache.dubbo.common.utils.ReflectUtils;
import org.apache.dubbo.common.utils.StringUtils;

import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -285,7 +284,7 @@ public ClassPool getClassPool() {
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package org.apache.dubbo.common.bytecode;

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

import java.lang.reflect.Method;
Expand Down Expand Up @@ -69,7 +69,7 @@ public static Mixin mixin(Class<?>[] ics, Class<?> dc, ClassLoader cl) {
* @return Mixin instance.
*/
public static Mixin mixin(Class<?>[] ics, Class<?>[] dcs) {
return mixin(ics, dcs, ClassHelper.getCallerClassLoader(Mixin.class));
return mixin(ics, dcs, ClassUtils.getCallerClassLoader(Mixin.class));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.apache.dubbo.common.bytecode;

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

import java.lang.ref.Reference;
Expand Down Expand Up @@ -62,7 +62,7 @@ protected Proxy() {
* @return Proxy instance.
*/
public static Proxy getProxy(Class<?>... ics) {
return getProxy(ClassHelper.getClassLoader(Proxy.class), ics);
return getProxy(ClassUtils.getClassLoader(Proxy.class), ics);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package org.apache.dubbo.common.bytecode;

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

import java.lang.reflect.Field;
Expand Down Expand Up @@ -127,7 +127,7 @@ private static Wrapper makeWrapper(Class<?> c) {
}

String name = c.getName();
ClassLoader cl = ClassHelper.getClassLoader(c);
ClassLoader cl = ClassUtils.getClassLoader(c);

StringBuilder c1 = new StringBuilder("public void setPropertyValue(Object o, String n, Object v){ ");
StringBuilder c2 = new StringBuilder("public Object getPropertyValue(Object o, String n){ ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.dubbo.common.compiler.support;

import org.apache.dubbo.common.compiler.Compiler;
import org.apache.dubbo.common.utils.ClassHelper;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -50,7 +49,7 @@ public Class<?> compile(String code, ClassLoader classLoader) {
}
String className = pkg != null && pkg.length() > 0 ? pkg + "." + cls : cls;
try {
return Class.forName(className, true, ClassHelper.getCallerClassLoader(getClass()));
return Class.forName(className, true, org.apache.dubbo.common.utils.ClassUtils.getCallerClassLoader(getClass()));
} catch (ClassNotFoundException e) {
if (!code.endsWith("}")) {
throw new IllegalStateException("The java code not endsWith \"}\", code: \n" + code + "\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.dubbo.common.compiler.support;

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

import javassist.CtClass;

Expand Down Expand Up @@ -49,25 +48,25 @@ public Class<?> doCompile(String name, String source) throws Throwable {
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-> {
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()) {
Expand All @@ -76,9 +75,9 @@ public Class<?> doCompile(String name, String source) throws Throwable {
builder.addMethod("public " + method);
}
});

// compile
ClassLoader classLoader = ClassHelper.getCallerClassLoader(getClass());
ClassLoader classLoader = org.apache.dubbo.common.utils.ClassUtils.getCallerClassLoader(getClass());
CtClass cls = builder.build(classLoader);
return cls.toClass(classLoader, JavassistCompiler.class.getProtectionDomain());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.dubbo.common.compiler.support;

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

import javax.tools.DiagnosticCollector;
import javax.tools.FileObject;
Expand Down Expand Up @@ -261,7 +260,7 @@ protected Class<?> findClass(final String qualifiedClassName) throws ClassNotFou
return defineClass(qualifiedClassName, bytes, 0, bytes.length);
}
try {
return ClassHelper.forNameWithCallerClassLoader(qualifiedClassName, getClass());
return org.apache.dubbo.common.utils.ClassUtils.forNameWithCallerClassLoader(qualifiedClassName, getClass());
} catch (ClassNotFoundException nf) {
return super.findClass(qualifiedClassName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.common.utils.ClassHelper;
import org.apache.dubbo.common.utils.ClassUtils;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.ConcurrentHashSet;
import org.apache.dubbo.common.utils.ConfigUtils;
import org.apache.dubbo.common.utils.Holder;
import org.apache.dubbo.common.utils.ReflectUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.common.utils.CollectionUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -142,7 +143,7 @@ public static void resetExtensionLoader(Class type) {
}

private static ClassLoader findClassLoader() {
return ClassHelper.getClassLoader(ExtensionLoader.class);
return ClassUtils.getClassLoader(ExtensionLoader.class);
}

public String getExtensionName(T extensionInstance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.ClassHelper;
import org.apache.dubbo.common.utils.ClassUtils;

import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -415,7 +415,7 @@ public long pendingTimeouts() {
}

private static void reportTooManyInstances() {
String resourceType = ClassHelper.simpleClassName(HashedWheelTimer.class);
String resourceType = ClassUtils.simpleClassName(HashedWheelTimer.class);
logger.error("You are creating too many " + resourceType + " instances. " +
resourceType + " is a shared resource that must be reused across the JVM," +
"so that only a few instances are created.");
Expand Down Expand Up @@ -657,7 +657,7 @@ public void expire() {
public String toString() {
final long currentTime = System.nanoTime();
long remaining = deadline - currentTime + timer.startTime;
String simpleClassName = ClassHelper.simpleClassName(this.getClass());
String simpleClassName = ClassUtils.simpleClassName(this.getClass());

StringBuilder buf = new StringBuilder(192)
.append(simpleClassName)
Expand Down
Loading

0 comments on commit e189048

Please sign in to comment.