use of org.objectweb.asm.ClassReader in project android_frameworks_base by ParanoidAndroid.
the class AsmAnalyzerTest method testFindClass.
@Test
public void testFindClass() throws IOException, LogAbortException {
Map<String, ClassReader> zipClasses = mAa.parseZip(mOsJarPath);
TreeMap<String, ClassReader> found = new TreeMap<String, ClassReader>();
ClassReader cr = mAa.findClass("mock_android.view.ViewGroup$LayoutParams", zipClasses, found);
assertNotNull(cr);
assertEquals("mock_android/view/ViewGroup$LayoutParams", cr.getClassName());
assertArrayEquals(new String[] { "mock_android.view.ViewGroup$LayoutParams" }, found.keySet().toArray());
assertArrayEquals(new ClassReader[] { cr }, found.values().toArray());
}
use of org.objectweb.asm.ClassReader in project android_frameworks_base by ParanoidAndroid.
the class AsmAnalyzerTest method testFindClassesDerivingFrom.
@Test
public void testFindClassesDerivingFrom() throws LogAbortException, IOException {
Map<String, ClassReader> zipClasses = mAa.parseZip(mOsJarPath);
TreeMap<String, ClassReader> found = new TreeMap<String, ClassReader>();
mAa.findClassesDerivingFrom("mock_android.view.View", zipClasses, found);
assertArrayEquals(new String[] { "mock_android.view.View", "mock_android.view.ViewGroup", "mock_android.widget.LinearLayout", "mock_android.widget.TableLayout" }, found.keySet().toArray());
for (String key : found.keySet()) {
ClassReader value = found.get(key);
assertNotNull("No value for " + key, value);
assertEquals(key, AsmAnalyzer.classReaderToClassName(value));
}
}
use of org.objectweb.asm.ClassReader in project android_frameworks_base by ParanoidAndroid.
the class ClassHasNativeVisitorTest method testHasNative.
@Test
public void testHasNative() throws IOException {
MockClassHasNativeVisitor cv = new MockClassHasNativeVisitor();
String className = this.getClass().getCanonicalName() + "$" + ClassWithNative.class.getSimpleName();
ClassReader cr = new ClassReader(className);
cr.accept(cv, 0);
assertArrayEquals(new String[] { "native_method" }, cv.getMethodsFound());
assertTrue(cv.hasNativeMethods());
}
use of org.objectweb.asm.ClassReader in project LogisticsPipes by RS485.
the class LogisticsWrapperHandler method getWrappedProxy.
@SuppressWarnings("unchecked")
public static <T> T getWrappedProxy(String modId, Class<T> interfaze, Class<? extends T> proxyClazz, T dummyProxy, Class<?>... wrapperInterfaces) throws NoSuchFieldException, SecurityException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
String proxyName = interfaze.getSimpleName().substring(1);
if (!proxyName.endsWith("Proxy")) {
throw new RuntimeException("UnuportedProxyName: " + proxyName);
}
proxyName = proxyName.substring(0, proxyName.length() - 5);
String className = "logisticspipes/asm/wrapper/generated/" + proxyName + "ProxyWrapper";
boolean ignoreModLoaded = false;
if (modId.startsWith("!")) {
ignoreModLoaded = true;
modId = modId.substring(1);
}
List<Class<?>> wrapperInterfacesList = Arrays.asList(wrapperInterfaces);
Class<?> clazz = null;
synchronized (lookupMap) {
clazz = LogisticsWrapperHandler.lookupMap.get(className);
if (clazz == null) {
String fieldName = interfaze.getName().replace('.', '/');
//String classFile = interfaze.getSimpleName().substring(1) + "Wrapper.java";
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, className, null, "logisticspipes/asm/wrapper/AbstractWrapper", new String[] { fieldName });
cw.visitSource(".LP|ASM.dynamic", null);
{
FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, "proxy", "L" + fieldName + ";", null, null);
fv.visitEnd();
}
{
FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "dummyProxy", "L" + fieldName + ";", null, null);
fv.visitEnd();
}
{
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(L" + fieldName + ";L" + fieldName + ";)V", null, null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitLineNumber(11, l0);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "logisticspipes/asm/wrapper/AbstractWrapper", "<init>", "()V");
Label l1 = new Label();
mv.visitLabel(l1);
mv.visitLineNumber(12, l1);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitFieldInsn(Opcodes.PUTFIELD, className, "dummyProxy", "L" + fieldName + ";");
Label l2 = new Label();
mv.visitLabel(l2);
mv.visitLineNumber(13, l2);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitVarInsn(Opcodes.ALOAD, 2);
mv.visitFieldInsn(Opcodes.PUTFIELD, className, "proxy", "L" + fieldName + ";");
Label l3 = new Label();
mv.visitLabel(l3);
mv.visitLineNumber(14, l3);
mv.visitInsn(Opcodes.RETURN);
Label l4 = new Label();
mv.visitLabel(l4);
mv.visitLocalVariable("this", "L" + className + ";", null, l0, l4, 0);
mv.visitLocalVariable("dProxy", "L" + fieldName + ";", null, l0, l4, 1);
mv.visitLocalVariable("iProxy", "L" + fieldName + ";", null, l0, l4, 2);
mv.visitMaxs(2, 3);
mv.visitEnd();
}
int lineAddition = 100;
for (Method method : interfaze.getMethods()) {
LogisticsWrapperHandler.addProxyMethod(cw, method, fieldName, className, lineAddition, !wrapperInterfacesList.contains(method.getReturnType()));
lineAddition += 10;
}
LogisticsWrapperHandler.addGetName(cw, className, proxyName);
LogisticsWrapperHandler.addGetTypeName(cw, className, "Proxy");
cw.visitEnd();
String lookfor = className.replace('/', '.');
byte[] bytes = cw.toByteArray();
if (LPConstants.DEBUG) {
if (LogisticsWrapperHandler.DUMP) {
LogisticsWrapperHandler.saveGeneratedClass(bytes, lookfor, "LP_WRAPPER_CLASSES");
}
ClassReader cr = new ClassReader(bytes);
org.objectweb.asm.util.CheckClassAdapter.verify(cr, Launch.classLoader, false, new PrintWriter(System.err));
}
try {
clazz = LogisticsWrapperHandler.loadClass(bytes, lookfor);
} catch (LinkageError e) {
try {
if (e.getMessage().contains("attempted") && e.getMessage().contains("duplicate class definition")) {
Class<?> prev = Class.forName(className);
System.err.println(e.getMessage());
System.err.println("Already loaded: " + String.valueOf(prev));
String resourcePath = className.replace('.', '/').concat(".class");
URL classResource = Launch.classLoader.findResource(resourcePath);
if (classResource != null) {
String path = classResource.getPath().toString();
System.err.println("Class source: " + path);
} else {
System.err.println("Class source: Null");
}
}
} catch (Exception e2) {
e2.printStackTrace();
}
throw e;
}
LogisticsWrapperHandler.lookupMap.put(className, clazz);
}
}
T proxy = null;
Throwable e = null;
if (ModStatusHelper.areModsLoaded(modId) || ignoreModLoaded) {
try {
proxy = proxyClazz.newInstance();
} catch (Exception e1) {
if (e1 instanceof VersionNotSupportedException) {
throw (VersionNotSupportedException) e1;
}
if (!(e1 instanceof DontLoadProxy)) {
e1.printStackTrace();
e = e1;
}
} catch (NoClassDefFoundError e1) {
if (!ignoreModLoaded) {
e1.printStackTrace();
e = e1;
}
}
}
T instance = (T) clazz.getConstructor(new Class<?>[] { interfaze, interfaze }).newInstance(dummyProxy, proxy);
if (proxy != null) {
LogisticsPipes.log.info("Loaded " + proxyName + "Proxy");
} else {
LogisticsPipes.log.info("Loaded " + proxyName + " DummyProxy");
if (e != null) {
((AbstractWrapper) instance).setState(WrapperState.Exception);
((AbstractWrapper) instance).setReason(e);
} else {
((AbstractWrapper) instance).setState(WrapperState.ModMissing);
}
}
((AbstractWrapper) instance).setModId(modId);
((AbstractWrapper) instance).setWrapperInterfaces(Collections.unmodifiableList(wrapperInterfacesList));
LogisticsWrapperHandler.wrapperController.add((AbstractWrapper) instance);
return instance;
}
use of org.objectweb.asm.ClassReader in project LogisticsPipes by RS485.
the class LogisticsPipesClassInjector method transform.
@Override
@SuppressWarnings("unchecked")
public byte[] transform(String name, String transformedName, byte[] bytes) {
if (bytes != null) {
if (name.startsWith("logisticspipes.")) {
final ClassReader reader = new ClassReader(bytes);
final ClassNode node = new ClassNode();
reader.accept(node, 0);
if (node.visibleAnnotations != null) {
for (AnnotationNode a : node.visibleAnnotations) {
if (a.desc.equals("Llogisticspipes/asm/ModVersionedClass;")) {
if (a.values.size() == 8 && a.values.get(0).equals("modId") && a.values.get(2).equals("version") && a.values.get(4).equals("classData") && a.values.get(6).equals("classDataDev")) {
String modId = a.values.get(1).toString();
String version = a.values.get(3).toString();
String classData = a.values.get(5).toString();
String classDataDev = a.values.get(7).toString();
if (ModStatusHelper.isModLoaded(modId) && !ModStatusHelper.isModVersionEqualsOrHigher(modId, version)) {
if (isObfEnv == null) {
try {
isObfEnv = (Class.forName("net.minecraft.world.World").getDeclaredField("chunkProvider") == null);
} catch (Throwable e) {
isObfEnv = true;
}
}
bytes = transform(name, transformedName, DatatypeConverter.parseBase64Binary(isObfEnv ? classData : classDataDev));
}
} else {
throw new UnsupportedOperationException("Can't parse the annotations correctly");
}
}
}
}
}
return bytes;
}
try {
if (name.startsWith("logisticspipes.proxy.opencomputers.asm.BaseWrapperClass$") && name.endsWith("$OpenComputersWrapper")) {
String correctName = name.substring(56, name.length() - 21);
Class<?> clazz = Launch.classLoader.findClass(correctName);
bytes = ClassCreator.getWrappedClassAsBytes(CCObjectWrapper.getWrapperInformation(clazz), clazz.getName());
Set<String> set = new TreeSet<>();
set.add(name);
Launch.classLoader.clearNegativeEntries(set);
Map<String, byte[]> map = (Map<String, byte[]>) fResourceCache.get(Launch.classLoader);
map.put(name, bytes);
return bytes;
}
} catch (Exception e) {
if (LPConstants.DEBUG) {
// For better Debugging
e.printStackTrace();
return bytes;
}
throw new RuntimeException(e);
}
return bytes;
}
Aggregations