forked from alibaba/jvm-sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
03ebfc6
commit 8b07856
Showing
7 changed files
with
149 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
sandbox-core/src/main/java/com/alibaba/jvm/sandbox/core/util/AsmUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.alibaba.jvm.sandbox.core.util; | ||
|
||
import com.alibaba.jvm.sandbox.core.util.matcher.structure.ClassStructure; | ||
import com.alibaba.jvm.sandbox.core.util.matcher.structure.ClassStructureFactory; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
import java.io.InputStream; | ||
|
||
import static com.alibaba.jvm.sandbox.core.util.SandboxStringUtils.toInternalClassName; | ||
|
||
/** | ||
* ASM工具集 | ||
* | ||
* @author luanjia@taobao.com | ||
*/ | ||
public class AsmUtils { | ||
|
||
/** | ||
* {@see org.objectweb.asm.ClassWriter#getCommonSuperClass(String, String)} | ||
*/ | ||
public static String getCommonSuperClass(String type1, String type2, ClassLoader loader) { | ||
return getCommonSuperClassImplByAsm(type1, type2, loader); | ||
} | ||
|
||
// implements by ASM | ||
private static String getCommonSuperClassImplByAsm(String type1, String type2, ClassLoader targetClassLoader) { | ||
InputStream inputStreamOfType1 = null, inputStreamOfType2 = null; | ||
try { | ||
inputStreamOfType1 = targetClassLoader.getResourceAsStream(type1 + ".class"); | ||
if (null == inputStreamOfType1) { | ||
return "java/lang/Object"; | ||
} | ||
inputStreamOfType2 = targetClassLoader.getResourceAsStream(type2 + ".class"); | ||
if (null == inputStreamOfType2) { | ||
return "java/lang/Object"; | ||
} | ||
final ClassStructure classStructureOfType1 = ClassStructureFactory.createClassStructure(inputStreamOfType1, targetClassLoader); | ||
final ClassStructure classStructureOfType2 = ClassStructureFactory.createClassStructure(inputStreamOfType2, targetClassLoader); | ||
if (classStructureOfType2.getFamilyTypeClassStructures().contains(classStructureOfType1)) { | ||
return type1; | ||
} | ||
if (classStructureOfType1.getFamilyTypeClassStructures().contains(classStructureOfType2)) { | ||
return type2; | ||
} | ||
if (classStructureOfType1.getAccess().isInterface() | ||
|| classStructureOfType2.getAccess().isInterface()) { | ||
return "java/lang/Object"; | ||
} | ||
ClassStructure classStructure = classStructureOfType1; | ||
do { | ||
classStructure = classStructure.getSuperClassStructure(); | ||
if (null == classStructure) { | ||
return "java/lang/Object"; | ||
} | ||
} while (!classStructureOfType2.getFamilyTypeClassStructures().contains(classStructure)); | ||
return toInternalClassName(classStructure.getJavaClassName()); | ||
} finally { | ||
IOUtils.closeQuietly(inputStreamOfType1); | ||
IOUtils.closeQuietly(inputStreamOfType2); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
sandbox-core/src/test/java/com/alibaba/jvm/sandbox/qatest/core/issues/Issues133.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.alibaba.jvm.sandbox.qatest.core.issues; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.io.InputStream; | ||
|
||
import static com.alibaba.jvm.sandbox.core.util.AsmUtils.getCommonSuperClass; | ||
|
||
public class Issues133 { | ||
|
||
@Test | ||
public void test() { | ||
final ClassLoader loader = getClass().getClassLoader(); | ||
Assert.assertEquals("java/io/InputStream", getCommonSuperClass("java/io/FileInputStream", "javax/servlet/ServletInputStream", loader)); | ||
Assert.assertEquals("java/lang/Exception", getCommonSuperClass("java/io/IOException", "javax/servlet/ServletException", loader)); | ||
Assert.assertEquals("javax/servlet/ServletResponse", getCommonSuperClass("javax/servlet/ServletResponse", "javax/servlet/http/HttpServletResponse", loader)); | ||
Assert.assertEquals("javax/servlet/ServletResponse", getCommonSuperClass("javax/servlet/http/HttpServletResponse", "javax/servlet/ServletResponse", loader)); | ||
Assert.assertEquals("java/lang/Object", getCommonSuperClass("java/lang/Throwable", "java/io/FileInputStream", loader)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters