org.jetbrains.java.decompiler.struct.lazy.LazyLoader Java Examples

The following examples show how to use org.jetbrains.java.decompiler.struct.lazy.LazyLoader. 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: ContextUnit.java    From JByteMod-Beta with GNU General Public License v2.0 6 votes vote down vote up
public void reload(LazyLoader loader) throws IOException {
  List<StructClass> lstClasses = new ArrayList<>();

  for (StructClass cl : classes) {
    String oldName = cl.qualifiedName;

    StructClass newCl;
    try (DataInputFullStream in = loader.getClassStream(oldName)) {
      newCl = new StructClass(in, cl.isOwn(), loader);
    }

    lstClasses.add(newCl);

    Link lnk = loader.getClassLink(oldName);
    loader.removeClassLink(oldName);
    loader.addClassLink(newCl.qualifiedName, lnk);
  }

  classes = lstClasses;
}
 
Example #2
Source File: Fernflower.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
public Fernflower(IBytecodeProvider provider, IResultSaver saver, Map<String, Object> customProperties, IFernflowerLogger logger) {
    Map<String, Object> properties = new HashMap<>(IFernflowerPreferences.DEFAULTS);
    if (customProperties != null) {
        properties.putAll(customProperties);
    }

    String level = (String) properties.get(IFernflowerPreferences.LOG_LEVEL);
    if (level != null) {
        try {
            logger.setSeverity(IFernflowerLogger.Severity.valueOf(level.toUpperCase(Locale.US)));
        } catch (IllegalArgumentException ignore) {
        }
    }

    structContext = new StructContext(saver, this, new LazyLoader(provider));
    classProcessor = new ClassesProcessor(structContext);

    PoolInterceptor interceptor = null;
    if ("1".equals(properties.get(IFernflowerPreferences.RENAME_ENTITIES))) {
        helper = loadHelper((String) properties.get(IFernflowerPreferences.USER_RENAMER_CLASS), logger);
        interceptor = new PoolInterceptor();
        converter = new IdentifierConverter(structContext, helper, interceptor);
    } else {
        helper = null;
        converter = null;
    }

    DecompilerContext context = new DecompilerContext(properties, logger, structContext, classProcessor, interceptor);
    DecompilerContext.setCurrentContext(context);
}
 
Example #3
Source File: StructContextDecorator.java    From Recaf with MIT License 5 votes vote down vote up
private void addResource(JavaResource resource, LazyLoader loader) throws IOException {
	// Iterate resource class entries
	for(Map.Entry<String, byte[]> entry : copySet(resource.getClasses().entrySet())) {
		String name = entry.getKey();
		byte[] code = entry.getValue();
		// register class in the map and lazy-loader.
		getClasses().put(name, new StructClass(code, true, loader));
		loader.addClassLink(name, new LazyLoader.Link(null, name + ".class"));
	}
}
 
Example #4
Source File: FernFlowerAccessor.java    From Recaf with MIT License 5 votes vote down vote up
/**
 * Constructs a FernFlower decompiler instance.
 *
 * @param provider
 * 		Class file provider.
 * @param saver
 * 		Decompilation output saver <i>(Unused/noop)</i>
 * @param properties
 * 		FernFlower options.
 * @param logger
 * 		FernFlower logger instance.
 */
public FernFlowerAccessor(IBytecodeProvider provider, IResultSaver saver, Map<String, Object>
		properties, IFernflowerLogger logger) {
	String level = (String) properties.get(IFernflowerPreferences.LOG_LEVEL);
	if(level != null) {
		logger.setSeverity(IFernflowerLogger.Severity.valueOf(level.toUpperCase(Locale.ENGLISH)));
	}
	structContext = new StructContextDecorator(saver, this, new LazyLoader(provider));
	classProcessor = new ClassesProcessor(structContext);
	DecompilerContext context = new DecompilerContext(properties, logger, structContext,
			classProcessor, null);
	DecompilerContext.setCurrentContext(context);
}
 
