com.strobel.assembler.metadata.Buffer Java Examples

The following examples show how to use com.strobel.assembler.metadata.Buffer. 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: UimaDecompiler.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * A special type loader that substitutes the byteArray for the given class name
 * @param classNameSlashes the name to look for to substitute
 * @param byteArray the value to substitute
 */
private void setDecompilerSettingsForByteArray(String classNameSlashes, byte[] byteArray) {
  ITypeLoader tl = new ITypeLoader() {
    
    @Override
    public boolean tryLoadType(String internalName, Buffer buffer) {
      if (classNameSlashes.equals(internalName)) {
        int length = byteArray.length;
        buffer.reset(length);
        System.arraycopy(byteArray, 0, buffer.array(), 0, length);
        return true;
      } else {
        return false;
      }
    }      
  }; 
  ITypeLoader tc = new CompositeTypeLoader(tl, getClasspathTypeLoader(), new InputTypeLoader());
  decompilerSettings.setTypeLoader(tc);
}
 
Example #2
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 #3
Source File: ComposedTypeLoader.java    From Recaf with MIT License 5 votes vote down vote up
@Override
public boolean tryLoadType(String s, Buffer buffer) {
    for (ITypeLoader loader : this.loaders) {
        if (loader.tryLoadType(s, buffer)) return true;
        buffer.reset();
    }
    return false;
}
 
Example #4
Source File: ProcyonDecompiler.java    From bytecode-viewer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean tryLoadType(final String internalName,
                           final Buffer buffer) {
    for (final ITypeLoader typeLoader : _typeLoaders) {
        if (typeLoader.tryLoadType(internalName, buffer)) {
            return true;
        }

        buffer.reset();
    }

    return false;
}
 
Example #5
Source File: LuytenTypeLoader.java    From Luyten with Apache License 2.0 5 votes vote down vote up
@Override
public boolean tryLoadType(final String internalName, final Buffer buffer) {
	for (final ITypeLoader typeLoader : _typeLoaders) {
		if (typeLoader.tryLoadType(internalName, buffer)) {
			return true;
		}

		buffer.reset();
	}

	return false;
}
 
Example #6
Source File: UimaDecompiler.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private ITypeLoader getClasspathTypeLoader() {
    return new ITypeLoader() {
      
      @Override
      public boolean tryLoadType(String internalName, Buffer buffer) {
        
        // read the class as a resource, and put into temporary byte array output stream
        // because we need to know the length
//        System.out.println("debug trying to load " + internalName);
        internalName = internalName.replace('.', '/') + ".class";
        InputStream stream = classLoader.getResourceAsStream(internalName);
        if (stream == null) {
//          System.out.println("debug failed to load " + internalName);
          return false;
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024 * 16);        
        byte[] b = new byte[1024 * 16]; 
        int numberRead;
        try {
          while (0 <= (numberRead = stream.read(b))){
            baos.write(b, 0, numberRead);
          }
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
        
        // Copy result (based on length) into output buffer spot
        int length = baos.size();
        b = baos.toByteArray();
        buffer.reset(length);
        System.arraycopy(b, 0, buffer.array(), 0, length);
//        System.out.println("debug OK loading " + internalName);
        return true;
      }      
    };    
  }
 
Example #7
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 #8
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 #9
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();
}