Пример #1
0
  static void optimize(final File f, final File d, final Remapper remapper) throws IOException {
    if (f.isDirectory()) {
      File[] files = f.listFiles();
      for (int i = 0; i < files.length; ++i) {
        optimize(files[i], d, remapper);
      }
    } else if (f.getName().endsWith(".class")) {
      ConstantPool cp = new ConstantPool();
      ClassReader cr = new ClassReader(new FileInputStream(f));
      // auto-boxing removal requires to recompute the maxs
      ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
      ClassConstantsCollector ccc = new ClassConstantsCollector(cw, cp);
      ClassOptimizer co = new ClassOptimizer(ccc, remapper);
      cr.accept(co, ClassReader.SKIP_DEBUG);

      Set<Constant> constants = new TreeSet<Constant>(new ConstantComparator());
      constants.addAll(cp.values());

      cr = new ClassReader(cw.toByteArray());
      cw = new ClassWriter(0);
      Iterator<Constant> i = constants.iterator();
      while (i.hasNext()) {
        Constant c = i.next();
        c.write(cw);
      }
      cr.accept(cw, ClassReader.SKIP_DEBUG);

      if (MAPPING.get(cr.getClassName() + "/remove") != null) {
        return;
      }
      String n = remapper.mapType(cr.getClassName());
      File g = new File(d, n + ".class");
      if (!g.exists() || g.lastModified() < f.lastModified()) {
        if (!g.getParentFile().exists() && !g.getParentFile().mkdirs()) {
          throw new IOException("Cannot create directory " + g.getParentFile());
        }
        OutputStream os = new FileOutputStream(g);
        try {
          os.write(cw.toByteArray());
        } finally {
          os.close();
        }
      }
    }
  }
Пример #2
0
  public static ClassMetadata scan(ClassReader source) {
    ClassMetadata info = new ClassMetadata();
    source.accept(new MetaScanner(info), 0);
    info.type = Type.getObjectType(source.getClassName());

    for (FieldDescriptor fd : info.fields) {
      if ((fd.access & ACC_PUBLIC) == ACC_PUBLIC) {
        info.directFieldAccess = true;
      }
    }

    return info;
  }
  /**
   * Runs ASM on the provider byteCode, chaining a reader to a writer and using the {@code
   * ClassVisitor} you yourself provide via the {@see #createClassVisitor(ClassWriter)} method as
   * the filter.
   */
  protected byte[] runASM(
      byte[] byteCode, boolean computeStacks, TransplantMapper transplantMapper) {
    ClassReader reader = new ClassReader(byteCode);
    int classFileFormatVersion = 48;
    if (byteCode.length > 7) classFileFormatVersion = byteCode[7] & 0xFF;

    int flags = classFileFormatVersion < 50 ? ClassWriter.COMPUTE_FRAMES : 0;
    if (computeStacks) flags |= ClassWriter.COMPUTE_MAXS;
    ClassWriter writer = new FixedClassWriter(reader, flags);

    ClassVisitor visitor = createClassVisitor(writer, reader.getClassName(), transplantMapper);
    reader.accept(visitor, 0);
    return writer.toByteArray();
  }
Пример #4
0
  private static Class loadCompiledScriptFromClass(Ruby runtime, InputStream in)
      throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buf = new byte[8192];
    int read;
    while ((read = in.read(buf)) != -1) {
      baos.write(buf, 0, read);
    }
    buf = baos.toByteArray();
    JRubyClassLoader jcl = runtime.getJRubyClassLoader();
    OneShotClassLoader oscl = new OneShotClassLoader(jcl);

    ClassReader cr = new ClassReader(buf);
    String className = cr.getClassName().replace('/', '.');

    return oscl.defineClass(className, buf);
  }
Пример #5
0
  public static void parse(File sourceFile) throws Exception {
    if (ByteCodeTranslator.verbose) {
      System.out.println("Parsing: " + sourceFile.getAbsolutePath());
    }
    ClassReader r = new ClassReader(new FileInputStream(sourceFile));
    /*if(ByteCodeTranslator.verbose) {
        System.out.println("Class: " + r.getClassName() + " derives from: " + r.getSuperName() + " interfaces: " + Arrays.asList(r.getInterfaces()));
    }*/
    Parser p = new Parser();
    p.clsName = r.getClassName().replace('/', '_').replace('$', '_');
    if (p.clsName.startsWith("java_lang_annotation")
        || p.clsName.startsWith("java_lang_Deprecated")
        || p.clsName.startsWith("java_lang_Override")
        || p.clsName.startsWith("java_lang_SuppressWarnings")) {
      return;
    }
    p.cls = new ByteCodeClass(p.clsName);
    r.accept(p, ClassReader.EXPAND_FRAMES);

    classes.add(p.cls);
  }
Пример #6
0
 public static String getClassName(Resource res) throws IOException {
   byte[] src = IOUtil.toBytes(res);
   ClassReader cr = new ClassReader(src);
   return cr.getClassName();
 }