Class BytecodeExtractor
- java.lang.Object
-
- io.github.mkoncek.classpathless.util.BytecodeExtractor
-
public class BytecodeExtractor extends java.lang.Object
A utility class to extract useful information from class files, for example type names, methods, fields.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
BytecodeExtractor.ExtrAnnotationVisitor
private static class
BytecodeExtractor.ExtrClassVisitor
private static class
BytecodeExtractor.ExtrFieldVisitor
private static class
BytecodeExtractor.ExtrMethodVisitor
-
Field Summary
Fields Modifier and Type Field Description private java.util.SortedSet<java.lang.String>
classes
private static int
CURRENT_ASM_OPCODE
private static java.util.regex.Pattern
FORMAL_CONTENTS_PATTERN
-
Constructor Summary
Constructors Constructor Description BytecodeExtractor()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static java.lang.String
dot(java.lang.String value)
static java.util.Collection<java.lang.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.(package private) static java.util.Collection<java.lang.String>
extractDependenciesImpl(IdentifiedBytecode initialClass, ClassesProvider classesProvider, java.util.function.Consumer<java.lang.String> first, java.util.function.Consumer<java.lang.String> second, java.util.function.Consumer<java.lang.String> third)
This is an implementation method.private static void
extractDescriptor(java.lang.String descriptor, java.util.Collection<java.lang.String> result)
Function for extracting the type names from descriptors.static java.util.SortedSet<java.lang.String>
extractDirectNestedClasses(byte[] classFile)
Extracts all directly nested class names from the initial outer class.private java.util.SortedSet<java.lang.String>
extractDirectNestedClassesFrom(byte[] classFile)
static java.util.Collection<java.lang.String>
extractFields(byte[] classFile)
Extracts all the field names of the provided class excluding inherited fields.static java.util.SortedSet<java.lang.String>
extractFullClassGroup(byte[] classFile, ClassesProvider classesProvider)
Walk up to outermost class and return all its transitively nested classes.static java.util.Collection<java.lang.String>
extractInterfaces(byte[] classFile)
Extracts the names of all directly implemented interfaces, i.static java.util.Collection<java.lang.String>
extractMethods(byte[] classFile)
Extracts all method names of given class.static java.util.SortedSet<java.lang.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.private java.util.SortedSet<java.lang.String>
extractNestedClassesFrom(byte[] classFile, ClassesProvider classesProvider)
static java.util.Optional<java.lang.String>
extractOuterClass(byte[] classFile)
Extracts the name of the outer class of the provided class.private static void
extractSignature(java.lang.String signature, java.util.Collection<java.lang.String> result)
Function for extracting the contents of formal parameters, i.static java.util.Optional<java.lang.String>
extractSuperClass(byte[] classFile)
Extracts the name of the super class of the provided class.static java.util.SortedSet<java.lang.String>
extractTypenames(byte[] classFile)
Extracts all type names present in the .class file.private java.util.SortedSet<java.lang.String>
extractTypenamesFrom(byte[] classFile)
-
-
-
Field Detail
-
CURRENT_ASM_OPCODE
private static final int CURRENT_ASM_OPCODE
- See Also:
- Constant Field Values
-
FORMAL_CONTENTS_PATTERN
private static final java.util.regex.Pattern FORMAL_CONTENTS_PATTERN
-
classes
private java.util.SortedSet<java.lang.String> classes
-
-
Method Detail
-
dot
private static java.lang.String dot(java.lang.String value)
-
extractDescriptor
private static void extractDescriptor(java.lang.String descriptor, java.util.Collection<java.lang.String> result)
Function for extracting the type names from descriptors.
-
extractSignature
private static void extractSignature(java.lang.String signature, java.util.Collection<java.lang.String> result)
Function for extracting the contents of formal parameters, i. e. those contained in <> 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 java.util.SortedSet<java.lang.String> extractTypenamesFrom(byte[] classFile)
-
extractDirectNestedClassesFrom
private java.util.SortedSet<java.lang.String> extractDirectNestedClassesFrom(byte[] classFile)
-
extractNestedClassesFrom
private java.util.SortedSet<java.lang.String> extractNestedClassesFrom(byte[] classFile, ClassesProvider classesProvider)
-
extractTypenames
public static java.util.SortedSet<java.lang.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 java.util.Collection<java.lang.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 java.util.Collection<java.lang.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 java.util.Collection<java.lang.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 java.util.Optional<java.lang.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 java.util.Optional<java.lang.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 java.util.SortedSet<java.lang.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 java.util.SortedSet<java.lang.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 java.util.SortedSet<java.lang.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 java.util.Collection<java.lang.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 java.util.Collection<java.lang.String> extractDependenciesImpl(IdentifiedBytecode initialClass, ClassesProvider classesProvider, java.util.function.Consumer<java.lang.String> first, java.util.function.Consumer<java.lang.String> second, java.util.function.Consumer<java.lang.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.
-
-