Skip to content

Api reference

Alexey Volkov edited this page Nov 24, 2022 · 7 revisions

Settings

JCDBSettings is used for creating instance of JCDB instance.

useJavaRuntime(file)

Use custom (not current process) java runtime for bytecode analysis.

There are two shortcuts JCDBSettings#useProcessJavaRuntime for using current process runtime (default option) and JCDBSettings#useJavaHomeRuntime for using Java runtime from JAVA_HOME environment variable

parameter type description
runtime File Required. File points to java runtime

persistent(location, clearOnStart)

Specify storing data properties

parameter type description
location string Optional. Location on file system to store Sqlite database file. Sqlite will be in-memory if null specified
clearOnStart boolean Force Sqlite database to cleanup stored data on startup. false by default

loadByteCode(files)

Which files to load on startup

parameter type description
files list of File jars or folders with compiled java code

watchFileSystem()

Should database look for filesystem changes

parameter type description
delay integer Optional. 10_000 by default. Watch interval for file system changes

installFeatures(features)

Which features jcdb will use

parameter type description
features Array of JcFeatures List of features. Features cannot be installed after jcdb is created

Database

JCDB instance created based on settings. If instance is not needed yet then close method should be called.

Properties

property type description
locations List of JcByteCodeLocation List of locations processed by database
persistence JCDBPersistence persistence which brings ability to read/write to database
runtimeVersion JavaRuntimeVersion version of java runtime which is used for this jcdb instance

classpath(dirOrJars)

Creates classpath instances

parameter type description
dirOrJars List of File List of files with bytecode to be loaded in current instance

refresh()

Refreshes state of jcdb instance and state of file systen. Should be called to clean up jars/folders that loaded versions are out of data and which are not used by any classpaths created by jcdb instance

rebuildFeatures()

Rebuild indexes for features installed in jcdb

awaitBackgroundJobs()

Await background jobs

close()

Is used to clean up resources used by jcdb instance

Classpath

Properties

Properties

property type description
locations List of JcByteCodeLocation List of locations attached to classpath
db JCDB database instance

findClassOrNull(name: String): JcClassOrInterface?

findClassOrNull(name)

Find class with bytecode (i.e arrays, primitives are not supported by this method).

parameter type description
name string name of the class (java.lang.String)

findTypeOrNull(name)

Find type by name. Arrays, primitives are supported:

  • arrays have [] in the end like java.lang.String[]
  • primitive has human readable names (checkout PredefiendPrimitives)
parameter type description
name string name of the type (java.lang.String[], int or java.util.List)

Classes

JcClassOrInterface

Represent bytecode from .class file

property type description
classpath JcClasspath classpath
access int access flag of class
name string human readable class name
simpleName string simple name
signature string signature from bytecode
declaredFields List of JcField List of declared fields
declaredMethods List of JcMethod List of declared methods
isAnonymous boolean is anonymous class
superClass JcClassOrInterface super class or null for java.lang.Object
interfaces List of JcClassOrInterface interfaces
outerMethod JcMethod outer method if any
outerClass JcClassOrInterface outer class if any
innerClasses List of JcClassOrInterface inner classes if any
annotations List of JcAnnotation list of annotations

bytecode()

return ASM ClassNode for this class

JcMethod

Represent method of JcClassOrInterface

property type description
enclosingClass JcClassOrInterface enclosing class
access int access flag of method
name string human readable class name
signature string signature from bytecode
description string jvm description of method
returnType string string with return type name
parameters List of JcParameter parameters
exceptions List of JcClassOrInterface exceptions that can be throws by method
annotations List of JcAnnotation list of annotations

bytecode()

return ASM MethodNode for this method

JcField

Represent field of JcClassOrInterface

property type description
enclosingClass JcClassOrInterface enclosing class
access int access flag of method
name string human readable class name
signature string method signature from bytecode
type string string with type of field
annotations List of JcAnnotation list of annotations

JcParameter

Represent method parameter

property type description
method JcMethod enclosing method
access int access flag of method
name string parameter name of present
type string string with parameter type name
index int index of parameter in method
annotations List of JcAnnotation list of annotations

JcAnnotation

Represent annotation

property type description
jcClass JcClassOrInterface annotation class. may be null
visible boolean access flag of method
values Map<String, Object> values of annotation according to java specification

matches(className)

Return true if className equals annotation class.

Types

