Documentation
¶
Overview ¶
Package java provides an Enricher to add reachability annotations for Java Packages.
Index ¶
- Constants
- Variables
- func GetMainClasses(manifest io.Reader) ([]string, error)
- func IsStdLib(class string) bool
- func NewDefault() enricher.Enricher
- type ClassFile
- type ConstantClass
- type ConstantDouble
- type ConstantDynamic
- type ConstantFieldref
- type ConstantFloat
- type ConstantInteger
- type ConstantInterfaceMethodref
- type ConstantInvokeDynamic
- type ConstantKind
- type ConstantLong
- type ConstantMethodHandle
- type ConstantMethodType
- type ConstantMethodref
- type ConstantModule
- type ConstantNameAndType
- type ConstantPackage
- type ConstantPlaceholder
- type ConstantPoolInfo
- type ConstantString
- type ConstantUtf8
- type DefaultPackageFinder
- type DynamicCodeStrategy
- type Enricher
- type MavenPackageFinder
- type ReachabilityEnumerator
- type ReachabilityResult
- type UniqueQueue
Constants ¶
const ( // Name is the unique name of this detector. Name = "reachability/java" // MetaDirPath is the path to the META-INF directory. MetaDirPath = "META-INF" )
const ( // DontHandleDynamicCode doesn't do any kind of special handling. DontHandleDynamicCode DynamicCodeStrategy = 0 // AssumeAllDirectDepsReachable assumes that the entirety of all direct dependencies (i.e. all their // classes) are fully reachable. AssumeAllDirectDepsReachable = 1 << 0 // AssumeAllClassesReachable assumes that every single class belonging to the current dependency are // fully reachable. AssumeAllClassesReachable = 1 << 1 )
const ( // BootInfClasses contains spring boot specific classes. BootInfClasses = "BOOT-INF/classes" // MetaInfVersions is a directory that contains multi-release JARs. MetaInfVersions = "META-INF/versions" )
Variables ¶
var ( // ErrClassNotFound is returned when a class is not found. ErrClassNotFound = errors.New("class not found") // ErrArtifactNotFound is returned when an artifact is not found. ErrArtifactNotFound = errors.New("artifact not found") )
var ( // ManifestFilePath is the path to the MANIFEST.MF file. ManifestFilePath = path.Join(MetaDirPath, "MANIFEST.MF") // MavenDepDirPath is the path to the Maven dependency directory. MavenDepDirPath = path.Join(MetaDirPath, "maven") // ServiceDirPath is the path to the META-INF/services directory. ServiceDirPath = path.Join(MetaDirPath, "services") // ErrMavenDependencyNotFound is returned when a JAR is not a Maven dependency. ErrMavenDependencyNotFound = errors.New(MavenDepDirPath + " directory not found") )
var ( // BinaryBaseTypes comes from https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.3 BinaryBaseTypes = []string{ "B", "C", "D", "F", "I", "J", "L", "S", "Z", } // StandardLibraryPrefixes defines the prefixes of standard library classes. StandardLibraryPrefixes = []string{ "java/", "javax/", "jdk/", "sun/", "org/ietf/", "org/omg/", "org/w3c/", "org/xml/", } )
var MavenBaseURL = "https://repo1.maven.org/maven2"
MavenBaseURL is the base URL for the repository.
Functions ¶
func GetMainClasses ¶
GetMainClasses extracts the main class name from the MANIFEST.MF file in a .jar.
func NewDefault ¶
NewDefault returns a new javareach enricher with the default configuration.
Types ¶
type ClassFile ¶
type ClassFile struct { Magic uint32 MinorVersion uint16 MajorVersion uint16 ConstantPoolCount uint16 ConstantPool []ConstantPoolInfo AccessFlags uint16 ThisClass uint16 }
ClassFile struct represents the overall structure of a Java class file. This only contains the fields we care about for reachability analysis.
func ParseClass ¶
ParseClass parses a Java class file from a reader.
func (*ClassFile) ConstantPoolClass ¶
ConstantPoolClass returns the class name at the given index.
type ConstantClass ¶
type ConstantClass struct {
NameIndex uint16
}
ConstantClass represents a class constant pool entry.
func (ConstantClass) Type ¶
func (c ConstantClass) Type() ConstantKind
Type returns the ConstantKind for ConstantClass.
type ConstantDouble ¶
type ConstantDouble struct {
Bytes float64
}
ConstantDouble represents a double constant pool entry.
func (ConstantDouble) Type ¶
func (c ConstantDouble) Type() ConstantKind
Type returns the ConstantKind for ConstantDouble.
type ConstantDynamic ¶
ConstantDynamic represents a dynamic constant pool entry.
func (ConstantDynamic) Type ¶
func (c ConstantDynamic) Type() ConstantKind
Type returns the ConstantKind for ConstantDynamic.
type ConstantFieldref ¶
ConstantFieldref represents a field reference constant pool entry.
func (ConstantFieldref) Type ¶
func (c ConstantFieldref) Type() ConstantKind
Type returns the ConstantKind for ConstantFieldref.
type ConstantFloat ¶
type ConstantFloat struct {
Bytes float32
}
ConstantFloat represents a float constant pool entry.
func (ConstantFloat) Type ¶
func (c ConstantFloat) Type() ConstantKind
Type returns the ConstantKind for ConstantFloat.
type ConstantInteger ¶
type ConstantInteger struct {
Bytes int32
}
ConstantInteger represents an integer constant pool entry.
func (ConstantInteger) Type ¶
func (c ConstantInteger) Type() ConstantKind
Type returns the ConstantKind for ConstantInteger.
type ConstantInterfaceMethodref ¶
ConstantInterfaceMethodref represents an interface method reference constant pool entry.
func (ConstantInterfaceMethodref) Type ¶
func (c ConstantInterfaceMethodref) Type() ConstantKind
Type returns the ConstantKind for ConstantInterfaceMethodref.
type ConstantInvokeDynamic ¶
ConstantInvokeDynamic represents an invoke dynamic constant pool entry.
func (ConstantInvokeDynamic) Type ¶
func (c ConstantInvokeDynamic) Type() ConstantKind
Type returns the ConstantKind for ConstantInvokeDynamic.
type ConstantKind ¶
type ConstantKind uint8
ConstantKind is the type of a constant pool entry.
const ( ConstantKindUtf8 ConstantKind = 1 ConstantKindInteger ConstantKind = 3 ConstantKindFloat ConstantKind = 4 ConstantKindLong ConstantKind = 5 ConstantKindDouble ConstantKind = 6 ConstantKindClass ConstantKind = 7 ConstantKindString ConstantKind = 8 ConstantKindFieldref ConstantKind = 9 ConstantKindMethodref ConstantKind = 10 ConstantKindInterfaceMethodref ConstantKind = 11 ConstantKindNameAndType ConstantKind = 12 ConstantKindMethodHandle ConstantKind = 15 ConstantKindMethodType ConstantKind = 16 ConstantKindDynamic ConstantKind = 17 ConstantKindInvokeDynamic ConstantKind = 18 ConstantKindModule ConstantKind = 19 ConstantKindPackage ConstantKind = 20 // ConstantKindPlaceholder is not a real Java class constant kind. // We use this to implement long and double constants taking up two entries // in the constant pool, as well as the constant pool being 1-indexed. // // From https://docs.oracle.com/javase/specs/jvms/se22/html/jvms-4.html#jvms-4.4.5 // All 8-byte constants take up two entries in the constant_pool table of // the class file. If a CONSTANT_Long_info or CONSTANT_Double_info structure // is the entry at index n in the constant_pool table, then the next usable // entry in the table is located at index n+2. The constant_pool index n+1 // must be valid but is considered unusable. ConstantKindPlaceholder ConstantKind = 255 )
ConstantKind values are defined in JAR constant pool entries.
type ConstantLong ¶
type ConstantLong struct {
Bytes int64
}
ConstantLong represents a long constant pool entry.
func (ConstantLong) Type ¶
func (c ConstantLong) Type() ConstantKind
Type returns the ConstantKind for ConstantLong.
type ConstantMethodHandle ¶
ConstantMethodHandle represents a method handle constant pool entry.
func (ConstantMethodHandle) Type ¶
func (c ConstantMethodHandle) Type() ConstantKind
Type returns the ConstantKind for ConstantMethodHandle.
type ConstantMethodType ¶
type ConstantMethodType struct {
DescriptorIndex uint16
}
ConstantMethodType represents a method type constant pool entry.
func (ConstantMethodType) Type ¶
func (c ConstantMethodType) Type() ConstantKind
Type returns the ConstantKind for ConstantMethodType.
type ConstantMethodref ¶
ConstantMethodref represents a method reference constant pool entry.
func (ConstantMethodref) Type ¶
func (c ConstantMethodref) Type() ConstantKind
Type returns the ConstantKind for ConstantMethodref.
type ConstantModule ¶
type ConstantModule struct {
NameIndex uint16
}
ConstantModule represents a module constant pool entry.
func (ConstantModule) Type ¶
func (c ConstantModule) Type() ConstantKind
Type returns the ConstantKind for ConstantModule.
type ConstantNameAndType ¶
ConstantNameAndType represents a name and type constant pool entry.
func (ConstantNameAndType) Type ¶
func (c ConstantNameAndType) Type() ConstantKind
Type returns the ConstantKind for ConstantNameAndType.
type ConstantPackage ¶
type ConstantPackage struct {
NameIndex uint16
}
ConstantPackage represents a package constant pool entry.
func (ConstantPackage) Type ¶
func (c ConstantPackage) Type() ConstantKind
Type returns the ConstantKind for ConstantPackage.
type ConstantPlaceholder ¶
type ConstantPlaceholder struct{}
ConstantPlaceholder is a placeholder constant pool entry.
func (ConstantPlaceholder) Type ¶
func (c ConstantPlaceholder) Type() ConstantKind
Type returns the ConstantKind for ConstantPlaceholder.
type ConstantPoolInfo ¶
type ConstantPoolInfo interface {
Type() ConstantKind
}
ConstantPoolInfo interface represents the base type for all constant pool entries.
type ConstantString ¶
type ConstantString struct {
StringIndex uint16
}
ConstantString represents a string constant pool entry.
func (ConstantString) Type ¶
func (c ConstantString) Type() ConstantKind
Type returns the ConstantKind for ConstantString.
type ConstantUtf8 ¶
ConstantUtf8 represents a UTF-8 string constant pool entry.
func (ConstantUtf8) Type ¶
func (c ConstantUtf8) Type() ConstantKind
Type returns the ConstantKind for ConstantUtf8.
type DefaultPackageFinder ¶
type DefaultPackageFinder struct {
// contains filtered or unexported fields
}
DefaultPackageFinder implements a MavenPackageFinder that downloads all .jar dependencies on demand and computes a local class to jar mapping.
func NewDefaultPackageFinder ¶
func NewDefaultPackageFinder(ctx context.Context, inv []*extractor.Package, jarRoot *os.Root, client *http.Client) (*DefaultPackageFinder, error)
NewDefaultPackageFinder creates a new DefaultPackageFinder based on a set of inventory.
type DynamicCodeStrategy ¶
type DynamicCodeStrategy int
DynamicCodeStrategy is a strategy for handling dynamic code loading.
type Enricher ¶
type Enricher struct {
// contains filtered or unexported fields
}
Enricher is the Java Reach enricher.
func NewEnricher ¶
NewEnricher creates a new Enricher. It accepts an http.Client as a dependency. If the provided client is nil, it defaults to the standard http.DefaultClient.
func (Enricher) Enrich ¶
func (enr Enricher) Enrich(ctx context.Context, input *enricher.ScanInput, inv *inventory.Inventory) error
Enrich enriches the inventory with Java Reach data.
func (Enricher) RequiredPlugins ¶
RequiredPlugins returns the names of the plugins required by the enricher.
func (Enricher) Requirements ¶
func (Enricher) Requirements() *plugin.Capabilities
Requirements returns the requirements of the enricher.
type MavenPackageFinder ¶
type MavenPackageFinder interface { // Find returns a list of package names that contain a class path. Find(classPath string) ([]string, error) // Find returns a list of class names that are part of a package. Classes(artifact string) ([]string, error) }
MavenPackageFinder is an interface for finding Maven packages that contain a class path.
type ReachabilityEnumerator ¶
type ReachabilityEnumerator struct { ClassPaths []string PackageFinder MavenPackageFinder CodeLoadingStrategy DynamicCodeStrategy DependencyInjectionStrategy DynamicCodeStrategy // contains filtered or unexported fields }
ReachabilityEnumerator enumerates the reachable classes from a set of root classes.
func NewReachabilityEnumerator ¶
func NewReachabilityEnumerator( classPaths []string, packageFinder MavenPackageFinder, codeLoadingStrategy DynamicCodeStrategy, dependencyInjectionStrategy DynamicCodeStrategy) *ReachabilityEnumerator
NewReachabilityEnumerator creates a new ReachabilityEnumerator.
func (*ReachabilityEnumerator) EnumerateReachability ¶
func (r *ReachabilityEnumerator) EnumerateReachability(jarRoot *os.Root, roots []*ClassFile) (*ReachabilityResult, error)
EnumerateReachability enumerates the reachable classes from a set of root classes.
func (*ReachabilityEnumerator) EnumerateReachabilityFromClasses ¶
func (r *ReachabilityEnumerator) EnumerateReachabilityFromClasses(jarRoot *os.Root, mainClasses []string, optionalRootClasses []string) (*ReachabilityResult, error)
EnumerateReachabilityFromClasses enumerates the reachable classes from a set of root classes.
type ReachabilityResult ¶
type ReachabilityResult struct { Classes []string UsesDynamicCodeLoading []string UsesDependencyInjection []string }
ReachabilityResult contains the result of a reachability enumeration.
type UniqueQueue ¶
type UniqueQueue[K, V comparable] struct { // contains filtered or unexported fields }
UniqueQueue is a queue data structure that will add keys at most once in its lifetime. Duplicate keys are ignored when pushing them.
func NewQueue ¶
func NewQueue[K, V comparable](seen map[K]struct{}) *UniqueQueue[K, V]
NewQueue creates a new UniqueQueue.
func (*UniqueQueue[K, V]) Empty ¶
func (q *UniqueQueue[K, V]) Empty() bool
Empty returns true if the queue is empty.
func (*UniqueQueue[K, V]) Pop ¶
func (q *UniqueQueue[K, V]) Pop() (K, V)
Pop removes the first key and value from the queue.
func (*UniqueQueue[K, V]) Push ¶
func (q *UniqueQueue[K, V]) Push(key K, value V) bool
Push adds a key and its value to the queue. If the key is already in the queue, it will be ignored. It returns true if the key was added, false otherwise.
func (*UniqueQueue[K, V]) Seen ¶
func (q *UniqueQueue[K, V]) Seen(key K) bool
Seen returns true if the key is already in the queue.