Java Code Examples for com.strobel.assembler.metadata.Buffer#position()

The following examples show how to use com.strobel.assembler.metadata.Buffer#position() . 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: RecafTypeLoader.java    From Recaf with MIT License 5 votes vote down vote up
@Override
public boolean tryLoadType(String name, Buffer buffer) {
    byte[] code = controller.getWorkspace().getRawClass(name);
    if (controller.config().decompile().stripDebug)
        code = ClassUtil.stripDebugForDecompile(code);
    if (code == null) return false;
    buffer.position(0);
    buffer.putByteArray(code, 0, code.length);
    buffer.position(0);
    return true;
}
 
Example 2
Source File: ProcyonDecompiler.java    From JByteMod-Beta with GNU General Public License v2.0 4 votes vote down vote up
public String decompile(byte[] b, MethodNode mn) {
  try {
  //TODO decompile method only
    DecompilerSettings settings = new DecompilerSettings();
    try {
      for (Field f : settings.getClass().getDeclaredFields()) {
        if (f.getType() == boolean.class) {
          f.setAccessible(true);
          f.setBoolean(settings, JByteMod.ops.get("procyon" + f.getName()).getBoolean());
        }
      }
    } catch (Throwable t) {
      t.printStackTrace();
    }
    settings.setShowSyntheticMembers(true);
    MetadataSystem metadataSystem = new MetadataSystem(new ITypeLoader() {
      private InputTypeLoader backLoader = new InputTypeLoader();

      @Override
      public boolean tryLoadType(String s, Buffer buffer) {
        if (s.equals(cn.name)) {
          buffer.putByteArray(b, 0, b.length);
          buffer.position(0);
          return true;
        } else {
          return backLoader.tryLoadType(s, buffer);
        }
      }
    });
    TypeReference type = metadataSystem.lookupType(cn.name);
    DecompilationOptions decompilationOptions = new DecompilationOptions();
    decompilationOptions.setSettings(DecompilerSettings.javaDefaults());
    decompilationOptions.setFullDecompilation(true);
    TypeDefinition resolvedType = null;
    if (type == null || ((resolvedType = type.resolve()) == null)) {
      new ErrorDisplay("Unable to resolve type.");
      return "error";
    }
    StringWriter stringwriter = new StringWriter();
    settings.getLanguage().decompileType(resolvedType, new PlainTextOutput(stringwriter), decompilationOptions);
    String decompiledSource = stringwriter.toString();
    return decompiledSource;
  } catch (Exception e) {
    e.printStackTrace();
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);
    return sw.toString();
  }
}
 
Example 3
Source File: WindupJarTypeLoader.java    From windup with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean tryLoadType(final String internalName, final Buffer buffer)
{
    try
    {
        if (LOG.isLoggable(Level.FINE))
        {
            LOG.fine("Attempting to load type: " + internalName + "...");
        }

        final JarEntry entry = _jarFile.getJarEntry(internalName + ".class");

        if (entry == null)
        {
            final String mappedName = _knownMappings.get(internalName);

            return mappedName != null &&
                        !mappedName.equals(internalName) && tryLoadType(mappedName, buffer);
        }

        final InputStream inputStream = _jarFile.getInputStream(entry);

        int remainingBytes = inputStream.available();

        buffer.reset(remainingBytes);

        while (remainingBytes > 0)
        {
            final int bytesRead = inputStream.read(buffer.array(), buffer.position(), remainingBytes);

            if (bytesRead < 0)
            {
                break;
            }

            buffer.position(buffer.position() + bytesRead);
            remainingBytes -= bytesRead;
        }

        buffer.position(0);

        final String actualName = getInternalNameFromClassFile(buffer);

        if (actualName != null && !actualName.equals(internalName))
        {
            _knownMappings.put(actualName, internalName);
        }

        if (LOG.isLoggable(Level.FINE))
        {
            LOG.fine("Type loaded from " + _jarFile.getName() + "!" + entry.getName() + ".");
        }

        return true;
    }
    catch (IOException e)
    {
        throw ExceptionUtilities.asRuntimeException(e);
    }
}
 
Example 4
Source File: WindupJarTypeLoader.java    From windup with Eclipse Public License 1.0 4 votes vote down vote up
private static String getInternalNameFromClassFile(final Buffer b)
{
    final long magic = b.readInt() & 0xFFFFFFFFL;

    if (magic != 0xCAFEBABEL)
    {
        return null;
    }

    b.readUnsignedShort(); // minor version
    b.readUnsignedShort(); // major version

    final ConstantPool constantPool = ConstantPool.read(b);

    b.readUnsignedShort(); // access flags

    final ConstantPool.TypeInfoEntry thisClass = constantPool.getEntry(b.readUnsignedShort());

    b.position(0);

    return thisClass.getName();
}