Skip to content

Commit

Permalink
解决问题:alibaba#107
Browse files Browse the repository at this point in the history
  • Loading branch information
oldmanpushcart committed Oct 27, 2018
1 parent 8b94f1b commit a1f928a
Show file tree
Hide file tree
Showing 18 changed files with 603 additions and 55 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

<groupId>com.alibaba.jvm.sandbox</groupId>
<artifactId>sandbox</artifactId>
<version>1.1.0</version>
<version>1.1.1-SNAPSHOT</version>
<packaging>pom</packaging>

<name>sandbox ${sandbox.version}</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sandbox.version>1.1.0</sandbox.version>
<sandbox.version>1.1.1-SNAPSHOT</sandbox.version>
</properties>

<build>
Expand Down
2 changes: 1 addition & 1 deletion sandbox-agent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.alibaba.jvm.sandbox</groupId>
<artifactId>sandbox</artifactId>
<version>1.1.0</version>
<version>1.1.1-SNAPSHOT</version>
</parent>
<artifactId>sandbox-agent</artifactId>
<name>sandbox-agent ${sandbox.version}</name>
Expand Down
2 changes: 1 addition & 1 deletion sandbox-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.alibaba.jvm.sandbox</groupId>
<artifactId>sandbox</artifactId>
<version>1.1.0</version>
<version>1.1.1-SNAPSHOT</version>
</parent>
<artifactId>sandbox-api</artifactId>
<name>sandbox-api ${sandbox.version}</name>
Expand Down
2 changes: 1 addition & 1 deletion sandbox-common-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>sandbox</artifactId>
<groupId>com.alibaba.jvm.sandbox</groupId>
<version>1.1.0</version>
<version>1.1.1-SNAPSHOT</version>
</parent>
<artifactId>sandbox-common-api</artifactId>
<name>sandbox-common-api ${sandbox.version}</name>
Expand Down
2 changes: 1 addition & 1 deletion sandbox-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.alibaba.jvm.sandbox</groupId>
<artifactId>sandbox</artifactId>
<version>1.1.0</version>
<version>1.1.1-SNAPSHOT</version>
</parent>
<artifactId>sandbox-core</artifactId>
<name>sandbox-core ${sandbox.version}</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import com.alibaba.jvm.sandbox.core.util.collection.Pair;
import com.alibaba.jvm.sandbox.core.util.matcher.structure.PrimitiveClassStructure.Primitive;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.objectweb.asm.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.*;

import static com.alibaba.jvm.sandbox.core.util.SandboxStringUtils.toInternalClassName;
Expand Down Expand Up @@ -198,14 +200,100 @@ static Primitive mappingPrimitiveByJavaClassName(final String javaClassName) {

}