classDiagram
    JcType <|-- JcPrimitiveType
    JcType <|-- JcRefType
    JcRefType <|-- JcUnboundWildcard
    JcRefType <|-- JcBoundedWildcard
    JcRefType <|-- JcArrayType
    JcRefType <|-- JcClassType
    JcRefType <|-- JcTypeVariable
    class JcType {
      +bool nullable
      +String typeName
    }

    class JcRefType {
    }
    class JcArrayType {
       +JcType elementType
    }

    class JcTypedMethod {
      +JcType[] parameters
      +JcType returnType
      +JcMethod method
    }

    class JcTypedField {
      +JcType type
      +JcField field
    }
    class JcClassType {
      +JcClassOrInterface jcClass
      +JcRefType[] typeParameters
      +JcRefType[] typeArguments
    }
    class JcTypeVariable {
      +String symbol
      +JcRefType[] bounds
    }
    class JcBoundedWildcard {
      +JcRefType lowerBound
      +JcRefType upperBound
    }
Loading

JcClassType

property type description
classpath JcClasspath classpath
jcClass JcClassOrInterface class
name string human readable class name
typeArguments List of JcRefType generics substitution
declaredFields List of JcTypedField List of declared typed fields
declaredMethods List of JcTypedMethod List of declared typed methods
fields List of JcTypedField all visible typed fields from whole hierarchy
methods List of JcTypedMethod all visible typed methods from whole hierarchy
superType JcClassType super class or null for java.lang.Object
interfaces List of JcClassType interfaces
outerMethod JcMethod outer typed method if any
outerType JcClassType outer type if any
innerType List of JcClassType inner types if any

JcTypedMethod

Represent typed method of JcClassType

property type description
name string human readable class name
returnType JcType return type name
typeParameters List of JcTypedParameter list of typed parameters
typeArguments List of JcRefType generics substitution
enclosingType JcClassOrInterface enclosing class

JcTypedField

Represent field of JcClassType

property type description
field JcField enclosing field
name string human readable class name
type JcType type of field
enclosingType JcClassType enclosing type

JcTypedParameter

Represent typed method parameter

property type description
method JcMethod enclosing method
access int access flag of method
name string parameter name of present
type string string with parameter type name
index int index of parameter in method

Hierarchy

Suitable for search class hierarchies. There are two features for calculating hierarchy: one is build upon persistence of bytecode another one uses (small enough) in-memory tree called InMemoryHierarchy.

findSubClasses(jcClass,allHierarchy)

Finds all classes in Classpath that are subclasses of jcClass

parameter type description
jcClass JcClassOrInterface or class name base class
allHierarchy boolean if true then will be return whole hierarchy

findOverrides(method)

Finds all methods in Classpath that override specified method

parameter type description
method JcMethod base method to search for overrides

Examples

find classes that implements java.lang.Runnable:

    val jcdb = jcdb {
        loadByteCode(allClasspath) // all classpath for current process
        installFeatures(InMemoryHierarchy) // without that line memory footprint will be low as well as performance
    }
    val classpath = jcdb.classpath(allClasspath)
    val extension = classpath.hierarchyExtension()
    extension.findSubClasses("java.lang.Runnable", allHierarchy = true) // all classes that implements `Runnable`
    
    val runMethod = classpath.findClassOrNull("java.lang.Runnable")!!.declaredMethods.first() // find run method
    extension.findOverrides(runMethod, allHierarchy = true) // all implementations of `Runnable#run` method

Usages (aka Call Graph)

Suitable to find fields and methods usages.

findUsages(method)

Finds usages of method in JcClasspath

parameter type description
method JcMethod method to search usages

findUsages(field, mode)

Finds usages of field in JcClasspath

parameter type description
field JcField base method to search for overrides
method enum search for READs or WRITEs of field

findInstantiations(jcClass) TBD

Finds all methods that instantiate instance of specified class

parameter type description
jcClass JcClassOrInterface class to search

Examples

find usages of

    val jcdb = jcdb {
        loadByteCode(allClasspath) // all classpath of current process
        installFeatures(Usages, InMemoryHierarchy) // without that line memory footprint will be low as well as performance
    }
    val classpath = jcdb.classpath(allClasspath)
    val extension = classpath.usagesExtension()
    
    val runMethod = classpath.findClassOrNull("java.lang.Runnable")!!.declaredMethods.first() // find run method
    extension.findUsages(runMethod) // all implementations of `Runnable#run` method

    val systemOutField = cp.findClass("java.lang.System").declaredFields.first { it.name == "out" }
    val result = ext.findUsages(systemOutField, FieldUsageMode.READ).toList()

Builders

Examples

find usages of

    val extension = classpath.buildersExtension()
    // find potential for instantiate DOM DocumentFactory builders
    //javax.xml.parsers.DocumentBuilderFactory#newInstance method will be in top of the list
    extension.findBuildMethods(classpath.findClass<DocumentBuilderFactory>()).toList()
Clone this wiki locally