org.objectweb.asm.commons.SimpleRemapper Java Examples

The following examples show how to use org.objectweb.asm.commons.SimpleRemapper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClazzReplacer.java    From atlas with Apache License 2.0 6 votes vote down vote up
@Override
protected void handleClazz(JarFile jarFile, JarOutputStream jos, JarEntry ze, String pathName) {

    //            logger.log(pathName);
    if (reMapping.containsKey(pathName.substring(0, pathName.length() - 6))) {
        logger.info("[ClazzReplacer] remove class  " + pathName + " form " + jarFile);
        return;
    }

    try {
        InputStream in = jarFile.getInputStream(ze);
        ClassReader cr = new ClassReader(in);
        ClassWriter cw = new ClassWriter(0);
        RemappingClassAdapter remappingClassAdapter = new RemappingClassAdapter(cw, new SimpleRemapper(reMapping));
        cr.accept(remappingClassAdapter, ClassReader.EXPAND_FRAMES);

        InputStream inputStream = new ByteArrayInputStream(cw.toByteArray());
        copyStreamToJar(inputStream, jos, pathName, ze.getTime());

    } catch (Throwable e) {

        System.err.println("[ClazzReplacer] rewrite error > " + pathName);
        justCopy(jarFile, jos, ze, pathName);
    }

}
 
Example #2
Source File: WeavingClassVisitor.java    From glowroot with Apache License 2.0 6 votes vote down vote up
@RequiresNonNull("type")
private void addMixin(ClassNode mixinClassNode) {
    List<FieldNode> fieldNodes = mixinClassNode.fields;
    for (FieldNode fieldNode : fieldNodes) {
        if (!Modifier.isTransient(fieldNode.access)) {
            // this is needed to avoid serialization issues (even if the new field is
            // serializable, this can still cause issues in a cluster if glowroot is not
            // deployed on all nodes)
            throw new IllegalStateException(
                    "@Mixin fields must be marked transient: " + mixinClassNode.name);
        }
        fieldNode.accept(this);
    }
    List<MethodNode> methodNodes = mixinClassNode.methods;
    for (MethodNode mn : methodNodes) {
        if (mn.name.equals("<init>")) {
            continue;
        }
        String[] exceptions = Iterables.toArray(mn.exceptions, String.class);
        MethodVisitor mv =
                cw.visitMethod(mn.access, mn.name, mn.desc, mn.signature, exceptions);
        mn.accept(new MethodRemapper(mv,
                new SimpleRemapper(mixinClassNode.name, type.getInternalName())));
    }
}
 
Example #3
Source File: Renamer.java    From AVM with MIT License 5 votes vote down vote up
private static Map<String, ClassNode> applyMapping(Map<String, ClassNode> classMap, Map<String, String> classNameMap) {
    SimpleRemapper remapper = new SimpleRemapper(classNameMap);
    Map<String, ClassNode> newClassMap = new HashMap<>();
    for (ClassNode node : classMap.values()) {
        ClassNode copy = new ClassNode();
        ClassRemapper adapter = new ClassRemapper(copy, remapper);
        node.accept(adapter);
        newClassMap.put(copy.name, copy);
    }
    return newClassMap;
}
 
Example #4
Source File: MergeAdapter.java    From JReFrameworker with MIT License 5 votes vote down vote up
/**
 * Adds the method to the base class
 * @param methodNode
 */
private void addMethod(MethodNode methodNode){
	String[] exceptions = new String[methodNode.exceptions.size()];
	methodNode.exceptions.toArray(exceptions);
	MethodVisitor mv = cv.visitMethod(methodNode.access, methodNode.name, methodNode.desc, methodNode.signature, exceptions);

	methodNode.instructions.resetLabels();
	// SimpleRemapper -> maps old name to new name
	// updates owners and descriptions appropriately
	methodNode.accept(new RemappingMethodAdapter(methodNode.access, methodNode.desc, mv, new SimpleRemapper(classToMerge.name, baseClassName)));
}
 
Example #5
Source File: MergeAdapter.java    From JReFrameworker with MIT License 5 votes vote down vote up
/**
 * Adds the method to the base class
 * @param methodNode
 */
private void addMethod(MethodNode methodNode){
	String[] exceptions = new String[methodNode.exceptions.size()];
	methodNode.exceptions.toArray(exceptions);
	MethodVisitor mv = cv.visitMethod(methodNode.access, methodNode.name, methodNode.desc, methodNode.signature, exceptions);

	methodNode.instructions.resetLabels();
	// SimpleRemapper -> maps old name to new name
	// updates owners and descriptions appropriately
	methodNode.accept(new RemappingMethodAdapter(methodNode.access, methodNode.desc, mv, new SimpleRemapper(classToMerge.name, baseClassName)));
}
 
Example #6
Source File: ByteArrayClassLoaderChildFirstTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
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(OpenedClassReader.ASM_API, classVisitor, new SimpleRemapper(oldName, newName)) {
        /* only anonymous to define usage of Byte Buddy specific API version */
    };
}
 
Example #7
Source File: MergeAdapter.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public void visitEnd() {
  // add all the fields of the class we're going to merge.
  for (Iterator<?> it = classToMerge.fields.iterator(); it.hasNext();) {

    // Special handling for nested classes. Drill uses non-static nested
    // "inner" classes in some templates. Prior versions of Drill would
    // create the generated nested classes as static, then this line
    // would copy the "this$0" field to convert the static nested class
    // into a non-static inner class. However, that approach is not
    // compatible with plain-old Java compilation. Now, Drill generates
    // the nested classes as non-static inner classes. As a result, we
    // do not want to copy the hidden fields; we'll end up with two if
    // we do.

    FieldNode field = (FieldNode) it.next();
    if (! field.name.startsWith("this$")) {
      field.accept(this);
    }
  }

  // add all the methods that we to include.
  for (Iterator<?> it = classToMerge.methods.iterator(); it.hasNext();) {
    MethodNode mn = (MethodNode) it.next();

    if (mn.name.equals("<init>")) {
      continue;
    }

    String[] exceptions = new String[mn.exceptions.size()];
    mn.exceptions.toArray(exceptions);
    MethodVisitor mv = cv.visitMethod(mn.access | Modifier.FINAL, mn.name, mn.desc, mn.signature, exceptions);
    if (verifyBytecode) {
      mv = new CheckMethodVisitorFsm(api, mv);
    }

    mn.instructions.resetLabels();

    // mn.accept(new RemappingMethodAdapter(mn.access, mn.desc, mv, new
    // SimpleRemapper("org.apache.drill.exec.compile.ExampleTemplate", "Bunky")));
    ClassSet top = set;
    while (top.parent != null) {
      top = top.parent;
    }
    mn.accept(new RemappingMethodAdapter(mn.access, mn.desc, mv,
        new SimpleRemapper(top.precompiled.slash, top.generated.slash)));

  }
  super.visitEnd();
}
 
Example #8
Source File: LambdaFactoryTest.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
private void renameClass(ClassReader reader, ClassVisitor classVisitor) {
    String className = "com/navercorp/pinpoint/profiler/instrument/lambda/mock/UnsafeClassMock";
    Remapper remapper = new SimpleRemapper(className,className + "2");
    ClassRemapper classRemapper = new ClassRemapper(classVisitor, remapper);
    reader.accept(classRemapper, 0);
}