Example #5
Source File: StructContext.java    From JByteMod-Beta with GNU General Public License v2.0 5 votes vote down vote up
public StructContext(IResultSaver saver, IDecompiledData decompiledData, LazyLoader loader) {
  this.saver = saver;
  this.decompiledData = decompiledData;
  this.loader = loader;
  ContextUnit defaultUnit = new ContextUnit(ContextUnit.TYPE_FOLDER, null, "", true, saver, decompiledData);
  units.put("", defaultUnit);
}
 
Example #6
Source File: StructContext.java    From JByteMod-Beta with GNU General Public License v2.0 5 votes vote down vote up
private void addArchive(String path, File file, int type, boolean isOwn) throws IOException {
  //noinspection IOResourceOpenedButNotSafelyClosed
  try (ZipFile archive = type == ContextUnit.TYPE_JAR ? new JarFile(file) : new ZipFile(file)) {
    Enumeration<? extends ZipEntry> entries = archive.entries();
    while (entries.hasMoreElements()) {
      ZipEntry entry = entries.nextElement();

      ContextUnit unit = units.get(path + "/" + file.getName());
      if (unit == null) {
        unit = new ContextUnit(type, path, file.getName(), isOwn, saver, decompiledData);
        if (type == ContextUnit.TYPE_JAR) {
          unit.setManifest(((JarFile) archive).getManifest());
        }
        units.put(path + "/" + file.getName(), unit);
      }

      String name = entry.getName();
      if (!entry.isDirectory()) {
        if (name.endsWith(".class")) {
          byte[] bytes = InterpreterUtil.getBytes(archive, entry);
          StructClass cl = new StructClass(bytes, isOwn, loader);
          classes.put(cl.qualifiedName, cl);
          unit.addClass(cl, name);
          loader.addClassLink(cl.qualifiedName, new LazyLoader.Link(LazyLoader.Link.ENTRY, file.getAbsolutePath(), name));
        } else {
          unit.addOtherEntry(file.getAbsolutePath(), name);
        }
      } else {
        unit.addDirEntry(name);
      }
    }
  }
}
 
Example #7
Source File: FernflowerDecompiler.java    From JByteMod-Beta with GNU General Public License v2.0 5 votes vote down vote up
public String decompile(byte[] b, MethodNode mn) {
  try {
  	//TODO decompile method only
    this.bytes = b;
    HashMap<String, Object> map = new HashMap<>();
    for (String key : options.keySet()) {
      map.put(key, JByteMod.ops.get("ff_" + key).getBoolean() ? "1" : "0");
    }
    Fernflower f = new Fernflower(this, this, map, new PrintStreamLogger(JByteMod.LOGGER));
    StructContext sc = f.getStructContext();
    StructClass cl = new StructClass(b, true, sc.getLoader());
    sc.getClasses().put(cn.name, cl);
    //instead of loading a file use custom bridge, created a few getters
    String fakePath = new File("none.class").getAbsolutePath();
    ContextUnit unit = new ContextUnit(ContextUnit.TYPE_FOLDER, null, fakePath, true, sc.getSaver(), sc.getDecompiledData());
    sc.getUnits().put(fakePath, unit);
    unit.addClass(cl, "none.class");
    sc.getLoader().addClassLink(cn.name, new LazyLoader.Link(LazyLoader.Link.CLASS, fakePath, null));

    f.decompileContext();
    return returned;
  } catch (Exception e) {
    e.printStackTrace();
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);
    return sw.toString();
  }
}
 
Example #8
Source File: StructContextDecorator.java    From Recaf with MIT License 4 votes vote down vote up
private LazyLoader getLoader() throws ReflectiveOperationException {
	// Hack to access private parent loader
	Field floader = StructContext.class.getDeclaredField("loader");
	floader.setAccessible(true);
	return (LazyLoader) floader.get(this);
}
 
Example #9
Source File: Fernflower.java    From JByteMod-Beta with GNU General Public License v2.0 4 votes vote down vote up
public Fernflower(IBytecodeProvider provider, IResultSaver saver, Map<String, Object> options, IFernflowerLogger logger) {
  structContext = new StructContext(saver, this, new LazyLoader(provider));
  DecompilerContext.initContext(options);
  DecompilerContext.setCounterContainer(new CounterContainer());
  DecompilerContext.setLogger(logger);
}
 
