List of usage examples for org.objectweb.asm Opcodes ASM5
int ASM5
To view the source code for org.objectweb.asm Opcodes ASM5.
Click Source Link
From source file:nl.hardijzer.fw.applysrg.Method.java
License:Open Source License
/** * @param args// w ww. j a v a2 s . com * @throws IOException */ public static void main(String[] args) throws IOException { String strInputFilename = null; String strOutputFilename = null; List<String> listInputSrg = new LinkedList<String>(); List<String> listInputInheritance = new LinkedList<String>(); List<ParseEntry> listTranslate = new LinkedList<ParseEntry>(); for (int i = 0; i < args.length; i++) { if (args[i].equals("--srg")) listInputSrg.add(args[++i]); else if (args[i].equals("--inheritance") || args[i].equals("--inh")) listInputInheritance.add(args[++i]); else if (args[i].equals("--in")) strInputFilename = args[++i]; else if (args[i].equals("--out")) strOutputFilename = args[++i]; else if (args[i].equals("--translate")) { String a = args[++i]; String b = args[++i]; listTranslate.add(new ParseEntry(a, b)); listInputInheritance.add(a); } } if (strInputFilename != null && strOutputFilename != null) { listTranslate.add(new ParseEntry(strInputFilename, strOutputFilename)); strInputFilename = strOutputFilename = null; } if (strInputFilename != null || strOutputFilename != null || listTranslate.size() < 1) { System.err.println("Usage: java -jar srgtool.jar apply [options]"); System.err.println("Options:"); System.err.println("--srg <srg file>\tLoads the SRG file"); System.err.println("--inheritance <jar/zip>\tLoads inheritance map from jar"); System.err.println("--in <jar/zip>"); System.err.println("--out <jar/zip>"); return; } if (strInputFilename != null) listInputInheritance.add(strInputFilename); Map<String, MappedClass> mapClasses = new TreeMap<String, MappedClass>(); Map<String, String> mapPackages = new TreeMap<String, String>(); for (String srg : listInputSrg) { System.out.println("Reading SRG: " + srg); BufferedReader brSrg = new BufferedReader(new FileReader(srg)); String strLine; while ((strLine = brSrg.readLine()) != null) { String arrLine[] = strLine.split(" "); if (arrLine[0].equals("PK:")) { mapPackages.put(arrLine[1], arrLine[2]); } else if (arrLine[0].equals("CL:")) { String strFrom = arrLine[1]; String strTo = arrLine[2]; MappedClass mappedCurrent = mapClasses.get(strFrom); if (mappedCurrent == null) { mapClasses.put(strFrom, new MappedClass(strTo)); } else { if (!mappedCurrent.strNewName.equals(strTo)) { System.err.println("ERROR: Mismatching mappings found"); return; } } } else if (arrLine[0].equals("FD:")) { String strFrom = arrLine[1]; String strTo = arrLine[2]; int nSplitFrom = strFrom.lastIndexOf('/'); int nSplitTo = strTo.lastIndexOf('/'); if (nSplitFrom == -1 || nSplitTo == -1) { System.err.println("ERROR: Invalid field specification: '" + strLine); return; } String strFromClass = strFrom.substring(0, nSplitFrom); strFrom = strFrom.substring(nSplitFrom + 1); String strToClass = strTo.substring(0, nSplitTo); strTo = strTo.substring(nSplitTo + 1); MappedClass mappedCurrent = mapClasses.get(strFromClass); if (strFromClass.equals(strToClass) && mappedCurrent == null) { mapClasses.put(strFromClass, mappedCurrent = new MappedClass(strToClass)); } if (mappedCurrent == null || !mappedCurrent.strNewName.equals(strToClass)) { System.err.println("ERROR: Class mapping invalid or non-existant on field"); System.err.println("Line: " + strLine); System.err.println(strFromClass + " -> " + strToClass + " should have been " + ((mappedCurrent == null) ? "null" : mappedCurrent.strNewName)); return; } mappedCurrent.mapFields.put(strFrom, strTo); } else if (arrLine[0].equals("MD:")) { String strFrom = arrLine[1]; String strFromArguments = arrLine[2]; String strTo = arrLine[3]; String strToArguments = arrLine[4]; int nSplitFrom = strFrom.lastIndexOf('/'); int nSplitTo = strTo.lastIndexOf('/'); if (nSplitFrom == -1 || nSplitTo == -1) { System.err.println("ERROR: Invalid method specification: '" + strLine); return; } String strFromClass = strFrom.substring(0, nSplitFrom); strFrom = strFrom.substring(nSplitFrom + 1); String strToClass = strTo.substring(0, nSplitTo); strTo = strTo.substring(nSplitTo + 1); MappedClass mappedCurrent = mapClasses.get(strFromClass); if (strFromClass.equals(strToClass) && mappedCurrent == null) { mapClasses.put(strFromClass, mappedCurrent = new MappedClass(strToClass)); } if (mappedCurrent == null || !mappedCurrent.strNewName.equals(strToClass)) { System.err.println("ERROR: Class mapping invalid or non-existant on method"); System.err.println("Line: " + strLine); System.err.println(strFromClass + " -> " + strToClass + " should have been " + ((mappedCurrent == null) ? "null" : mappedCurrent.strNewName)); return; } //NOTE: arguments not saved, will be mapped automagically. mappedCurrent.mapMethods.put(new Method(strFrom, strFromArguments), strTo); } } } System.out.println("Class map loaded of " + mapClasses.size() + " classes"); Map<String, ClassInfo> mapClassInheritance = new HashMap<String, ClassInfo>(); for (String inherit : listInputInheritance) { System.out.println("Parsing inheritance in " + inherit); //System.out.println("Parsing inheritance in "+inherit); ZipFile zipInherit = new ZipFile(inherit); Enumeration<? extends ZipEntry> entries = zipInherit.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (entry.isDirectory()) continue; if (entry.getName().endsWith(".class")) { ClassReader cr = new ClassReader(zipInherit.getInputStream(entry)); InheritanceMapClassVisitor cvInheritance = new InheritanceMapClassVisitor(Opcodes.ASM5); cr.accept(cvInheritance, 0); mapClassInheritance.put(cvInheritance.strName, cvInheritance.info); } } zipInherit.close(); } System.out.println("Inheritance map loaded of " + mapClassInheritance.size() + " classes"); for (ParseEntry e : listTranslate) { System.out.println("Translating " + e.strInputFilename + " -> " + e.strOutputFilename); ZipFile zipInput = new ZipFile(e.strInputFilename); ZipOutputStream zipOutput = new ZipOutputStream(new FileOutputStream(e.strOutputFilename)); Enumeration<? extends ZipEntry> entries = zipInput.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (entry.isDirectory()) continue; if (entry.getName().endsWith(".class")) { ClassReader cr = new ClassReader(zipInput.getInputStream(entry)); ClassWriter cw = new ClassWriter(0); Remapper remapper = new MyRemapper(mapClasses, mapClassInheritance, mapPackages); RemappingClassAdapter ca = new RemappingClassAdapter(cw, remapper); cr.accept(ca, ClassReader.EXPAND_FRAMES); byte[] bOutput = cw.toByteArray(); String strName = entry.getName(); strName = strName.substring(0, strName.lastIndexOf('.')); strName = remapper.mapType(strName); ZipEntry entryCopy = new ZipEntry(strName + ".class"); entryCopy.setCompressedSize(-1); entryCopy.setSize(bOutput.length); zipOutput.putNextEntry(entryCopy); zipOutput.write(bOutput); zipOutput.closeEntry(); } else { ZipEntry entryCopy = new ZipEntry(entry); entryCopy.setCompressedSize(-1); zipOutput.putNextEntry(entryCopy); InputStream is = zipInput.getInputStream(entry); byte[] buffer = new byte[1024]; int read = 0; while ((read = is.read(buffer)) != -1) zipOutput.write(buffer, 0, read); zipOutput.closeEntry(); } } zipInput.close(); zipOutput.close(); } System.out.println("Done!"); }
From source file:nl.hardijzer.fw.srgcollisions.Method.java
License:Open Source License
/** * @param args/*www . jav a2s. c o m*/ * @throws IOException */ public static void main(String[] args) throws IOException { String strInputSrg = null; String strInputJar = null; for (int i = 0; i < args.length; i++) { if (args[i].equals("--srg")) strInputSrg = args[++i]; else if (args[i].equals("--in")) strInputJar = args[++i]; else System.err.println("Unknown option: " + args[i]); } if (strInputSrg == null || strInputJar == null) { System.err.println("Usage: java -jar srgtool.jar collisions --srg file.srg --in file.jar"); return; } Map<String, MappedClass> mapClasses = new TreeMap<String, MappedClass>(); BufferedReader brSrg = new BufferedReader(new InputStreamReader(new FileInputStream(strInputSrg))); String line; while ((line = brSrg.readLine()) != null) { String[] lineParts = line.split(" "); String tmp; if (lineParts[0].equals("PK:")) { //Package, ignoring for now. We're not actually using that anywhere } else if (lineParts[0].equals("CL:")) { String strFrom = lineParts[1]; String strTo = lineParts[2]; MappedClass mapped = mapClasses.get(strFrom); if (mapped != null) { if (!mapped.strNewName.equals(strTo)) { System.err.println("Inconsistent class mapping"); return; } } else { mapClasses.put(strFrom, new MappedClass(strTo)); } } else if (lineParts[0].equals("FD:")) { String strFrom = lineParts[1]; String strTo = lineParts[2]; int nSplitFrom = strFrom.lastIndexOf('/'); int nSplitTo = strTo.lastIndexOf('/'); if (nSplitFrom == -1 || nSplitTo == -1) { System.err.println("ERROR: Invalid field specification"); return; } String strFromClass = strFrom.substring(0, nSplitFrom); strFrom = strFrom.substring(nSplitFrom + 1); String strToClass = strTo.substring(0, nSplitTo); strTo = strTo.substring(nSplitTo + 1); MappedClass mappedClass = mapClasses.get(strFromClass); if (mappedClass != null) { if (!mappedClass.strNewName.equals(strToClass)) { System.err.println("Inconsistent class mapping"); return; } } else { mapClasses.put(strFromClass, mappedClass = new MappedClass(strToClass)); } String mappedField = mappedClass.mapFields.get(strFrom); if (mappedField != null) { if (!mappedField.equals(strTo)) { System.err.println("Inconsistent field mapping"); return; } } else { mappedClass.mapFields.put(strFrom, strTo); } } else if (lineParts[0].equals("MD:")) { String strFrom = lineParts[1]; String strTo = lineParts[3]; int nSplitFrom = strFrom.lastIndexOf('/'); int nSplitTo = strTo.lastIndexOf('/'); if (nSplitFrom == -1 || nSplitTo == -1) { System.err.println("ERROR: Invalid field specification"); return; } String strFromClass = strFrom.substring(0, nSplitFrom); strFrom = strFrom.substring(nSplitFrom + 1); String strToClass = strTo.substring(0, nSplitTo); strTo = strTo.substring(nSplitTo + 1); MappedClass mappedClass = mapClasses.get(strFromClass); if (mappedClass != null) { if (!mappedClass.strNewName.equals(strToClass)) { System.err.println("Inconsistent class mapping"); return; } } else { mapClasses.put(strFromClass, mappedClass = new MappedClass(strToClass)); } Method mFrom = new Method(strFrom, lineParts[2]); Method mTo = new Method(strTo, lineParts[4]); Method mappedMethod = mappedClass.mapMethods.get(mFrom); if (mappedMethod != null) { if (mappedMethod.compareTo(mTo) != 0) { System.err.println("Inconsistent method mapping"); return; } } else { mappedClass.mapMethods.put(mFrom, mTo); } } else { System.err.println("Unknown command: '" + lineParts[0] + "', aborting"); return; } } Map<String, ClassInfo> mapInfo = new TreeMap<String, ClassInfo>(); ZipFile zipInput = new ZipFile(strInputJar); Enumeration<? extends ZipEntry> entries = zipInput.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (entry.isDirectory()) continue; if (entry.getName().endsWith(".class")) { ClassReader cr = new ClassReader(zipInput.getInputStream(entry)); ClassInfoCollector cic = new ClassInfoCollector(Opcodes.ASM5); cr.accept(cic, ClassReader.SKIP_CODE); mapInfo.put(cic.info.strName, cic.info); } } zipInput.close(); for (String strClass : mapInfo.keySet()) { ClassInfo info = mapInfo.get(strClass); MappedClass map = mapClasses.get(strClass); if (map == null) continue; Map<String, String> mapFields = new TreeMap<String, String>(); Map<Method, Method> mapMethods = new TreeMap<Method, Method>(); for (String strField : info.sFields) { String strMappedField = map.mapFields.get(strField); if (strMappedField == null) strMappedField = strField; String strPrev = mapFields.get(strMappedField); if (strPrev != null) { System.out.println( "FD: " + strClass + "/" + strPrev + " " + map.strNewName + "/" + strMappedField); System.out.println( "FD: " + strClass + "/" + strField + " " + map.strNewName + "/" + strMappedField); System.out.println(""); } else { mapFields.put(strMappedField, strField); } } for (Method mMethod : info.sMethods) { Method mMappedMethod = map.mapMethods.get(mMethod); if (mMappedMethod == null) mMappedMethod = mMethod; Method mPrev = mapMethods.get(mMappedMethod); if (mPrev != null) { System.out.println("MD: " + strClass + "/" + mPrev.name + " " + mPrev.desc + " " + map.strNewName + "/" + mMappedMethod.name + " " + mMappedMethod.desc); System.out.println("MD: " + strClass + "/" + mMethod.name + " " + mMethod.desc + " " + map.strNewName + "/" + mMappedMethod.name + " " + mMappedMethod.desc); System.out.println(""); } else { mapMethods.put(mMappedMethod, mMethod); } } } }
From source file:nova.core.wrapper.mc.forge.v17.asm.lib.InsnListPrinter.java
License:Open Source License
public InsnListPrinter() { super(Opcodes.ASM5); }
From source file:org.apache.aries.proxy.impl.common.AbstractWovenProxyAdapter.java
License:Apache License
/** * Create a new adapter for the supplied class * /*from w w w.j a va 2s. c o m*/ * @param writer * The ClassWriter to delegate to * @param className * The name of this class * @param loader * The ClassLoader loading this class */ public AbstractWovenProxyAdapter(ClassVisitor writer, String className, ClassLoader loader) { super(Opcodes.ASM5, writer); typeBeingWoven = Type.getType("L" + className.replace('.', '/') + ";"); //By default we expect to see methods from a concrete class currentMethodDeclaringType = typeBeingWoven; currentMethodDeclaringTypeIsInterface = false; this.loader = loader; }
From source file:org.apache.aries.proxy.impl.common.AbstractWovenProxyAdapter.java
License:Apache License
/** * This method is called on each method implemented on this object (but not * for superclass methods) Each of these methods is visited in turn and the * code here generates the byte code for the calls to the InovcationListener * around the existing method/* ww w . j a va 2s . co m*/ */ public final MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { LOGGER.debug(Constants.LOG_ENTRY, "visitMethod", new Object[] { access, name, desc, signature, exceptions }); Method currentMethod = new Method(name, desc); getKnownMethods().add(currentMethod); MethodVisitor methodVisitorToReturn = null; // Only weave "real" instance methods. Not constructors, initializers or // compiler generated ones. if ((access & (ACC_STATIC | ACC_PRIVATE | ACC_SYNTHETIC | ACC_NATIVE | ACC_BRIDGE)) == 0 && !!!name.equals("<init>") && !!!name.equals("<clinit>")) { // found a method we should weave //Create a field name and store it for later String methodStaticFieldName = "methodField" + getSanitizedUUIDString(); transformedMethods.put(methodStaticFieldName, new TypeMethod(currentMethodDeclaringType, currentMethod)); // Surround the MethodVisitor with our weaver so we can manipulate the code methodVisitorToReturn = getWeavingMethodVisitor(access, name, desc, signature, exceptions, currentMethod, methodStaticFieldName, currentMethodDeclaringType, currentMethodDeclaringTypeIsInterface); } else if (name.equals("<clinit>")) { //there is an existing clinit method, change the fields we use //to write our init code to static_init_UUID instead staticInitMethod = new Method("static_init_" + UU_ID, Type.VOID_TYPE, NO_ARGS); staticInitMethodFlags = staticInitMethodFlags | ACC_FINAL; methodVisitorToReturn = new AdviceAdapter(Opcodes.ASM5, cv.visitMethod(access, name, desc, signature, exceptions), access, name, desc) { @Override protected void onMethodEnter() { //add into the <clinit> a call to our synthetic static_init_UUID invokeStatic(typeBeingWoven, staticInitMethod); super.onMethodEnter(); } }; } else { if (currentMethod.getArgumentTypes().length == 0 && name.equals("<init>")) hasNoArgsConstructor = true; //This isn't a method we want to weave, so just get the default visitor methodVisitorToReturn = cv.visitMethod(access, name, desc, signature, exceptions); } LOGGER.debug(Constants.LOG_EXIT, "visitMethod", methodVisitorToReturn); return methodVisitorToReturn; }
From source file:org.apache.aries.proxy.impl.common.ConstructorFinder.java
License:Apache License
public ConstructorFinder() { super(Opcodes.ASM5); }
From source file:org.apache.aries.proxy.impl.common.MethodCopyingClassAdapter.java
License:Apache License
public MethodCopyingClassAdapter(AbstractWovenProxyAdapter awpa, ClassLoader definingLoader, Class<?> superToCopy, Type overridingClassType, Set<Method> knownMethods, Map<String, TypeMethod> transformedMethods) { super(Opcodes.ASM5); this.wovenProxyAdapter = awpa; this.superToCopy = superToCopy; this.overridingClassType = overridingClassType; this.knownMethods = knownMethods; this.transformedMethods = transformedMethods; //To be in the same package they must be loaded by the same classloader and be in the same package! if (definingLoader != superToCopy.getClassLoader()) { samePackage = false;//from w w w . j ava 2 s . c o m } else { String overridingClassName = overridingClassType.getClassName(); int lastIndex1 = superToCopy.getName().lastIndexOf('.'); int lastIndex2 = overridingClassName.lastIndexOf('.'); if (lastIndex1 != lastIndex2) { samePackage = false; } else if (lastIndex1 == -1) { samePackage = true; } else { samePackage = superToCopy.getName().substring(0, lastIndex1) .equals(overridingClassName.substring(0, lastIndex2)); } } }
From source file:org.apache.aries.proxy.impl.common.OSGiFriendlyClassVisitor.java
License:Apache License
public OSGiFriendlyClassVisitor(ClassVisitor cv, int arg1) { super(Opcodes.ASM5, cv); inlineJSR = arg1 == ClassWriter.COMPUTE_FRAMES; }
From source file:org.apache.aries.proxy.impl.gen.ProxySubclassAdapter.java
License:Apache License
public ProxySubclassAdapter(ClassVisitor writer, String newClassName, ClassLoader loader) { // call the superclass constructor super(Opcodes.ASM5, writer); // the writer is now the cv in the superclass of ClassAdapter LOGGER.debug(Constants.LOG_ENTRY, "ProxySubclassAdapter", new Object[] { this, writer, newClassName }); // set the newClassName field this.newClassName = newClassName; // set the newClassType descriptor newClassType = Type.getType("L" + newClassName + ";"); // set the classloader this.loader = loader; LOGGER.debug(Constants.LOG_EXIT, "ProxySubclassAdapter", this); }
From source file:org.apache.aries.proxy.impl.gen.ProxySubclassHierarchyAdapter.java
License:Apache License
ProxySubclassHierarchyAdapter(ProxySubclassAdapter adapter, Collection<String> methodsToImplement) { super(Opcodes.ASM5); LOGGER.debug(Constants.LOG_ENTRY, "ProxySubclassHeirarchyAdapter", new Object[] { this, adapter, methodsToImplement }); this.methodsToImplement = methodsToImplement; this.adapter = adapter; LOGGER.debug(Constants.LOG_EXIT, "ProxySubclassHeirarchyAdapter", this); }