Java Code Examples for org.objectweb.asm.ClassReader#SKIP_DEBUG

The following examples show how to use org.objectweb.asm.ClassReader#SKIP_DEBUG . 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: ClassEntry.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/** Adds in all the super classes found for the given class entries into the given map */
private static void addSuperClasses(
        @NonNull LintClient client,
        @NonNull SuperclassVisitor visitor,
        @NonNull List<ClassEntry> entries) {
    for (ClassEntry entry : entries) {
        try {
            ClassReader reader = new ClassReader(entry.bytes);
            int flags = ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG
                    | ClassReader.SKIP_FRAMES;
            reader.accept(visitor, flags);
        } catch (Throwable t) {
            client.log(null, "Error processing %1$s: broken class file?", entry.path());
        }
    }
}
 
Example 2
Source File: DirectoryReader.java    From buck with Apache License 2.0 6 votes vote down vote up
@Override
public void visitClass(Path relativePath, ClassVisitor cv, boolean skipCode) throws IOException {
  if (!isClass(relativePath)) {
    throw new IllegalArgumentException();
  }

  int parsingOptions = ClassReader.SKIP_FRAMES;
  if (skipCode) {
    parsingOptions |= ClassReader.SKIP_DEBUG | ClassReader.SKIP_CODE;
  }

  try (InputStream inputStream = openInputStream(relativePath)) {
    ClassReader reader = new ClassReader(inputStream);
    reader.accept(cv, parsingOptions);
  }
}
 
Example 3
Source File: Printer.java    From Concurnas with MIT License 5 votes vote down vote up
/**
 * Prints a the given class to the given output.
 *
 * <p>Command line arguments: [-debug] &lt;binary class name or class file name &gt;
 *
 * @param args the command line arguments.
 * @param usage the help message to show when command line arguments are incorrect.
 * @param printer the printer to convert the class into text.
 * @param output where to print the result.
 * @param logger where to log errors.
 * @throws IOException if the class cannot be found, or if an IOException occurs.
 */
static void main(
    final String[] args,
    final String usage,
    final Printer printer,
    final PrintWriter output,
    final PrintWriter logger)
    throws IOException {
  if (args.length < 1 || args.length > 2 || (args[0].equals("-debug") && args.length != 2)) {
    logger.println(usage);
    return;
  }

  TraceClassVisitor traceClassVisitor = new TraceClassVisitor(null, printer, output);

  String className;
  int parsingOptions;
  if (args[0].equals("-debug")) {
    className = args[1];
    parsingOptions = ClassReader.SKIP_DEBUG;
  } else {
    className = args[0];
    parsingOptions = 0;
  }

  if (className.endsWith(".class")
      || className.indexOf('\\') != -1
      || className.indexOf('/') != -1) {
    InputStream inputStream =
        new FileInputStream(className); // NOPMD(AvoidFileStream): can't fix for 1.5 compatibility
    new ClassReader(inputStream).accept(traceClassVisitor, parsingOptions);
  } else {
    new ClassReader(className).accept(traceClassVisitor, parsingOptions);
  }
}
 
Example 4
Source File: Mappings.java    From Recaf with MIT License 5 votes vote down vote up
private void accept(Map<String, byte[]> updated, ClassReader cr, int readFlags, int writeFlags) {
	String name = cr.getClassName();
	// Apply with mapper
	SimpleRecordingRemapper mapper = new SimpleRecordingRemapper(getMappings(),
			checkFieldHierarchy, checkMethodHierarchy, workspace);
	WorkspaceClassWriter cw = workspace.createWriter(writeFlags);
	cw.setMappings(getMappings(), reverseClassMappings);
	ClassRemapper adapter = new ClassRemapper(cw, mapper);
	if (clearDebugInfo)
		readFlags |= ClassReader.SKIP_DEBUG;
	cr.accept(adapter, readFlags);
	// Only return the modified class if any references to the mappings were found.
	if (mapper.isDirty())
		updated.put(name, cw.toByteArray());
}
 
Example 5
Source File: Printer.java    From JReFrameworker with MIT License 5 votes vote down vote up
/**
 * Prints a the given class to the standard output.
 *
 * <p>Command line arguments: [-debug] &lt;binary class name or class file name &gt;
 *
 * @param usage the help message to show when command line arguments are incorrect.
 * @param printer the printer to convert the class into text.
 * @param args the command line arguments.
 * @throws IOException if the class cannot be found, or if an IOException occurs.
 */
static void main(final String usage, final Printer printer, final String[] args)
    throws IOException {
  if (args.length < 1 || args.length > 2 || (args[0].equals("-debug") && args.length != 2)) {
    System.err.println(usage);
    return;
  }

  TraceClassVisitor traceClassVisitor =
      new TraceClassVisitor(null, printer, new PrintWriter(System.out));

  String className;
  int parsingOptions;
  if (args[0].equals("-debug")) {
    className = args[1];
    parsingOptions = ClassReader.SKIP_DEBUG;
  } else {
    className = args[0];
    parsingOptions = 0;
  }

  if (className.endsWith(".class")
      || className.indexOf('\\') != -1
      || className.indexOf('/') != -1) {
    InputStream inputStream =
        new FileInputStream(className); // NOPMD(AvoidFileStream): can't fix for 1.5 compatibility
    new ClassReader(inputStream).accept(traceClassVisitor, parsingOptions);
  } else {
    new ClassReader(className).accept(traceClassVisitor, parsingOptions);
  }
}
 
Example 6
Source File: Printer.java    From JReFrameworker with MIT License 5 votes vote down vote up
/**
 * Prints a the given class to the standard output.
 *
 * <p>Command line arguments: [-debug] &lt;binary class name or class file name &gt;
 *
 * @param usage the help message to show when command line arguments are incorrect.
 * @param printer the printer to convert the class into text.
 * @param args the command line arguments.
 * @throws IOException if the class cannot be found, or if an IOException occurs.
 */