class ArrayClassStructure extends EmptyClassStructure {
class ArrayClassStructure extends FamilyClassStructure {

private final ClassStructure elementClassStructure;

ArrayClassStructure(ClassStructure elementClassStructure) {
this.elementClassStructure = elementClassStructure;
}

@Override
public Access getAccess() {
final Access access = elementClassStructure.getAccess();
return new Access() {
@Override
public boolean isPublic() {
return access.isPublic();
}

@Override
public boolean isPrivate() {
return access.isPrivate();
}

@Override
public boolean isProtected() {
return access.isProtected();
}

@Override
public boolean isStatic() {
return false;
}

@Override
public boolean isFinal() {
return true;
}

@Override
public boolean isInterface() {
return false;
}

@Override
public boolean isNative() {
return false;
}

@Override
public boolean isAbstract() {
return true;
}

@Override
public boolean isEnum() {
return false;
}

@Override
public boolean isAnnotation() {
return false;
}
};
}

@Override
public ClassLoader getClassLoader() {
return elementClassStructure.getClassLoader();
}

@Override
public ClassStructure getSuperClassStructure() {
return new ClassStructureImplByJDK(Object.class);
}

@Override
public List<ClassStructure> getInterfaceClassStructures() {
return new ArrayList<ClassStructure>(
Arrays.asList(
new ClassStructureImplByJDK(Cloneable.class),
new ClassStructureImplByJDK(Serializable.class)
)
);
}

@Override
public List<ClassStructure> getAnnotationTypeClassStructures() {
return new LinkedList<ClassStructure>();
}

@Override
public List<BehaviorStructure> getBehaviorStructures() {
return new LinkedList<BehaviorStructure>();
}

@Override
public String getJavaClassName() {
return new StringBuilder()
Expand Down Expand Up @@ -236,7 +324,15 @@ public ClassStructureImplByAsm(final byte[] classByteArray,
final ClassLoader loader) {
this.classReader = new ClassReader(classByteArray);
this.loader = loader;
this.access = new AccessImplByAsm(this.classReader.getAccess());
this.access = new AccessImplByAsm(this.classReader.getAccess()){
@Override
public boolean isStatic() {
// 修复ASM的BUG,如果是Enum类型,必定是static的
// 但ASM(至少在6.0版本)会判断错误
return isEnum()
|| super.isStatic();
}
};
}

private boolean isBootstrapClassLoader() {
Expand Down Expand Up @@ -283,6 +379,17 @@ private ClassStructure newInstance(final String javaClassName) {
if (classStructureCache.containsKey(pair)) {
return classStructureCache.get(pair);
} else {

// 修复ASM获取Class时,遇到已加载的类会导致ClassLoader被重新定义的BUG
try {
final Class<?> tryToFoundClass = loader.loadClass(javaClassName);
final ClassStructure classStructure = new ClassStructureImplByJDK(tryToFoundClass);
classStructureCache.put(pair, classStructure);
return classStructure;
} catch (ClassNotFoundException e) {
// ignore...
}

final InputStream is = getResourceAsStream(internalClassNameToResourceName(toInternalClassName(javaClassName)));
if (null != is) {
try {
Expand Down Expand Up @@ -415,6 +522,13 @@ public MethodVisitor visitMethod(final int access,
final String desc,
final String signature,
final String[] exceptions) {

// 修复ASM会把<clinit>列入正常方法中的问题
// 实际上这个方法并不会参与到任何的逻辑判断
if(StringUtils.equals("<clinit>", name)) {
return super.visitMethod(access, name, desc, signature, exceptions);
}

return new MethodVisitor(ASM6, super.visitMethod(access, name, desc, signature, exceptions)) {

private final Type methodType = Type.getMethodType(desc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,26 @@ public String getJavaClassName() {
return javaClassName;
}

return javaClassName = clazz.isArray()
? clazz.getCanonicalName()
: clazz.getName();
return getJavaClassName(clazz);

// return javaClassName = clazz.isArray()
// ? clazz.getCanonicalName()
// : clazz.getName();

// final String canonicalName = clazz.getCanonicalName();
// return javaClassName = StringUtils.isEmpty(canonicalName)
// ? clazz.getName()
// : canonicalName;
}

private String getJavaClassName(Class<?> clazz) {
if(clazz.isArray()) {
return getJavaClassName(clazz.getComponentType())+"[]";
}
return clazz.getName();
}


@Override
public ClassLoader getClassLoader() {
return clazz.getClassLoader();
Expand Down
2 changes: 1 addition & 1 deletion sandbox-debug-module/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.alibaba.jvm.sandbox</groupId>
<artifactId>sandbox-module-starter</artifactId>
<version>1.1.0</version>
<version>1.1.1-SNAPSHOT</version>
<relativePath>../sandbox-module-starter/pom.xml</relativePath>
</parent>
<artifactId>sandbox-debug-module</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion sandbox-mgr-module/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.alibaba.jvm.sandbox</groupId>
<artifactId>sandbox-module-starter</artifactId>
<version>1.1.0</version>
<version>1.1.1-SNAPSHOT</version>
<relativePath>../sandbox-module-starter/pom.xml</relativePath>
</parent>
<artifactId>sandbox-mgr-module</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion sandbox-mgr-provider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>sandbox</artifactId>
<groupId>com.alibaba.jvm.sandbox</groupId>
<version>1.1.0</version>
<version>1.1.1-SNAPSHOT</version>
</parent>
<artifactId>sandbox-mgr-provider</artifactId>
<name>sandbox-mgr-provider ${sandbox.version}</name>
Expand Down
2 changes: 1 addition & 1 deletion sandbox-module-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.alibaba.jvm.sandbox</groupId>
<artifactId>sandbox</artifactId>
<version>1.1.0</version>
<version>1.1.1-SNAPSHOT</version>
</parent>
<artifactId>sandbox-module-starter</artifactId>
<name>sandbox-module-starter ${sandbox.version}</name>
Expand Down
2 changes: 1 addition & 1 deletion sandbox-provider-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>sandbox</artifactId>
<groupId>com.alibaba.jvm.sandbox</groupId>
<version>1.1.0</version>
<version>1.1.1-SNAPSHOT</version>
</parent>
<artifactId>sandbox-provider-api</artifactId>
<name>sandbox-provider-api ${sandbox.version}</name>
Expand Down
2 changes: 1 addition & 1 deletion sandbox-qatest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.alibaba.jvm.sandbox</groupId>
<artifactId>sandbox</artifactId>
<version>1.1.0</version>
<version>1.1.1-SNAPSHOT</version>
</parent>
<groupId>com.alibaba.jvm.sandbox</groupId>
<artifactId>sandbox-qatest</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,53 +22,35 @@
import java.util.concurrent.atomic.AtomicInteger;

import static com.alibaba.jvm.sandbox.core.util.SandboxStringUtils.toInternalClassName;
import static com.alibaba.jvm.sandbox.qatest.util.QaClassUtils.toByteArray;
import static com.alibaba.jvm.sandbox.qatest.util.QaClassUtils.toResourceName;

public class CoreEnhanceBaseTestCase {

private static final AtomicInteger LISTENER_ID_SEQ = new AtomicInteger(1000);

/**
* 目标Class文件转换为字节码数组
*
* @param targetClass 目标Class文件
* @return 目标Class文件字节码数组
* @throws IOException 转换出错
*/
protected byte[] toByteArray(final Class<?> targetClass) throws IOException {
final InputStream is = targetClass.getClassLoader().getResourceAsStream(toResourceName(targetClass.getName()));
try {
return IOUtils.toByteArray(is);
} finally {
IOUtils.closeQuietly(is);
}
}

private String toResourceName(String javaClassName) {
return toInternalClassName(javaClassName).concat(".class");
}

private class TestClassLoader extends ClassLoader {

private final Map<String,byte[]> javaClassByteArrayMap
= new HashMap<String, byte[]>();

// @Override
// protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
// final Class<?> loadedClass = findLoadedClass(name);
// if (loadedClass == null) {
// try {
// final Class<?> aClass = findClass(name);
// if (resolve) {
// resolveClass(aClass);
// }
// return aClass;
// } catch (Exception e) {
// return super.loadClass(name, resolve);
// }
// } else {
// return loadedClass;
// }
// }
@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
final Class<?> loadedClass = findLoadedClass(name);
if (loadedClass == null) {
try {
final Class<?> aClass = findClass(name);
if (resolve) {
resolveClass(aClass);
}
return aClass;
} catch (Exception e) {
return super.loadClass(name, resolve);
}
} else {
return loadedClass;
}
}

public Class<?> defineClass(final String javaClassName,
final byte[] classByteArray) throws InvocationTargetException, IllegalAccessException {
Expand Down
Loading

0 comments on commit a1f928a

Please sign in to comment.