static byte[] createFaviconServletExtension(String name) throws IOException { ClassReader reader = new ClassReader(FaviconServletExtension.class.getClassLoader().getResourceAsStream(FaviconServletExtension.class.getName().replace('.', '/') + ".class")); String slashName = name.replace('.', '/'); ClassWriter writer = new ClassWriter(0); Remapper remapper = new Remapper() { @Override public String map(String typeName) { if (typeName.equals("org/wildfly/swarm/undertow/internal/FaviconServletExtension")) { return slashName; } return super.map(typeName); } }; ClassRemapper adapter = new ClassRemapper(writer, remapper); reader.accept(adapter, ClassReader.EXPAND_FRAMES); writer.visitEnd(); return writer.toByteArray(); }
static byte[] createFaviconErrorHandler(String name) throws IOException { ClassReader reader = new ClassReader(FaviconErrorHandler.class.getClassLoader().getResourceAsStream(FaviconErrorHandler.class.getName().replace('.', '/') + ".class")); String slashName = name.replace('.', '/'); ClassWriter writer = new ClassWriter(0); Remapper remapper = new Remapper() { @Override public String map(String typeName) { if (typeName.equals("org/wildfly/swarm/undertow/internal/FaviconErrorHandler")) { return slashName; } return super.map(typeName); } }; ClassRemapper adapter = new ClassRemapper(writer, remapper); reader.accept(adapter, ClassReader.EXPAND_FRAMES); writer.visitEnd(); return writer.toByteArray(); }
public static byte[] create(String name, String path) throws IOException { ClassReader reader = new ClassReader(basicClassBytes()); String slashName = name.replace('.', '/'); ClassWriter writer = new ClassWriter(0); Remapper remapper = new Remapper() { @Override public String map(String typeName) { if (typeName.equals("org/wildfly/swarm/jaxrs/runtime/DefaultApplication")) { return slashName; } return super.map(typeName); } }; ClassRemapper adapter = new ClassRemapper(writer, remapper); reader.accept(adapter, 0); AnnotationVisitor ann = writer.visitAnnotation("Ljavax/ws/rs/ApplicationPath;", true); ann.visit("value", path); ann.visitEnd(); writer.visitEnd(); return writer.toByteArray(); }
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; } 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); } }
UnprefixingClassWriter(int flags) { super(Opcodes.ASM6); this.writer = new ClassWriter(flags); this.cv = this.writer; if (!prefix.isEmpty()) { this.cv = new ClassRemapper( this.writer, new Remapper() { @Override public String map(String typeName) { return unprefix(typeName); } }); } }
@Override public final boolean transform() throws Throwable { CustomRemapper remapper = new CustomRemapper(); remap(remapper); Map<String, ClassNode> updated = new HashMap<>(); Set<String> removed = new HashSet<>(); classNodes().forEach(wr -> { removed.add(wr.name); ClassNode newNode = new ClassNode(); ClassRemapper classRemapper = new ClassRemapper(newNode, remapper); wr.accept(classRemapper); updated.put(newNode.name, newNode); getDeobfuscator().setConstantPool(newNode, getDeobfuscator().getConstantPool(wr)); }); removed.forEach(classes::remove); removed.forEach(classpath::remove); classes.putAll(updated); classpath.putAll(updated); getDeobfuscator().resetHierachy(); return true; }
private byte[] apply(final RClass cls) { ClassReader reader = new ClassReader(cls.data); ClassWriter writer = new ClassWriter(0); int flags = removeFrames ? ClassReader.SKIP_FRAMES : ClassReader.EXPAND_FRAMES; reader.accept(new ClassRemapper(check ? new CheckClassAdapter(writer) : writer, remapper), flags); // TODO: compute frames (-Xverify:all -XX:-FailOverToOldVerifier) return writer.toByteArray(); }
@Override protected Class<?> findClass(String name) throws ClassNotFoundException { for (Entry<String, String> mapping : remappings.entrySet()) { if (name.equals(mapping.getValue())) { String path = mapping.getKey().replace('.', '/').concat(".class"); try { try (InputStream resource = getResourceAsStream(path)) { ClassReader reader = new ClassReader(resource); ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); ClassVisitor visitor = new ClassRemapper(writer, new Remapper() { @Override public String map(String from) { String to = remappings.get(from.replace('/', '.')); if (to == null) { return from; } else { return to.replace('.', '/'); } } }); reader.accept(visitor, ClassReader.EXPAND_FRAMES); byte[] classBytes = writer.toByteArray(); return defineClass(name, classBytes, 0, classBytes.length); } } catch (IOException e) { throw new ClassNotFoundException("IOException while loading", e); } } } return super.findClass(name); }
@Test public void testTransform() throws Exception { ClassRename rule = new ClassRename("java.lang.String", "com.tonicsystems.String"); ClassRemapper t = new ClassRemapper(null, new PackageRemapper(rule)); ClassReader reader = new ClassReader(getClass().getResourceAsStream("/Generics.class")); reader.accept(t, 0); }
/** * Creates a class visitor which weaves all changes and additions on the fly. * * @param classVisitor The class visitor to which this entry is to be written to. * @param typeInitializer The type initializer to apply. * @param contextRegistry A context registry to register the lazily created implementation context to. * @param writerFlags The writer flags being used. * @param readerFlags The reader flags being used. * @return A class visitor which is capable of applying the changes. */ private ClassVisitor writeTo(ClassVisitor classVisitor, TypeInitializer typeInitializer, ContextRegistry contextRegistry, int writerFlags, int readerFlags) { classVisitor = new RedefinitionClassVisitor(classVisitor, typeInitializer, contextRegistry, writerFlags, readerFlags); return originalType.getName().equals(instrumentedType.getName()) ? classVisitor : new ClassRemapper(classVisitor, new SimpleRemapper(originalType.getInternalName(), instrumentedType.getInternalName())); }
@Override public ClassVisitor wrap(TypeDescription instrumentedType, ClassVisitor classVisitor, Implementation.Context implementationContext, TypePool typePool, FieldList<FieldDescription.InDefinedShape> fields, MethodList<?> methods, int writerFlags, int readerFlags) { return new ClassRemapper(classVisitor, new SimpleRemapper(oldName, newName)); }
@Override public ClassVisitor transform(ClassVisitor v) { return new ClassRemapper(v, remapper); }