Class BytecodeExtractor

java.lang.Object
io.github.mkoncek.classpathless.util.BytecodeExtractor

public class BytecodeExtractor extends Object
A utility class to extract useful information from class files, for example type names, methods, fields.
  • Field Details

    • CURRENT_ASM_OPCODE

      private static final int CURRENT_ASM_OPCODE
      See Also:
    • FORMAL_CONTENTS_PATTERN

      private static final Pattern FORMAL_CONTENTS_PATTERN
    • classes

      private SortedSet<String> classes
  • Constructor Details

    • BytecodeExtractor

      public BytecodeExtractor()
  • Method Details

    • dot

      private static String dot(String value)
    • extractDescriptor

      private static void extractDescriptor(String descriptor, Collection<String> result)
      Function for extracting the type names from descriptors.
    • extractSignature

      private static void extractSignature(String signature, Collection<String> result)
      Function for extracting the contents of formal parameters, i. e. those contained in invalid input: '<'> parentheses. This function does not do full signature parsing, just a simple search. The types not caught by the regular expression should be already caught by other visitors. For reference about signatures, see: https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-4.html#jvms-4.3
    • extractTypenamesFrom

      private SortedSet<String> extractTypenamesFrom(byte[] classFile)
    • extractDirectNestedClassesFrom

      private SortedSet<String> extractDirectNestedClassesFrom(byte[] classFile)
    • extractNestedClassesFrom

      private SortedSet<String> extractNestedClassesFrom(byte[] classFile, ClassesProvider classesProvider)
    • extractTypenames

      public static SortedSet<String> extractTypenames(byte[] classFile)
      Extracts all type names present in the .class file.
      Parameters:
      classFile - The file to extract names from.
      Returns:
      The set of fully qualified type names present in the class file.
    • extractFields

      public static Collection<String> extractFields(byte[] classFile)
      Extracts all the field names of the provided class excluding inherited fields.
      Parameters:
      classFile - The file to extract names from.
      Returns:
      The collection of field names.
    • extractMethods

      public static Collection<String> extractMethods(byte[] classFile)
      Extracts all method names of given class. This will not include methods of inner classes nor inherited methods (unless they are overriden).
      Parameters:
      classFile - The file to extract names from.
      Returns:
      The collection of method names.
    • extractInterfaces

      public static Collection<String> extractInterfaces(byte[] classFile)
      Extracts the names of all directly implemented interfaces, i. e. not transitively.
      Parameters:
      classFile - The file to extract names from.
      Returns:
      The collection of implemented interfaces.
    • extractSuperClass

      public static Optional<String> extractSuperClass(byte[] classFile)
      Extracts the name of the super class of the provided class. Classes which to not inherit this will be equal to "java.lang.Object".
      Parameters:
      classFile - The file to extract the name from.
      Returns:
      The name of the super class.
    • extractOuterClass

      public static Optional<String> extractOuterClass(byte[] classFile)
      Extracts the name of the outer class of the provided class.
      Parameters:
      classFile - The file to extract the name from.
      Returns:
      The name of the super class.
    • extractDirectNestedClasses

      public static SortedSet<String> extractDirectNestedClasses(byte[] classFile)
      Extracts all directly nested class names from the initial outer class.
      Parameters:
      classFile - The file to extract names from.
      Returns:
      The set of all directly nested fully qualified class names excluding the initial outer class.
    • extractNestedClasses

      public static SortedSet<String> extractNestedClasses(byte[] classFile, ClassesProvider classesProvider)
      Recursively extracts all the nested class names from the initial outer class possibly by pulling more class files from the class provider.
      Parameters:
      classFile - The file to extract names from.
      classesProvider - The provider of nested classes' bytecode.
      Returns:
      The set of all nested fully qualified class names excluding the initial outer class.
    • extractFullClassGroup

      public static SortedSet<String> extractFullClassGroup(byte[] classFile, ClassesProvider classesProvider)
      Walk up to outermost class and return all its transitively nested classes.
      Parameters:
      classFile - The file to extract names from.
      classesProvider - The provider of nested classes' bytecode.
      Returns:
      The set of all fully qualified class names of the nest to which this class belongs.
    • extractDependencies

      public static Collection<String> extractDependencies(IdentifiedBytecode initialClass, ClassesProvider classesProvider)
      This method returns all the class names that are required for the compilation of a source file corresponding to the bytecode of initialClass.
      Parameters:
      initialClass - The bytecode the dependencies of which are requested.
      classesProvider - ClassesProvider of class dependencies.
      Returns:
      A collection of all class names that are required for compilation.
    • extractDependenciesImpl

      static Collection<String> extractDependenciesImpl(IdentifiedBytecode initialClass, ClassesProvider classesProvider, Consumer<String> first, Consumer<String> second, Consumer<String> third)
      This is an implementation method.
      Parameters:
      initialClass - The bytecode the dependencies of which are requested.
      classesProvider - ClassesProvider of class dependencies.
      first - The consumer of a class name in case a class is added in the first phase.
      second - The consumer of a class name in case a class is added in the second phase.
      third - The consumer of a class name in case a class is added in the third phase.
      Returns:
      A collection of all class names that are required for compilation.