org.objectweb.asm.commons.RemappingClassAdapter Java Examples

The following examples show how to use org.objectweb.asm.commons.RemappingClassAdapter. 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: ClassNameRemapper.java    From Concurnas with MIT License 6 votes vote down vote up
public static byte[] remapClass(byte[] resource, String nameFrom, String nameTo) {
	ClassReader reader = new ClassReader(resource);

	ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
	ClassVisitor visitor = new RemappingClassAdapter(writer, new Remapper() {
		@Override
		public String map(String from) {
			if (from.equals(nameFrom)) {
				return nameTo;
			}
			return from;
		}
	});

	reader.accept(visitor, ClassReader.EXPAND_FRAMES);
	return writer.toByteArray();
}
 
Example #2
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 #3
Source File: ApplicationFactory2.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 6 votes vote down vote up
static byte[] create(String name, String path) throws IOException {
    ClassReader reader = new ClassReader(DefaultApplication.class.getClassLoader().getResourceAsStream(DefaultApplication.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/jaxrs/internal/DefaultApplication")) {
                return slashName;
            }
            return super.map(typeName);
        }
    };

    RemappingClassAdapter adapter = new RemappingClassAdapter(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();
}
 
Example #4
Source File: FaviconExceptionMapperFactory.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 6 votes vote down vote up
static byte[] create() throws IOException {
    ClassReader reader = new ClassReader(FaviconExceptionMapper.class.getClassLoader().getResourceAsStream(FaviconExceptionMapper.class.getName().replace('.', '/') + ".class"));

    ClassWriter writer = new ClassWriter(0);
    Remapper remapper = new Remapper() {
        @Override
        public String map(String typeName) {
            if (typeName.equals("org/wildfly/swarm/jaxrs/internal/FaviconExceptionMapper")) {
                return "org/wildfly/swarm/generated/FaviconExceptionMapper";
            }
            return super.map(typeName);
        }
    };

    RemappingClassAdapter adapter = new RemappingClassAdapter(writer, remapper);
    reader.accept(adapter, 0);

    writer.visitAnnotation("Ljavax/ws/rs/ext/Provider;", true).visitEnd();
    writer.visitEnd();

    return writer.toByteArray();
}
 
Example #5
Source File: MergeAdapter.java    From Bats with Apache License 2.0 4 votes vote down vote up
public static MergedClassResult getMergedClass(final ClassSet set, final byte[] precompiledClass,
      ClassNode generatedClass, final boolean scalarReplace) {
    if (verifyBytecode) {
      if (!AsmUtil.isClassBytesOk(logger, "precompiledClass", precompiledClass)) {
        throw new IllegalStateException("Problem found in precompiledClass");
      }
      if ((generatedClass != null) && !AsmUtil.isClassOk(logger, "generatedClass", generatedClass)) {
        throw new IllegalStateException("Problem found in generatedClass");
      }
    }

    /*
     * Setup adapters for merging, remapping class names and class writing. This is done in
     * reverse order of how they will be evaluated.
     */
    final RemapClasses re = new RemapClasses(set);
    try {
      if (scalarReplace && generatedClass != null) {
        if (logger.isDebugEnabled()) {
          AsmUtil.logClass(logger, "generated " + set.generated.dot, generatedClass);
        }

        final ClassNode generatedMerged = new ClassNode();
        ClassVisitor mergeGenerator = generatedMerged;
        if (verifyBytecode) {
          mergeGenerator = new DrillCheckClassAdapter(CompilationConfig.ASM_API_VERSION,
              new CheckClassVisitorFsm(CompilationConfig.ASM_API_VERSION, generatedMerged), true);
        }

        /*
         * Even though we're effectively transforming-creating a new class in mergeGenerator,
         * there's no way to pass in ClassWriter.COMPUTE_MAXS, which would save us from having
         * to figure out stack size increases on our own. That gets handled by the
         * InstructionModifier (from inside ValueHolderReplacement > ScalarReplacementNode).
         */
        generatedClass.accept(new ValueHolderReplacementVisitor(mergeGenerator, verifyBytecode));
        if (verifyBytecode) {
          if (!AsmUtil.isClassOk(logger, "generatedMerged", generatedMerged)) {
            throw new IllegalStateException("Problem found with generatedMerged");
          }
        }
        generatedClass = generatedMerged;
      }

      final ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
      ClassVisitor writerVisitor = writer;
      if (verifyBytecode) {
        writerVisitor = new DrillCheckClassAdapter(CompilationConfig.ASM_API_VERSION,
            new CheckClassVisitorFsm(CompilationConfig.ASM_API_VERSION, writerVisitor), true);
      }
      ClassVisitor remappingAdapter = new RemappingClassAdapter(writerVisitor, re);
      if (verifyBytecode) {
        remappingAdapter = new DrillCheckClassAdapter(CompilationConfig.ASM_API_VERSION,
            new CheckClassVisitorFsm(CompilationConfig.ASM_API_VERSION, remappingAdapter), true);
      }

      ClassVisitor visitor = remappingAdapter;
      if (generatedClass != null) {
        visitor = new MergeAdapter(set, remappingAdapter, generatedClass);
      }
      ClassReader tReader = new ClassReader(precompiledClass);
      tReader.accept(visitor, ClassReader.SKIP_FRAMES);
      byte[] outputClass = writer.toByteArray();
      if (logger.isDebugEnabled()) {
        AsmUtil.logClassFromBytes(logger, "merged " + set.generated.dot, outputClass);
      }

      // enable when you want all the generated merged class files to also be written to disk.
//      try {
//        File destDir = new File( "/tmp/scratch/drill-generated-classes" );
//        destDir.mkdirs();
//        Files.write(outputClass, new File(destDir, String.format("%s-output.class", set.generated.dot)));
//      } catch (IOException e) {
//        // Ignore;
//      }

      return new MergedClassResult(outputClass, re.getInnerClasses());
    } catch (Error | RuntimeException e) {
      logger.error("Failure while merging classes.", e);
      AsmUtil.logClass(logger, "generatedClass", generatedClass);
      throw e;
    }
  }
 
Example #6
Source File: ObfuscationRun.java    From CodeChickenCore with MIT License 4 votes vote down vote up
public void remap(ClassNode cnode, ClassVisitor cv)
{
    cstMappper.transform(cnode);
    cnode.accept(new RemappingClassAdapter(cv, obfMapper));
}