java

package
v0.3.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2025 License: Apache-2.0 Imports: 29 Imported by: 1

Documentation

Overview

Package java provides an Enricher to add reachability annotations for Java Packages.

Index

Constants

View Source
const (
	// Name is the unique name of this detector.
	Name = "reachability/java"
	// MetaDirPath is the path to the META-INF directory.
	MetaDirPath = "META-INF"
)
View Source
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
)
View Source
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

View Source
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")
)
View Source
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")
)
View Source
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/",
	}
)
View Source
var MavenBaseURL = "https://repo1.maven.org/maven2"

MavenBaseURL is the base URL for the repository.

Functions

func GetMainClasses

func GetMainClasses(manifest io.Reader) ([]string, error)

GetMainClasses extracts the main class name from the MANIFEST.MF file in a .jar.

func IsStdLib

func IsStdLib(class string) bool

IsStdLib returns true if the class is a standard library class.

func NewDefault

func NewDefault() enricher.Enricher

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

func ParseClass(r io.Reader) (*ClassFile, error)

ParseClass parses a Java class file from a reader.

func (*ClassFile) ConstantPoolClass

func (cf *ClassFile) ConstantPoolClass(idx int) (string, error)

ConstantPoolClass returns the class name at the given index.

func (*ClassFile) ConstantPoolMethodref

func (cf *ClassFile) ConstantPoolMethodref(idx int) (class string, method string, descriptor string, err error)

ConstantPoolMethodref returns the class, method, and descriptor for a method reference at the given index.

func (*ClassFile) ConstantPoolUtf8

func (cf *ClassFile) ConstantPoolUtf8(idx int) (string, error)

ConstantPoolUtf8 returns the UTF-8 string 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

type ConstantDynamic struct {
	BootstrapMethodAttrIndex uint16
	NameAndTypeIndex         uint16
}

ConstantDynamic represents a dynamic constant pool entry.

func (ConstantDynamic) Type

func (c ConstantDynamic) Type() ConstantKind

Type returns the ConstantKind for ConstantDynamic.

type ConstantFieldref

type ConstantFieldref struct {
	ClassIndex       uint16
	NameAndTypeIndex uint16
}

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

type ConstantInterfaceMethodref struct {
	ClassIndex       uint16
	NameAndTypeIndex uint16
}

ConstantInterfaceMethodref represents an interface method reference constant pool entry.

func (ConstantInterfaceMethodref) Type

Type returns the ConstantKind for ConstantInterfaceMethodref.

type ConstantInvokeDynamic

type ConstantInvokeDynamic struct {
	BootstrapMethodAttrIndex uint16
	NameAndTypeIndex         uint16
}

ConstantInvokeDynamic represents an invoke dynamic constant pool entry.

func (ConstantInvokeDynamic) Type

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

type ConstantMethodHandle struct {
	ReferenceKind  uint8
	ReferenceIndex uint16
}

ConstantMethodHandle represents a method handle constant pool entry.

func (ConstantMethodHandle) Type

Type returns the ConstantKind for ConstantMethodHandle.

type ConstantMethodType

type ConstantMethodType struct {
	DescriptorIndex uint16
}

ConstantMethodType represents a method type constant pool entry.

func (ConstantMethodType) Type

Type returns the ConstantKind for ConstantMethodType.

type ConstantMethodref

type ConstantMethodref struct {
	ClassIndex       uint16
	NameAndTypeIndex uint16
}

ConstantMethodref represents a method reference constant pool entry.

func (ConstantMethodref) Type

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

type ConstantNameAndType struct {
	NameIndex       uint16
	DescriptorIndex uint16
}

ConstantNameAndType represents a name and type constant pool entry.

func (ConstantNameAndType) Type

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

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

type ConstantUtf8 struct {
	Length uint16
	Bytes  []byte
}

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.

func (*DefaultPackageFinder) Classes

func (f *DefaultPackageFinder) Classes(artifact string) ([]string, error)

Classes find returns a list of package names that contain a class path.

func (*DefaultPackageFinder) Find

func (f *DefaultPackageFinder) Find(classPath string) ([]string, error)

Find returns a list of package names that contain a class path.

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

func NewEnricher(client *http.Client) *Enricher

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) Name

func (Enricher) Name() string

Name returns the name of the enricher.

func (Enricher) RequiredPlugins

func (Enricher) RequiredPlugins() []string

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.

func (Enricher) Version

func (Enricher) Version() int

Version returns the version 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL