List of usage examples for org.objectweb.asm.tree ClassNode ClassNode
public ClassNode()
From source file:com.github.jasmo.obfuscate.Obfuscator.java
License:Open Source License
private void supplyJar(JarFile jar) { Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); try (InputStream in = jar.getInputStream(entry)) { byte[] bytes; try (ByteArrayOutputStream tmp = new ByteArrayOutputStream()) { byte[] buf = new byte[256]; for (int n; (n = in.read(buf)) != -1;) { tmp.write(buf, 0, n); }/* w w w.j a v a2 s. com*/ bytes = tmp.toByteArray(); } if (!entry.getName().endsWith(".class")) { getFiles().put(entry.getName(), bytes); continue; } ClassNode c = new ClassNode(); new ClassReader(bytes).accept(c, getReadFlags()); getClassMap().put(c.name, c); } catch (IOException e) { e.printStackTrace(); } } }
From source file:com.github.jasmo.obfuscate.Obfuscator.java
License:Open Source License
public void supply(Path root) throws IOException { if (root.toString().endsWith(".jar")) { log.debug("Supplying jar file: {}", root); supplyJar(new JarFile(root.toAbsolutePath().toString())); return;/* w ww . j a v a2 s.co m*/ } if (!Files.isDirectory(root)) throw new IOException("Cannot specify files, only classpath root directory."); log.debug("Walking file tree: {}", root); Files.walkFileTree(root, new HashSet<>(), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (file.toString().endsWith(".jar")) { log.debug(" Supplying jar file: {}", file); supplyJar(new JarFile(file.toAbsolutePath().toString())); return FileVisitResult.CONTINUE; } String relative = root.relativize(file).toString(); byte[] bytes = Files.readAllBytes(file); if (relative.endsWith(".class")) { log.debug(" Class found: {}", relative); ClassNode node = new ClassNode(); ClassReader reader = new ClassReader(bytes); reader.accept(node, getReadFlags()); getClassMap().put(node.name, node); } else { log.debug(" File found: {}", relative); getFiles().put(relative, bytes); } return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { return FileVisitResult.CONTINUE; } }); }
From source file:com.github.jasmo.obfuscate.RemoveDebugInfo.java
License:Open Source License
@Override public void transform(Map<String, ClassNode> classMap) { Map<String, ClassNode> map = new HashMap<>(); for (ClassNode cn : classMap.values()) { log.debug("Removing debug info from class: {}", cn.name); ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); cn.accept(writer);/*www . j ava 2 s. co m*/ ClassNode clone = new ClassNode(); new ClassReader(writer.toByteArray()).accept(clone, ClassReader.SKIP_DEBUG); map.put(clone.name, clone); } classMap.clear(); classMap.putAll(map); }
From source file:com.github.jasmo.util.BytecodeHelper.java
License:Open Source License
public static void applyMappings(Map<String, ClassNode> classMap, Map<String, String> remap) { log.debug("Applying mappings ["); for (Map.Entry<String, String> entry : remap.entrySet()) { String k = entry.getKey(); String v = entry.getValue(); if (k.equals(v)) continue; // skip members with same name // field format = [ "<owner>.<name>" : "<newname>" ] // method format = [ "<owner>.<name> <desc>" : "<newname>" ] int n = k.indexOf('.'); if (n != -1 && v.length() >= n && v.substring(n).equals(k)) { continue; }//from w w w . j ava 2 s . com log.debug(" Map {} to {}", entry.getKey(), entry.getValue()); } log.debug("]"); SimpleRemapper remapper = new SimpleRemapper(remap); for (ClassNode node : new ArrayList<>(classMap.values())) { ClassNode copy = new ClassNode(); ClassRemapper adapter = new ClassRemapper(copy, remapper); node.accept(adapter); classMap.put(node.name, copy); } }
From source file:com.github.jasmo.util.ClassPath.java
License:Open Source License
private void append(String jarPath) { log.debug("Loading library from classpath: {}", jarPath); try (JarFile jar = new JarFile(jarPath)) { Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); try (InputStream in = jar.getInputStream(entry)) { if (entry.getName().endsWith(".class")) { byte[] bytes; try (ByteArrayOutputStream tmp = new ByteArrayOutputStream()) { byte[] buf = new byte[256]; for (int n; (n = in.read(buf)) != -1;) { tmp.write(buf, 0, n); }//from w ww. ja va2 s.co m bytes = tmp.toByteArray(); } ClassNode c = new ClassNode(); ClassReader r = new ClassReader(bytes); r.accept(c, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG); put(c.name, c); } } } } catch (IOException e) { log.error("An error occurred while reading jar file: {}", jarPath, e); } }
From source file:com.github.veithen.cosmos.osgi.runtime.Patcher.java
License:Apache License
static synchronized void patch() throws BundleException { if (patched) { return;/*w ww.j a v a 2 s.c o m*/ } try { ClassLoader classLoader = Patcher.class.getClassLoader(); Method defineClass = ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class); defineClass.setAccessible(true); ClassNode classNode; try (InputStream in = classLoader .getResourceAsStream("com/github/veithen/cosmos/osgi/runtime/FrameworkUtil.class")) { classNode = new ClassNode(); new ClassReader(in).accept(classNode, 0); } ClassWriter classWriter; try (InputStream in = classLoader.getResourceAsStream("org/osgi/framework/FrameworkUtil.class")) { ClassReader classReader = new ClassReader(in); classWriter = new ClassWriter(classReader, 0); classReader.accept(new MemberInjector(classWriter, classNode), 0); } byte[] bytes = classWriter.toByteArray(); defineClass.invoke(classLoader, "org.osgi.framework.FrameworkUtil", bytes, 0, bytes.length); patched = true; } catch (IOException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) { throw new BundleException("Failed to patch FrameworkUtil", ex); } }
From source file:com.github.wreulicke.bean.validation.ASMMethodParameterValidationInjectorTest.java
License:Open Source License
@Test public void firstCase() throws IOException { ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES + ClassWriter.COMPUTE_MAXS); ClassReader reader = new ClassReader(getByteCode(Example.class)); ClassNode node = new ClassNode(); reader.accept(node, ClassReader.EXPAND_FRAMES); ASMMethodParameterValidationInjector injector = new ASMMethodParameterValidationInjector(); injector.inject(node);/*from ww w. j ava 2 s. c o m*/ node.accept(writer); byte[] result = writer.toByteArray(); // ByteCodes.dumpAndDecomplie((p) -> p.resolve("Example.class"), getByteCode(Example.class)); }
From source file:com.github.wreulicke.bean.validation.ASMNotNullInstrumentation.java
License:Open Source License
@Override public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES + ClassWriter.COMPUTE_MAXS); ClassReader reader = new ClassReader(classfileBuffer); ClassNode node = new ClassNode(); reader.accept(node, ClassReader.EXPAND_FRAMES); new InnerNotNullInstrumentation(node).visit(); node.accept(writer);/*from w ww. j av a 2 s .c o m*/ return writer.toByteArray(); }
From source file:com.google.devtools.build.android.desugar.GeneratedClassStore.java
License:Open Source License
/** * Adds a class for the given internal name. It's the caller's responsibility to {@link * ClassVisitor#visit} the returned object to initialize the desired class, and to avoid * confusion, this method throws if the class had already been present. *//*from w w w . j a v a 2 s. c o m*/ public ClassVisitor add(String internalClassName) { ClassNode result = new ClassNode(); checkState(classes.put(internalClassName, result) == null, "Already present: %s", internalClassName); return result; }
From source file:com.google.template.soy.jbcsrc.ClassData.java
License:Apache License
/** * Runs the {@link CheckClassAdapter} on this class in basic analysis mode. * * <p>Basic anaylsis mode can flag verification errors that don't depend on knowing complete type * information for the classes and methods being called. This is useful for flagging simple * generation mistakes (e.g. stack underflows, method return type mismatches, accessing invalid * locals). Additionally, the error messages are more useful than what the java verifier normally * presents./* w w w . j a va 2 s . c o m*/ */ void checkClass() { ClassNode cv = new ClassNode(); new ClassReader(data).accept(new CheckClassAdapter(cv, true /* check data flow */), 0); // double check our fields while we are here. checkState(type.internalName().equals(cv.name)); checkState(numberOfFields == cv.fields.size()); }