Example #10
Source File: StructClass.java    From JByteMod-Beta with GNU General Public License v2.0 4 votes vote down vote up
public StructClass(byte[] bytes, boolean own, LazyLoader loader) throws IOException {
  this(new DataInputFullStream(bytes), own, loader);
}
 
Example #11
Source File: StructClass.java    From JByteMod-Beta with GNU General Public License v2.0 4 votes vote down vote up
public StructClass(DataInputFullStream in, boolean own, LazyLoader loader) throws IOException {
  this.own = own;
  this.loader = loader;

  in.discard(4);

  minorVersion = in.readUnsignedShort();
  majorVersion = in.readUnsignedShort();

  pool = new ConstantPool(in);

  accessFlags = in.readUnsignedShort();
  int thisClassIdx = in.readUnsignedShort();
  int superClassIdx = in.readUnsignedShort();
  qualifiedName = pool.getPrimitiveConstant(thisClassIdx).getString();
  superClass = pool.getPrimitiveConstant(superClassIdx);

  // interfaces
  int length = in.readUnsignedShort();
  interfaces = new int[length];
  interfaceNames = new String[length];
  for (int i = 0; i < length; i++) {
    interfaces[i] = in.readUnsignedShort();
    interfaceNames[i] = pool.getPrimitiveConstant(interfaces[i]).getString();
  }

  // fields
  length = in.readUnsignedShort();
  fields = new VBStyleCollection<>();
  for (int i = 0; i < length; i++) {
    StructField field = new StructField(in, this);
    fields.addWithKey(field, InterpreterUtil.makeUniqueKey(field.getName(), field.getDescriptor()));
  }

  // methods
  length = in.readUnsignedShort();
  methods = new VBStyleCollection<>();
  for (int i = 0; i < length; i++) {
    StructMethod method = new StructMethod(in, this);
    methods.addWithKey(method, InterpreterUtil.makeUniqueKey(method.getName(), method.getDescriptor()));
  }

  // attributes
  attributes = readAttributes(in, pool);

  releaseResources();
}
 
Example #12
Source File: StructClass.java    From JByteMod-Beta with GNU General Public License v2.0 4 votes vote down vote up
public LazyLoader getLoader() {
  return loader;
}
 
Example #13
Source File: StructContext.java    From JByteMod-Beta with GNU General Public License v2.0 4 votes vote down vote up
public LazyLoader getLoader() {
  return loader;
}
 
Example #14
Source File: StructContextDecorator.java    From Recaf with MIT License 3 votes vote down vote up
/**
 * @param workspace
 * 		Recaf workspace to pull classes from.
 *
 * @throws IOException
 * 		Thrown if a class cannot be read.
 * @throws ReflectiveOperationException
 * 		Thrown if the parent loader could not be fetched.
 * @throws IndexOutOfBoundsException
 * 		Thrown if FernFlower can't read the class.
 * 		<i>(IE: It fails on newer Java class files)</i>
 */
public void addWorkspace(Workspace workspace) throws IOException,
		ReflectiveOperationException {
	LazyLoader loader = getLoader();
	// Add primary resource classes
	addResource(workspace.getPrimary(), loader);
	for (JavaResource resource : workspace.getLibraries())
		addResource(resource, loader);
}
 
Example #15
Source File: StructContextDecorator.java    From Recaf with MIT License 2 votes vote down vote up
/**
 * Constructs a StructContext.
 *
 * @param saver
 * 		Result saver <i>(Unused/noop)</i>
 * @param data
 * 		Data instance, should be an instance of
 * 		{@link me.coley.recaf.decompile.fernflower.FernFlowerAccessor}.
 * @param loader
 * 		LazyLoader to hold links to class resources.
 */
public StructContextDecorator(IResultSaver saver, IDecompiledData data, LazyLoader loader) {
	super(saver, data, loader);
}