static void main(final String usage, final Printer printer, final String[] args)
    throws IOException {
  if (args.length < 1 || args.length > 2 || (args[0].equals("-debug") && args.length != 2)) {
    System.err.println(usage);
    return;
  }

  TraceClassVisitor traceClassVisitor =
      new TraceClassVisitor(null, printer, new PrintWriter(System.out));

  String className;
  int parsingOptions;
  if (args[0].equals("-debug")) {
    className = args[1];
    parsingOptions = ClassReader.SKIP_DEBUG;
  } else {
    className = args[0];
    parsingOptions = 0;
  }

  if (className.endsWith(".class")
      || className.indexOf('\\') != -1
      || className.indexOf('/') != -1) {
    InputStream inputStream =
        new FileInputStream(className); // NOPMD(AvoidFileStream): can't fix for 1.5 compatibility
    new ClassReader(inputStream).accept(traceClassVisitor, parsingOptions);
  } else {
    new ClassReader(className).accept(traceClassVisitor, parsingOptions);
  }
}
 
Example 7
Source File: DAppCreator.java    From AVM with MIT License 4 votes vote down vote up
private static Map<String, byte[]> rejectionAndRenameInputClasses(Map<String, byte[]> inputClasses, ClassHierarchy classHierarchy, ClassRenamer classRenamer, boolean preserveDebuggability) {
    // By this point, we at least know that the classHierarchy is internally consistent.
    // This also means we can safely count instance variables to make sure we haven't reached our limit.
    InstanceVariableCountManager manager = new InstanceVariableCountManager();
    Map<String, byte[]> safeClasses = new HashMap<>();

    Set<String> preRenameUserClassAndInterfaceSet = classHierarchy.getPreRenameUserDefinedClassesAndInterfaces();
    Set<String> preRenameUserDefinedClasses = classHierarchy.getPreRenameUserDefinedClassesOnly(classRenamer);

    PreRenameClassAccessRules preRenameClassAccessRules = new PreRenameClassAccessRules(preRenameUserDefinedClasses, preRenameUserClassAndInterfaceSet);
    NamespaceMapper namespaceMapper = new NamespaceMapper(preRenameClassAccessRules);
    
    for (String name : inputClasses.keySet()) {
        // Note that transformClasses requires that the input class names by the .-style names.
        RuntimeAssertionError.assertTrue(-1 == name.indexOf("/"));

        int parsingOptions = preserveDebuggability ? 0: ClassReader.SKIP_DEBUG;
        try {
            byte[] classBytecode = inputClasses.get(name);
            // Read the class to check our static geometry limits before running this through our high-level ASM rejection pipeline.
            // (note that this processing is done for HistogramDataCollector, back in AvmImpl, but this duplication isn't a large concern since that is disabled, by default).
            ClassFileInfoBuilder.ClassFileInfo classFileInfo = ClassFileInfoBuilder.getDirectClassFileInfo(classBytecode);
            
            // Impose class-level restrictions.
            if (classFileInfo.definedMethods.size() > ConsensusLimitConstants.MAX_METHOD_COUNT) {
                throw RejectedClassException.maximumMethodCountExceeded(name);
            }
            if (classFileInfo.constantPoolEntryCount > ConsensusLimitConstants.MAX_CONSTANT_POOL_ENTRIES) {
                throw RejectedClassException.maximumConstantPoolEntriesExceeded(name);
            }
            
            // Impose method-level restrictions.
            for (ClassFileInfoBuilder.MethodCode methodCode : classFileInfo.definedMethods) {
                if (methodCode.codeLength > ConsensusLimitConstants.MAX_METHOD_BYTE_LENGTH) {
                    throw RejectedClassException.maximumMethodSizeExceeded(name);
                }
                if (methodCode.exceptionTableSize > ConsensusLimitConstants.MAX_EXCEPTION_TABLE_ENTRIES) {
                    throw RejectedClassException.maximumExceptionTableEntriesExceeded(name);
                }
                if (methodCode.maxStack > ConsensusLimitConstants.MAX_OPERAND_STACK_DEPTH) {
                    throw RejectedClassException.maximumOperandStackDepthExceeded(name);
                }
                if (methodCode.maxLocals > ConsensusLimitConstants.MAX_LOCAL_VARIABLES) {
                    throw RejectedClassException.maximumLocalVariableCountExceeded(name);
                }
            }
            
            // Now, proceed with the ASM pipeline for high-level rejection and renaming.
            InstanceVariableCountingVisitor variableCounter = new InstanceVariableCountingVisitor(manager);
            byte[] bytecode = new ClassToolchain.Builder(classBytecode, parsingOptions)
                .addNextVisitor(new RejectionClassVisitor(preRenameClassAccessRules, namespaceMapper, preserveDebuggability))
                .addNextVisitor(new LoopingExceptionStrippingVisitor())
                .addNextVisitor(variableCounter)
                .addNextVisitor(new UserClassMappingVisitor(namespaceMapper, preserveDebuggability))
                .addWriter(new TypeAwareClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS, classHierarchy, classRenamer))
                .build()
                .runAndGetBytecode();
            String mappedName = DebugNameResolver.getUserPackageDotPrefix(name, preserveDebuggability);
            safeClasses.put(mappedName, bytecode);
        } catch (Exception e) {
            throw new RejectedClassException(e.getMessage());
        }
    }
    // Before we return, make sure we didn't exceed the instance variable limits (will throw RejectedClassException on failure).
    manager.verifyAllCounts();
    return safeClasses;
}