Package com.tonic.analysis.xref
Class XrefDatabase
- java.lang.Object
-
- com.tonic.analysis.xref.XrefDatabase
-
public class XrefDatabase extends Object
Cross-reference store that keeps every xref indexed by source class, target class, target method, target field and reference type for constant-time lookup.
-
-
Constructor Summary
Constructors Constructor Description XrefDatabase()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddAllXrefs(Collection<Xref> xrefs)Stores a batch of cross-references.voidaddXref(Xref xref)Stores a cross-reference and files it into every index it belongs to.voidclear()Clear all xrefs and indexes.List<Xref>findCallersOfMethodNamed(String methodName)Collects callers of every indexed method carrying a given name, whatever the owner or descriptor.List<Xref>findRefsBetweenClasses(String sourceClass, String targetClass)Collects the references one class makes to another.List<Xref>getAllFieldReads()List<Xref>getAllFieldWrites()List<Xref>getAllInstantiations()List<Xref>getAllMethodCalls()List<Xref>getAllXrefs()longgetBuildTimeMs()Set<String>getClassesReferencedByClass(String className)Set<String>getClassesReferencingClass(String className)List<Xref>getRefsByType(XrefType type)List<Xref>getRefsFromClass(String className)List<Xref>getRefsFromMethod(MethodReference method)List<Xref>getRefsFromMethod(String owner, String name, String desc)Looks up the references made by a method named by its parts.List<Xref>getRefsToClass(String className)List<Xref>getRefsToField(FieldReference field)List<Xref>getRefsToField(String owner, String name, String desc)Looks up accesses of a field named by its parts.List<Xref>getRefsToMethod(MethodReference method)List<Xref>getRefsToMethod(String owner, String name, String desc)Looks up callers of a method named by its parts.List<Xref>getRefsToMethodByName(String owner, String name)Looks up callers by owner and name only, for queries that carry no descriptor.StringgetSummary()intgetTotalClasses()intgetTotalMethods()intgetTotalXrefCount()intgetUniqueSourceClassCount()intgetUniqueTargetClassCount()intgetUniqueTargetFieldCount()intgetUniqueTargetMethodCount()Map<XrefType,Integer>getXrefCountByType()Map<XrefType,List<Xref>>groupIncomingByType(String className)Map<XrefType,List<Xref>>groupOutgoingByType(String className)booleanisEmpty()Check if the database is empty.List<Xref>searchIncomingRefs(String query)Scans every reference for a target class or member containing the query, with dots in the query treated as package separators.List<Xref>searchOutgoingRefs(String query)Scans every reference for a source class or method containing the query, with dots in the query treated as package separators.voidsetBuildTimeMs(long buildTimeMs)Records how long the indexing run took.voidsetTotalClasses(int totalClasses)Records how many classes the indexing run scanned.voidsetTotalMethods(int totalMethods)Records how many methods the indexing run scanned.
-
-
-
Method Detail
-
addXref
public void addXref(Xref xref)
Stores a cross-reference and files it into every index it belongs to.- Parameters:
xref- the reference to add
-
addAllXrefs
public void addAllXrefs(Collection<Xref> xrefs)
Stores a batch of cross-references.- Parameters:
xrefs- the references to add
-
getRefsToClass
public List<Xref> getRefsToClass(String className)
- Parameters:
className- internal name of the referenced class- Returns:
- the references pointing at that class, empty if none
-
getRefsFromClass
public List<Xref> getRefsFromClass(String className)
- Parameters:
className- internal name of the referring class- Returns:
- the references originating in that class, empty if none
-
getRefsToMethod
public List<Xref> getRefsToMethod(MethodReference method)
- Parameters:
method- the called method- Returns:
- the references calling it, empty if none
-
getRefsToMethod
public List<Xref> getRefsToMethod(String owner, String name, String desc)
Looks up callers of a method named by its parts.- Parameters:
owner- internal name of the declaring classname- the method namedesc- the method descriptor- Returns:
- the references calling it, empty if none
-
getRefsToMethodByName
public List<Xref> getRefsToMethodByName(String owner, String name)
Looks up callers by owner and name only, for queries that carry no descriptor.- Parameters:
owner- internal name or simple name of the declaring class, or null for anyname- the method name, or null for any- Returns:
- the references calling any matching method
-
getRefsFromMethod
public List<Xref> getRefsFromMethod(MethodReference method)
- Parameters:
method- the calling method- Returns:
- the references originating in its body, empty if none
-
getRefsFromMethod
public List<Xref> getRefsFromMethod(String owner, String name, String desc)
Looks up the references made by a method named by its parts.- Parameters:
owner- internal name of the declaring classname- the method namedesc- the method descriptor- Returns:
- the references originating in its body, empty if none
-
getRefsToField
public List<Xref> getRefsToField(FieldReference field)
- Parameters:
field- the accessed field- Returns:
- the references reading or writing it, empty if none
-
getRefsToField
public List<Xref> getRefsToField(String owner, String name, String desc)
Looks up accesses of a field named by its parts.- Parameters:
owner- internal name of the declaring classname- the field namedesc- the field descriptor- Returns:
- the references reading or writing it, empty if none
-
getRefsByType
public List<Xref> getRefsByType(XrefType type)
- Parameters:
type- the reference kind to select- Returns:
- the references of that kind, empty if none
-
getAllInstantiations
public List<Xref> getAllInstantiations()
- Returns:
- every class-instantiation reference
-
searchIncomingRefs
public List<Xref> searchIncomingRefs(String query)
Scans every reference for a target class or member containing the query, with dots in the query treated as package separators.- Parameters:
query- the substring to match- Returns:
- the matching references
-
searchOutgoingRefs
public List<Xref> searchOutgoingRefs(String query)
Scans every reference for a source class or method containing the query, with dots in the query treated as package separators.- Parameters:
query- the substring to match- Returns:
- the matching references
-
findCallersOfMethodNamed
public List<Xref> findCallersOfMethodNamed(String methodName)
Collects callers of every indexed method carrying a given name, whatever the owner or descriptor.- Parameters:
methodName- the exact method name to match- Returns:
- the references calling any such method
-
findRefsBetweenClasses
public List<Xref> findRefsBetweenClasses(String sourceClass, String targetClass)
Collects the references one class makes to another.- Parameters:
sourceClass- internal name of the referring classtargetClass- internal name of the referenced class- Returns:
- the references from source to target
-
groupIncomingByType
public Map<XrefType,List<Xref>> groupIncomingByType(String className)
- Parameters:
className- internal name of the referenced class- Returns:
- the incoming references bucketed by reference kind
-
groupOutgoingByType
public Map<XrefType,List<Xref>> groupOutgoingByType(String className)
- Parameters:
className- internal name of the referring class- Returns:
- the outgoing references bucketed by reference kind
-
getClassesReferencingClass
public Set<String> getClassesReferencingClass(String className)
- Parameters:
className- internal name of the referenced class- Returns:
- the distinct classes that reference it
-
getClassesReferencedByClass
public Set<String> getClassesReferencedByClass(String className)
- Parameters:
className- internal name of the referring class- Returns:
- the distinct classes it references
-
getTotalXrefCount
public int getTotalXrefCount()
- Returns:
- the number of stored references
-
getXrefCountByType
public Map<XrefType,Integer> getXrefCountByType()
- Returns:
- the reference count per reference kind
-
getUniqueTargetClassCount
public int getUniqueTargetClassCount()
- Returns:
- the number of distinct referenced classes
-
getUniqueSourceClassCount
public int getUniqueSourceClassCount()
- Returns:
- the number of distinct referring classes
-
getUniqueTargetMethodCount
public int getUniqueTargetMethodCount()
- Returns:
- the number of distinct referenced methods
-
getUniqueTargetFieldCount
public int getUniqueTargetFieldCount()
- Returns:
- the number of distinct referenced fields
-
getBuildTimeMs
public long getBuildTimeMs()
- Returns:
- the build time ms
-
setBuildTimeMs
public void setBuildTimeMs(long buildTimeMs)
Records how long the indexing run took.- Parameters:
buildTimeMs- elapsed build time in milliseconds
-
getTotalClasses
public int getTotalClasses()
- Returns:
- the total classes
-
setTotalClasses
public void setTotalClasses(int totalClasses)
Records how many classes the indexing run scanned.- Parameters:
totalClasses- the scanned class count
-
getTotalMethods
public int getTotalMethods()
- Returns:
- the total methods
-
setTotalMethods
public void setTotalMethods(int totalMethods)
Records how many methods the indexing run scanned.- Parameters:
totalMethods- the scanned method count
-
getSummary
public String getSummary()
- Returns:
- a one-line summary of the reference, class and method counts and build time
-
clear
public void clear()
Clear all xrefs and indexes.
-
isEmpty
public boolean isEmpty()
Check if the database is empty.- Returns:
- true if no cross-references have been recorded
-
-