Java 类org.objectweb.asm.commons.ClassRemapper 实例源码

项目:wildfly-swarm    文件:FaviconFactory.java   
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();
}
项目:wildfly-swarm    文件:FaviconFactory.java   
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();
}
项目:wildfly-swarm    文件:DefaultApplicationFactory.java   
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();
}
项目:java-asm-obfuscator    文件:BytecodeHelper.java   
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);
    }
}
项目:bazel    文件:CoreLibraryRewriter.java   
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);
              }
            });
  }
}
项目:deobfuscator    文件:AbstractNormalizer.java   
@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;
}
项目:tiny-remapper    文件:TinyRemapper.java   
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();
}
项目:offheap-store    文件:SerializablePortabilityTestUtilities.java   
@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);
}
项目:bazel    文件:GenericsTest.java   
@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);
}
项目:byte-buddy    文件:TypeWriter.java   
/**
 * 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()));
}
项目:byte-buddy    文件:ByteArrayClassLoaderChildFirstTest.java   
@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));
}
项目:bazel    文件:RemappingClassTransformer.java   
@Override
public ClassVisitor transform(ClassVisitor v) {
    return new ClassRemapper(v, remapper);
}