the first commit
This commit is contained in:
+60
@@ -0,0 +1,60 @@
|
|||||||
|
## OS Generated
|
||||||
|
|
||||||
|
.DS_Store
|
||||||
|
.DS_Store?
|
||||||
|
._*
|
||||||
|
.Spotlight-V100
|
||||||
|
.Trashes
|
||||||
|
ehthumbs.db
|
||||||
|
Thumbs.db
|
||||||
|
Desktop.ini
|
||||||
|
|
||||||
|
## Eclipse IDE
|
||||||
|
|
||||||
|
.project
|
||||||
|
.classpath
|
||||||
|
.settings/
|
||||||
|
.metadata
|
||||||
|
bin/
|
||||||
|
*.tmp
|
||||||
|
*.bak
|
||||||
|
*.swp
|
||||||
|
local.properties
|
||||||
|
.externalToolBuilders/
|
||||||
|
*.launch
|
||||||
|
.pmd
|
||||||
|
.eclipse-pmd
|
||||||
|
|
||||||
|
# Intellij Idea
|
||||||
|
.idea
|
||||||
|
*.iml
|
||||||
|
|
||||||
|
# Maven
|
||||||
|
target/
|
||||||
|
|
||||||
|
# Gradle
|
||||||
|
.gradle/
|
||||||
|
build/
|
||||||
|
out/
|
||||||
|
|
||||||
|
## Project specific
|
||||||
|
deploy
|
||||||
|
|
||||||
|
# Default by github for Java projects
|
||||||
|
|
||||||
|
*.class
|
||||||
|
|
||||||
|
# Mobile Tools for Java (J2ME)
|
||||||
|
.mtj.tmp/
|
||||||
|
|
||||||
|
# Package Files #
|
||||||
|
*.jar
|
||||||
|
*.war
|
||||||
|
*.ear
|
||||||
|
|
||||||
|
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||||
|
hs_err_pid*
|
||||||
|
|
||||||
|
# Maven versionsBackup
|
||||||
|
|
||||||
|
*.versionsBackup
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
Dux Technica Source Code License Agreement
|
DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL)
|
||||||
|
|
||||||
1. Introduction
|
1. Introduction
|
||||||
|
|
||||||
This is a Limited Use Source Code License Agreement ("Agreement") that is made and entered into by
|
This is a Limited Use Source Code License Agreement ("Agreement") that is made and entered into by
|
||||||
and between the Licensor, Dux Technica, and the Licensee. By accessing, downloading, or using the
|
and between the Licensor, DuxTechnica, and the Licensee. By accessing, downloading, or using the
|
||||||
source code provided by the Licensor (the "Source Code"), the Licensee agrees to be bound by the
|
source code provided by the Licensor (the "Source Code"), the Licensee agrees to be bound by the
|
||||||
terms and conditions of this Agreement.
|
terms and conditions of this Agreement.
|
||||||
|
|
||||||
@@ -11,8 +11,8 @@ terms and conditions of this Agreement.
|
|||||||
|
|
||||||
Subject to the terms and conditions of this Agreement, the Licensor hereby grants to the Licensee a
|
Subject to the terms and conditions of this Agreement, the Licensor hereby grants to the Licensee a
|
||||||
non-exclusive, non-transferable, limited license to use the Source Code solely for internal
|
non-exclusive, non-transferable, limited license to use the Source Code solely for internal
|
||||||
purposes. The Licensee may use the Source Code for evaluation, development, and testing purposes
|
purposes only. The Licensee may use the Source Code for evaluation, development, and testing
|
||||||
within their organisation.
|
purposes within their organisation.
|
||||||
|
|
||||||
3. Restrictions
|
3. Restrictions
|
||||||
|
|
||||||
@@ -31,6 +31,7 @@ reasonable steps to prevent unauthorised access or disclosure.
|
|||||||
Source Code.
|
Source Code.
|
||||||
|
|
||||||
4. Payment
|
4. Payment
|
||||||
|
|
||||||
Access to the Source Code may be subject to payment. The terms of payment will be specified
|
Access to the Source Code may be subject to payment. The terms of payment will be specified
|
||||||
separately by the Licensor. The Licensee agrees to pay any applicable fees as stipulated by the
|
separately by the Licensor. The Licensee agrees to pay any applicable fees as stipulated by the
|
||||||
Licensor before accessing the Source Code.
|
Licensor before accessing the Source Code.
|
||||||
@@ -78,5 +79,5 @@ amendments or modifications to this Agreement must be in writing and signed by b
|
|||||||
|
|
||||||
For any questions regarding this Agreement, please contact the Licensor at:
|
For any questions regarding this Agreement, please contact the Licensor at:
|
||||||
|
|
||||||
Dux Technica, Bangalore, India
|
DuxTechnica, Bangalore, India
|
||||||
email: [email protected]
|
email: [email protected]
|
||||||
|
|||||||
@@ -0,0 +1,102 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
~ Copyright (c) DuxTechnica
|
||||||
|
~
|
||||||
|
~ Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||||
|
~ use this file except in compliance with the License. You may obtain a copy of the License by
|
||||||
|
~ contacting:
|
||||||
|
~
|
||||||
|
~ Dux Technica, [email protected]
|
||||||
|
~
|
||||||
|
~ This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
|
~ either express or implied. See the License for the specific language governing permissions and
|
||||||
|
~ limitations under the License.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>dux.scanpath</groupId>
|
||||||
|
<artifactId>dux.scanpath</artifactId>
|
||||||
|
<version>v1-SNAPSHOT</version>
|
||||||
|
<name>scanpath</name>
|
||||||
|
<description>Scans the Java classpath for specific entities</description>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<plugin.compiler.version>3.12.1</plugin.compiler.version>
|
||||||
|
<plugin.javadoc.version>3.6.3</plugin.javadoc.version>
|
||||||
|
<plugin.source.version>3.3.0</plugin.source.version>
|
||||||
|
<plugin.release.version>3.1.1</plugin.release.version>
|
||||||
|
<plugin.deploy.version>3.1.1</plugin.deploy.version>
|
||||||
|
<target.java.version>17</target.java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>${plugin.compiler.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<release>${target.java.version}</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
<version>${plugin.javadoc.version}</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>javadoc</id>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>jar</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-source-plugin</artifactId>
|
||||||
|
<version>${plugin.source.version}</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>attach-sources</id>
|
||||||
|
<phase>verify</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>jar-no-fork</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-release-plugin</artifactId>
|
||||||
|
<version>${plugin.release.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<useReleaseProfile>false</useReleaseProfile>
|
||||||
|
<releaseProfiles>release</releaseProfiles>
|
||||||
|
<autoVersionSubmodules>true</autoVersionSubmodules>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-deploy-plugin</artifactId>
|
||||||
|
<version>${plugin.deploy.version}</version>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.javassist</groupId>
|
||||||
|
<artifactId>javassist</artifactId>
|
||||||
|
<version>3.30.2-GA</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,307 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) DuxTechnica
|
||||||
|
*
|
||||||
|
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||||
|
* contacting:
|
||||||
|
*
|
||||||
|
* Dux Technica, [email protected]
|
||||||
|
*
|
||||||
|
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
|
* either express or implied. See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dux.scanpath.v1;
|
||||||
|
|
||||||
|
import dux.scanpath.v1.support.ClassAnnotationVisitor;
|
||||||
|
import dux.scanpath.v1.support.ClassFile;
|
||||||
|
import dux.scanpath.v1.support.ClassFilter;
|
||||||
|
import dux.scanpath.v1.support.ClassList;
|
||||||
|
import dux.scanpath.v1.support.ClassVisitor;
|
||||||
|
import dux.scanpath.v1.support.DiskClassList;
|
||||||
|
import dux.scanpath.v1.support.FieldAnnotationVisitor;
|
||||||
|
import dux.scanpath.v1.support.InheritanceVisitor;
|
||||||
|
import dux.scanpath.v1.support.JarClassList;
|
||||||
|
import dux.scanpath.v1.support.MetaStore;
|
||||||
|
import dux.scanpath.v1.support.MethodAnnotationVisitor;
|
||||||
|
import dux.scanpath.v1.support.ZipClassList;
|
||||||
|
import javassist.ClassPool;
|
||||||
|
import javassist.NotFoundException;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileFilter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.jar.JarFile;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
import java.util.zip.ZipFile;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scans one or more Java classpaths and indexes the discovered types so callers can query
|
||||||
|
* inheritance, annotation, field, and method metadata without having to load every class manually.
|
||||||
|
*
|
||||||
|
* @author indroneel
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class ClasspathScanner {
|
||||||
|
|
||||||
|
private static final Logger _L = Logger.getLogger(ClasspathScanner.class.getName());
|
||||||
|
|
||||||
|
private final List<ClassList> classPaths;
|
||||||
|
private final List<ClassVisitor> visitors;
|
||||||
|
private final MetaStore metaStore;
|
||||||
|
private final ClassPool clsPool;
|
||||||
|
private final ClassFilter clsFilter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new classpath scanner to search for specific items on the classpath.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public ClasspathScanner() {
|
||||||
|
classPaths = new ArrayList<>();
|
||||||
|
clsPool = new ClassPool();
|
||||||
|
clsPool.appendSystemPath();
|
||||||
|
|
||||||
|
visitors = new ArrayList<>();
|
||||||
|
metaStore = new MetaStore();
|
||||||
|
clsFilter = new ClassFilter();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a custom classloader that is subsequently used to load classes from the scanned class path.
|
||||||
|
*
|
||||||
|
* @param cl a custom classloader that is used to load the classes. A null value will default
|
||||||
|
* to the system classloader.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public void setClassLoader(ClassLoader cl) {
|
||||||
|
metaStore.setClassLoader(cl);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare the list of Java types that would be further looked up for types with specific
|
||||||
|
* characteristics and/or constructs.
|
||||||
|
*
|
||||||
|
* @param sscope provides the configuration, in terms of classpaths and name matching criteria,
|
||||||
|
* that is used to arrive at the list of Java types for further lookup.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public void load(ScanScope sscope) {
|
||||||
|
if(sscope == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(String path : sscope.classPaths()) {
|
||||||
|
addClassPath(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(String path : sscope.libPaths()) {
|
||||||
|
addLibPath(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(String name : sscope.includedPackages()) {
|
||||||
|
clsFilter.includePackage(name);
|
||||||
|
if(_L.isLoggable(Level.FINE)) {
|
||||||
|
_L.fine("package_to_scan = %s (included)".formatted(name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(String name : sscope.excludedPackages()) {
|
||||||
|
clsFilter.excludePackage(name);
|
||||||
|
if(_L.isLoggable(Level.FINE)) {
|
||||||
|
_L.fine("package_to_scan = %s (excluded)".formatted(name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(String pattern : sscope.includedClassesByPattern()) {
|
||||||
|
clsFilter.includeClassesByPattern(pattern);
|
||||||
|
if(_L.isLoggable(Level.FINE)) {
|
||||||
|
_L.fine(String.format("class_to_scan pattern = %s (included)", pattern));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(String pattern : sscope.excludedClassesByPattern()) {
|
||||||
|
clsFilter.excludeClassesByPattern(pattern);
|
||||||
|
if(_L.isLoggable(Level.FINE)) {
|
||||||
|
_L.fine(String.format("class_to_scan pattern = %s (excluded)", pattern));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(String name : sscope.includedClasses()) {
|
||||||
|
clsFilter.includeClass(name);
|
||||||
|
if(_L.isLoggable(Level.FINE)) {
|
||||||
|
_L.fine(String.format("class_to_scan = %s (included)", name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(String name : sscope.excludedClasses()) {
|
||||||
|
clsFilter.excludeClass(name);
|
||||||
|
if(_L.isLoggable(Level.FINE)) {
|
||||||
|
_L.fine(String.format("class_to_scan = %s (excluded)", name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
visitors.clear();
|
||||||
|
addClassVisitor(new InheritanceVisitor());
|
||||||
|
addClassVisitor(new ClassAnnotationVisitor());
|
||||||
|
addClassVisitor(new FieldAnnotationVisitor());
|
||||||
|
addClassVisitor(new MethodAnnotationVisitor());
|
||||||
|
|
||||||
|
for(ClassList clsList : classPaths) {
|
||||||
|
for(ClassFile cf : clsList.getFiles()) {
|
||||||
|
visitClass(cf);
|
||||||
|
}
|
||||||
|
clsList.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(_L.isLoggable(Level.INFO)) {
|
||||||
|
_L.info("done loading meta information from classpath");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a list of Java types that derives/implements, either directly or indirectly, the given
|
||||||
|
* type.
|
||||||
|
*
|
||||||
|
* @param type a Java type that is present on the type hierarchy for each entry in the returned
|
||||||
|
* list of Java types.
|
||||||
|
* @return the corresponding list of Java types.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public List<Class<?>> listInheritingClasses(Class<?> type) {
|
||||||
|
return metaStore.listInheritingClasses(type.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a list of Java types, that are qualified with the given annotation. The list also
|
||||||
|
* includes types that derive from or inherit at least one Java type that is qualified with the
|
||||||
|
* given annotation.
|
||||||
|
*
|
||||||
|
* @param ann the annotation, the target type being a Java class.
|
||||||
|
* @return the corresponding list of Java types that have the annotation, or derive/inherit from
|
||||||
|
* one or more Java types that bear the annotation.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public List<Class<?>> listAnnotatedClasses(Class<?> ann) {
|
||||||
|
return metaStore.listAnnotatedClasses(ann.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a list of Java types, that encapsulates at least one field, that is qualified with the
|
||||||
|
* given annotation. The list also includes types that derive/inherit from at least one Java type
|
||||||
|
* that encapsulates at least one field that is qualified with the given annotation.
|
||||||
|
*
|
||||||
|
* @param ann the annotation, the target type being a Java field.
|
||||||
|
* @return the corresponding list of Java types.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public List<Class<?>> listClassesWithAnnotatedFields(Class<?> ann) {
|
||||||
|
return metaStore.listClassesWithAnnotatedFields(ann.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a list of Java types, that encapsulates at least one method, that is qualified with the
|
||||||
|
* given annotation. The list also includes types that derive/inherit from at least one Java type
|
||||||
|
* that encapsulates at least one method that is qualified with the given annotation.
|
||||||
|
*
|
||||||
|
* @param ann the annotation, the target type being a Java method.
|
||||||
|
* @return the corresponding list of Java types.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public List<Class<?>> listClassesWithAnnotatedMethods(Class<?> ann) {
|
||||||
|
return metaStore.listClassesWithAnnotatedMethods(ann.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Helper methods
|
||||||
|
|
||||||
|
private void addClassPath(String path) {
|
||||||
|
File f = new File(path);
|
||||||
|
if(f.isDirectory()) {
|
||||||
|
classPaths.add(new DiskClassList(f));
|
||||||
|
try {
|
||||||
|
if(path.endsWith(File.separator)) {
|
||||||
|
path = path.substring(0, path.length() - 1);
|
||||||
|
}
|
||||||
|
clsPool.appendClassPath(path);
|
||||||
|
if(_L.isLoggable(Level.FINE)) {
|
||||||
|
_L.fine(String.format("path = %s (folder)", path));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (NotFoundException exep) {
|
||||||
|
if(_L.isLoggable(Level.WARNING)) {
|
||||||
|
_L.log(Level.WARNING, "error adding classpath", exep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(f.getName().toLowerCase().endsWith(".zip")) {
|
||||||
|
try {
|
||||||
|
classPaths.add(new ZipClassList(new ZipFile(f)));
|
||||||
|
clsPool.appendClassPath(path);
|
||||||
|
if(_L.isLoggable(Level.FINE)) {
|
||||||
|
_L.fine(String.format("path = %s (zipfile)", path));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IOException | NotFoundException exep) {
|
||||||
|
if(_L.isLoggable(Level.WARNING)) {
|
||||||
|
_L.log(Level.WARNING, "error adding classpath", exep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(f.getName().toLowerCase().endsWith(".jar")) {
|
||||||
|
try {
|
||||||
|
classPaths.add(new JarClassList(new JarFile(f)));
|
||||||
|
clsPool.appendClassPath(path);
|
||||||
|
if(_L.isLoggable(Level.FINE)) {
|
||||||
|
_L.fine(String.format("path = %s (jarfile)", path));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IOException | NotFoundException exep) {
|
||||||
|
if(_L.isLoggable(Level.WARNING)) {
|
||||||
|
_L.log(Level.WARNING, "error adding classpath", exep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if(_L.isLoggable(Level.FINE)) {
|
||||||
|
_L.fine(String.format("path = %s (skipped)", path));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addLibPath(String path) {
|
||||||
|
File pathAsFile = new File(path);
|
||||||
|
File[] archives = pathAsFile.listFiles(new FileFilter() {
|
||||||
|
@Override
|
||||||
|
public boolean accept(File f) {
|
||||||
|
String name = f.getName().toLowerCase();
|
||||||
|
return name.endsWith(".zip") || name.endsWith(".jar");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
assert archives != null;
|
||||||
|
for(File f : archives) {
|
||||||
|
addClassPath(f.getAbsolutePath());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addClassVisitor(ClassVisitor cv) {
|
||||||
|
cv.setMetaStore(metaStore);
|
||||||
|
cv.setClassPool(clsPool);
|
||||||
|
visitors.add(cv);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void visitClass(ClassFile cf) {
|
||||||
|
if(!clsFilter.accept(cf.getName())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(_L.isLoggable(Level.FINE)) {
|
||||||
|
_L.fine("visiting class %s".formatted(cf.getName()));
|
||||||
|
}
|
||||||
|
for(ClassVisitor cv: visitors) {
|
||||||
|
cv.visit(cf.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,299 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) DuxTechnica
|
||||||
|
*
|
||||||
|
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||||
|
* contacting:
|
||||||
|
*
|
||||||
|
* Dux Technica, [email protected]
|
||||||
|
*
|
||||||
|
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
|
* either express or implied. See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dux.scanpath.v1;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is used to specify the classpath entries and library files to be scanned for specific
|
||||||
|
* Java types. It also defines the name patterns to be used to filter out and arrive at the final
|
||||||
|
* list of Java types to be used by the classpath browser for lookup operations.
|
||||||
|
*
|
||||||
|
* @author indroneel
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class ScanScope {
|
||||||
|
|
||||||
|
private final List<String> classPaths;
|
||||||
|
private final List<String> libPaths;
|
||||||
|
private final List<String> includePkgs;
|
||||||
|
private final List<String> excludePkgs;
|
||||||
|
private final List<String> includeClasses;
|
||||||
|
private final List<String> excludeClasses;
|
||||||
|
private final List<String> includePats;
|
||||||
|
private final List<String> excludePats;
|
||||||
|
private boolean includeSCP;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new scan scope with no classpath or libpath entries, and no package or type name
|
||||||
|
* filters. By default, the system classpath is included in the scan scope.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public ScanScope() {
|
||||||
|
includeSCP = true;
|
||||||
|
classPaths = new LinkedList<>();
|
||||||
|
libPaths = new LinkedList<>();
|
||||||
|
includePkgs = new LinkedList<>();
|
||||||
|
excludePkgs = new LinkedList<>();
|
||||||
|
includeClasses = new LinkedList<>();
|
||||||
|
excludeClasses = new LinkedList<>();
|
||||||
|
includePats = new LinkedList<>();
|
||||||
|
excludePats = new LinkedList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the classpath entries to scan. When system classpath inclusion is enabled, entries from
|
||||||
|
* the {@code java.class.path} system property are returned before the explicitly added entries.
|
||||||
|
*
|
||||||
|
* @return the classpath entries to scan
|
||||||
|
*/
|
||||||
|
|
||||||
|
List<String> classPaths() {
|
||||||
|
List<String> result = new LinkedList<>();
|
||||||
|
if(includeSCP) {
|
||||||
|
//Prepare the java class path for subsequent scanning.
|
||||||
|
String javaClassPath = System.getProperty("java.class.path");
|
||||||
|
if (javaClassPath != null) {
|
||||||
|
Collections.addAll(result, javaClassPath.split(File.pathSeparator));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.addAll(classPaths);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the library paths whose JAR and ZIP files should be scanned.
|
||||||
|
*
|
||||||
|
* @return the configured library paths
|
||||||
|
*/
|
||||||
|
|
||||||
|
List<String> libPaths() {
|
||||||
|
return libPaths;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the package names included in the scan scope. Included packages also cover their
|
||||||
|
* subpackages.
|
||||||
|
*
|
||||||
|
* @return the package names to include
|
||||||
|
*/
|
||||||
|
|
||||||
|
List<String> includedPackages() {
|
||||||
|
return includePkgs;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the package names excluded from the scan scope. Excluded packages also cover their
|
||||||
|
* subpackages.
|
||||||
|
*
|
||||||
|
* @return the package names to exclude
|
||||||
|
*/
|
||||||
|
|
||||||
|
List<String> excludedPackages() {
|
||||||
|
return excludePkgs;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the fully qualified type names included in the scan scope.
|
||||||
|
*
|
||||||
|
* @return the fully qualified type names to include
|
||||||
|
*/
|
||||||
|
|
||||||
|
List<String> includedClasses() {
|
||||||
|
return includeClasses;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the fully qualified type names excluded from the scan scope.
|
||||||
|
*
|
||||||
|
* @return the fully qualified type names to exclude
|
||||||
|
*/
|
||||||
|
|
||||||
|
List<String> excludedClasses() {
|
||||||
|
return excludeClasses;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the regular expressions used to include fully qualified type names in the scan scope.
|
||||||
|
*
|
||||||
|
* @return the inclusion regular expressions
|
||||||
|
*/
|
||||||
|
|
||||||
|
List<String> includedClassesByPattern() {
|
||||||
|
return includePats;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the regular expressions used to exclude fully qualified type names from the scan scope.
|
||||||
|
*
|
||||||
|
* @return the exclusion regular expressions
|
||||||
|
*/
|
||||||
|
|
||||||
|
List<String> excludedClassesByPattern() {
|
||||||
|
return excludePats;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Public methods
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an entry to the list of classpaths that should be scanned by the classpath browser for
|
||||||
|
* lookup of specific Java types. A classpath denotes an absolute or relative path to a folder, or a
|
||||||
|
* ZIP/JAR archive containing a set of Java class files.
|
||||||
|
*
|
||||||
|
* @param path a classpath entry
|
||||||
|
* @return this object for fluent style invocation of methods.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public ScanScope addClassPath(String path) {
|
||||||
|
classPaths.add(path);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an entry to the list of libpaths that should be scanned by the classpath browser for lookup
|
||||||
|
* of specific Java types. A libpath is a folder on the local file system that contains a set of ZIP
|
||||||
|
* and JAR files, each of which should be treated as a classpath entry.
|
||||||
|
*
|
||||||
|
* @param path a libpath entry
|
||||||
|
* @return this object for fluent style invocation of methods.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public ScanScope addLibPath(String path) {
|
||||||
|
libPaths.add(path);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Excludes the system classpath from the scan scope. By default, the system classpath is included
|
||||||
|
* in the scan scope. This method can be used to exclude it if desired.
|
||||||
|
*
|
||||||
|
* @return this object for fluent style invocation of methods.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public ScanScope excludeSystemClassPath() {
|
||||||
|
includeSCP = false;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an entry to the list of package names to narrow down the set of Java types for lookup
|
||||||
|
* operations by the classpath browser. Use of wildcard or any other form of pattern-matching
|
||||||
|
* expressions are not allowed. All Java types under this packages and sub-packages are included in
|
||||||
|
* the lookup scope.
|
||||||
|
*
|
||||||
|
* @param name a Java package name for inclusion.
|
||||||
|
* @return this object for fluent style invocation of methods.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public ScanScope includePackage(String name) {
|
||||||
|
includePkgs.add(name);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an entry to the list of package names to narrow down the set of Java types for lookup
|
||||||
|
* operations by the classpath browser. Use of wildcard or any other form of pattern-matching
|
||||||
|
* expressions are not allowed. All Java types under this packages and sub-packages are excluded
|
||||||
|
* from the lookup scope.
|
||||||
|
*
|
||||||
|
* @param name a Java package name for exclusion.
|
||||||
|
* @return this object for fluent style invocation of methods.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public ScanScope excludePackage(String name) {
|
||||||
|
excludePkgs.add(name);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Adds an entry to the list of inclusion patterns to narrow down the set of Java types for lookup
|
||||||
|
* operations by the classpath browser. These patterns follow the semantics of Java regular
|
||||||
|
* expressions. Each fully qualified type name on the classpaths and libpaths is matched against
|
||||||
|
* these patterns for inclusion in the lookup scope.
|
||||||
|
*
|
||||||
|
* @param pattern a regular expression that can match a Java type name.
|
||||||
|
* @return this object for fluent style invocation of methods.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public ScanScope includeClassesByPattern(String pattern) {
|
||||||
|
includePats.add(pattern);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Adds an entry to the list of exclusion patterns to narrow down the set of Java types for lookup
|
||||||
|
* operations by the classpath browser. These patterns follow the semantics of Java regular
|
||||||
|
* expressions. Each fully qualified type name on the classpaths and libpaths is matched against
|
||||||
|
* these patterns for exclusion from the lookup scope.
|
||||||
|
*
|
||||||
|
* @param pattern a regular expression that can match a Java type name.
|
||||||
|
* @return this object for fluent style invocation of methods.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public ScanScope excludeClassesByPattern(String pattern) {
|
||||||
|
excludePats.add(pattern);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an entry to the list of type names to narrow down the set of Java types for lookup
|
||||||
|
* operations by the classpath browser. Use of wildcard or any other form of pattern-matching
|
||||||
|
* expressions are not allowed.
|
||||||
|
*
|
||||||
|
* @param name a fully qualified type name for inclusion in the lookup scope.
|
||||||
|
* @return this object for fluent style invocation of methods.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public ScanScope includeClass(String name) {
|
||||||
|
includeClasses.add(name);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an entry to the list of type names to narrow down the set of Java types for lookup
|
||||||
|
* operations by the classpath browser. Use of wildcard or any other form of pattern-matching
|
||||||
|
* expressions are not allowed.
|
||||||
|
*
|
||||||
|
* @param name a fully qualified type name for exclusion from the lookup scope.
|
||||||
|
* @return this object for fluent style invocation of methods.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public ScanScope excludeClass(String name) {
|
||||||
|
excludeClasses.add(name);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new scanning scope using the data contained within this scope.
|
||||||
|
*
|
||||||
|
* @return the new scan scope
|
||||||
|
*/
|
||||||
|
|
||||||
|
public ScanScope duplicate() {
|
||||||
|
ScanScope scope = new ScanScope();
|
||||||
|
scope.classPaths.addAll(classPaths);
|
||||||
|
scope.libPaths.addAll(libPaths);
|
||||||
|
scope.includePkgs.addAll(includePkgs);
|
||||||
|
scope.excludePkgs.addAll(excludePkgs);
|
||||||
|
scope.includeClasses.addAll(includeClasses);
|
||||||
|
scope.excludeClasses.addAll(excludeClasses);
|
||||||
|
scope.includePats.addAll(includePats);
|
||||||
|
scope.excludePats.addAll(excludePats);
|
||||||
|
scope.includeSCP = includeSCP;
|
||||||
|
return scope;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) DuxTechnica
|
||||||
|
*
|
||||||
|
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||||
|
* contacting:
|
||||||
|
*
|
||||||
|
* Dux Technica, [email protected]
|
||||||
|
*
|
||||||
|
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
|
* either express or implied. See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dux.scanpath.v1.support;
|
||||||
|
|
||||||
|
import javassist.CtClass;
|
||||||
|
import javassist.NotFoundException;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visits classes and records the annotations declared on their class, interface, and superclass
|
||||||
|
* hierarchy.
|
||||||
|
*
|
||||||
|
* @author indroneel
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class ClassAnnotationVisitor extends ClassVisitor {
|
||||||
|
|
||||||
|
private static final Logger _L = Logger.getLogger(ClassAnnotationVisitor.class.getName());
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Methods of base class ClassVisitor
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(String className) {
|
||||||
|
try {
|
||||||
|
CtClass cls = clsPool.get(className);
|
||||||
|
visitImpl(className, cls);
|
||||||
|
}
|
||||||
|
catch (ClassNotFoundException | NotFoundException exep) {
|
||||||
|
if(_L.isLoggable(Level.WARNING)) {
|
||||||
|
_L.log(Level.WARNING, "unable to visit class %s".formatted(className), exep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Helper methods
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collects annotations from the supplied class and its related types and
|
||||||
|
* adds the corresponding class-to-annotation mappings to the metadata store.
|
||||||
|
*
|
||||||
|
* @param startClsName the name of the class from which traversal starts
|
||||||
|
* @param cls the class whose hierarchy is traversed
|
||||||
|
* @throws ClassNotFoundException if a referenced class cannot be found
|
||||||
|
* @throws NotFoundException if a referenced class member cannot be found
|
||||||
|
*/
|
||||||
|
|
||||||
|
private void visitImpl(String startClsName, CtClass cls)
|
||||||
|
throws ClassNotFoundException, NotFoundException {
|
||||||
|
|
||||||
|
CtClass[] clscol = collateClasses(cls);
|
||||||
|
for(CtClass scls : clscol) {
|
||||||
|
Object[] annotations = scls.getAnnotations();
|
||||||
|
if(annotations != null) {
|
||||||
|
for(Object obj : annotations) {
|
||||||
|
Annotation ann = (Annotation) obj;
|
||||||
|
metaStore.addClassWithAnnotation(
|
||||||
|
ann.annotationType().getName(), cls.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collates a class with its interfaces and superclass hierarchy.
|
||||||
|
*
|
||||||
|
* @param cls the class whose related types should be collated
|
||||||
|
* @return the class and all related interfaces and superclasses
|
||||||
|
* @throws NotFoundException if a related type cannot be found
|
||||||
|
*/
|
||||||
|
|
||||||
|
private CtClass[] collateClasses(CtClass cls) throws NotFoundException {
|
||||||
|
CtClass[] retCol = new CtClass[1];
|
||||||
|
retCol[0] = cls;
|
||||||
|
|
||||||
|
CtClass[] ifaces = cls.getInterfaces();
|
||||||
|
for(CtClass iface : ifaces) {
|
||||||
|
CtClass[] ifcol = collateClasses(iface);
|
||||||
|
CtClass[] combined = new CtClass[retCol.length + ifcol.length];
|
||||||
|
System.arraycopy(retCol, 0, combined, 0, retCol.length);
|
||||||
|
System.arraycopy(ifcol, 0, combined, retCol.length, ifcol.length);
|
||||||
|
retCol = combined;
|
||||||
|
}
|
||||||
|
|
||||||
|
CtClass supcls = cls.getSuperclass();
|
||||||
|
if(supcls != null && !supcls.getName().equals("java.lang.Object")) {
|
||||||
|
CtClass[] supcol = collateClasses(supcls);
|
||||||
|
CtClass[] combined = new CtClass[retCol.length + supcol.length];
|
||||||
|
System.arraycopy(retCol, 0, combined, 0, retCol.length);
|
||||||
|
System.arraycopy(supcol, 0, combined, retCol.length, supcol.length);
|
||||||
|
retCol = combined;
|
||||||
|
}
|
||||||
|
return retCol;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) DuxTechnica
|
||||||
|
*
|
||||||
|
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||||
|
* contacting:
|
||||||
|
*
|
||||||
|
* Dux Technica, [email protected]
|
||||||
|
*
|
||||||
|
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
|
* either express or implied. See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dux.scanpath.v1.support;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstracts out a file that contains bytecodes of a Java class.
|
||||||
|
*
|
||||||
|
* @author indroneel
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public interface ClassFile {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the fully qualified name of the class whose bytecodes are contained in this file.
|
||||||
|
*
|
||||||
|
* @return the fully qualified class name.
|
||||||
|
*/
|
||||||
|
|
||||||
|
String getName();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the name of the class whose bytecodes are contained in this file.
|
||||||
|
*
|
||||||
|
* @return the class name without any package information.
|
||||||
|
*/
|
||||||
|
|
||||||
|
String getSimpleName();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an input stream to read bytecode contents of the class file.
|
||||||
|
*
|
||||||
|
* @return an input stream to read from.
|
||||||
|
* @throws IOException for any errors in file operations.
|
||||||
|
*/
|
||||||
|
|
||||||
|
InputStream openInputStream() throws IOException;
|
||||||
|
}
|
||||||
@@ -0,0 +1,193 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) DuxTechnica
|
||||||
|
*
|
||||||
|
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||||
|
* contacting:
|
||||||
|
*
|
||||||
|
* Dux Technica, [email protected]
|
||||||
|
*
|
||||||
|
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
|
* either express or implied. See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dux.scanpath.v1.support;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
import java.util.regex.PatternSyntaxException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters classes based on package, class name, and class name patterns. This is used to limit the
|
||||||
|
* scope of the classpath scanning process.
|
||||||
|
*
|
||||||
|
* @author indroneel
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class ClassFilter {
|
||||||
|
|
||||||
|
private static final Logger _L = Logger.getLogger(ClassFilter.class.getName());
|
||||||
|
|
||||||
|
private final List<String> includePkgs;
|
||||||
|
private final List<String> excludePkgs;
|
||||||
|
private final List<Pattern> includeClsPatterns;
|
||||||
|
private final List<Pattern> excludeClsPatterns;
|
||||||
|
private final List<String> includeClsNames;
|
||||||
|
private final List<String> excludeClsNames;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new class filter with no includes or excludes. All classes will be rejected until the
|
||||||
|
* caller adds include/exclude rules.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public ClassFilter() {
|
||||||
|
includePkgs = new ArrayList<>();
|
||||||
|
excludePkgs = new ArrayList<>();
|
||||||
|
includeClsPatterns = new ArrayList<>();
|
||||||
|
excludeClsPatterns = new ArrayList<>();
|
||||||
|
includeClsNames = new ArrayList<>();
|
||||||
|
excludeClsNames = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a package to the list of included packages. All classes in this package and its subpackages
|
||||||
|
* will be accepted unless they are explicitly excluded.
|
||||||
|
*
|
||||||
|
* @param pkgName the package name to include
|
||||||
|
*/
|
||||||
|
|
||||||
|
public void includePackage(String pkgName) {
|
||||||
|
includePkgs.add(pkgName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a package to the list of excluded packages. All classes in this package and its subpackages
|
||||||
|
* will be rejected unless they are explicitly included.
|
||||||
|
*
|
||||||
|
* @param pkgName the package name to exclude
|
||||||
|
*/
|
||||||
|
|
||||||
|
public void excludePackage(String pkgName) {
|
||||||
|
excludePkgs.add(pkgName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a class name pattern to the list of included class patterns. All classes matching this
|
||||||
|
* pattern will be accepted unless they are explicitly excluded.
|
||||||
|
*
|
||||||
|
* @param pattern the class name pattern to include
|
||||||
|
*/
|
||||||
|
|
||||||
|
public void includeClassesByPattern(String pattern) {
|
||||||
|
try {
|
||||||
|
includeClsPatterns.add(Pattern.compile(pattern));
|
||||||
|
}
|
||||||
|
catch(PatternSyntaxException exep) {
|
||||||
|
if(_L.isLoggable(Level.WARNING)) {
|
||||||
|
_L.log(Level.WARNING, "include_class_pattern error compiling", exep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a class name pattern to the list of excluded class patterns. All classes matching this
|
||||||
|
* pattern will be rejected unless they are explicitly included.
|
||||||
|
*
|
||||||
|
* @param pattern the class name pattern to exclude
|
||||||
|
*/
|
||||||
|
|
||||||
|
public void excludeClassesByPattern(String pattern) {
|
||||||
|
try {
|
||||||
|
excludeClsPatterns.add(Pattern.compile(pattern));
|
||||||
|
}
|
||||||
|
catch(PatternSyntaxException exep) {
|
||||||
|
if(_L.isLoggable(Level.WARNING)) {
|
||||||
|
_L.log(Level.WARNING, "exclude_class_pattern error compiling", exep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a class name to the list of included classes. This class will be accepted unless it is
|
||||||
|
* explicitly excluded.
|
||||||
|
*
|
||||||
|
* @param name the class name to include
|
||||||
|
*/
|
||||||
|
|
||||||
|
public void includeClass(String name) {
|
||||||
|
includeClsNames.add(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a class name to the list of excluded classes. This class will be rejected unless it is
|
||||||
|
* explicitly included.
|
||||||
|
*
|
||||||
|
* @param name the class name to exclude
|
||||||
|
*/
|
||||||
|
|
||||||
|
public void excludeClass(String name) {
|
||||||
|
excludeClsNames.add(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines whether a class should be accepted based on the include/exclude rules.
|
||||||
|
*
|
||||||
|
* @param className the fully qualified class name to check
|
||||||
|
* @return true if the class is accepted, false otherwise
|
||||||
|
*/
|
||||||
|
|
||||||
|
public boolean accept(String className) {
|
||||||
|
|
||||||
|
boolean exChecks = !excludePkgs.isEmpty() || !excludeClsNames.isEmpty()
|
||||||
|
|| !excludeClsPatterns.isEmpty();
|
||||||
|
boolean inChecks = !includePkgs.isEmpty() || !includeClsNames.isEmpty()
|
||||||
|
|| !includeClsPatterns.isEmpty();
|
||||||
|
|
||||||
|
// if the scope does not define any includes, then reject
|
||||||
|
if(!inChecks) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(Pattern pattern : excludeClsPatterns) {
|
||||||
|
if(pattern.matcher(className).matches()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(String clsStr : excludeClsNames) {
|
||||||
|
if(className.equals(clsStr)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(String pkgStr : excludePkgs) {
|
||||||
|
if(className.startsWith(pkgStr + '.')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Pattern pattern : includeClsPatterns) {
|
||||||
|
if (pattern.matcher(className).matches()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String clsStr : includeClsNames) {
|
||||||
|
if (className.equals(clsStr)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String pkgStr : includePkgs) {
|
||||||
|
if (className.startsWith(pkgStr + '.')) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) DuxTechnica
|
||||||
|
*
|
||||||
|
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||||
|
* contacting:
|
||||||
|
*
|
||||||
|
* Dux Technica, [email protected]
|
||||||
|
*
|
||||||
|
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
|
* either express or implied. See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dux.scanpath.v1.support;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstracts out a collection of class files that can be read from a specific location. This
|
||||||
|
* location can be a starting filesystem folder, a ZIP or a JAR file.
|
||||||
|
*
|
||||||
|
* @author indroneel
|
||||||
|
*/
|
||||||
|
|
||||||
|
public interface ClassList {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an iterable collection of ClassFile objects that can be read from this ClassList. The
|
||||||
|
* returned collection is not guaranteed to be sorted in any particular order.
|
||||||
|
*
|
||||||
|
* @return an iterable collection of ClassFile objects that can be read from this ClassList
|
||||||
|
*/
|
||||||
|
|
||||||
|
Iterable<ClassFile> getFiles();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes this ClassList and releases any resources associated with it. After calling this method,
|
||||||
|
* the ClassList should not be used again.
|
||||||
|
*/
|
||||||
|
|
||||||
|
void close();
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) DuxTechnica
|
||||||
|
*
|
||||||
|
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||||
|
* contacting:
|
||||||
|
*
|
||||||
|
* Dux Technica, [email protected]
|
||||||
|
*
|
||||||
|
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
|
* either express or implied. See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dux.scanpath.v1.support;
|
||||||
|
|
||||||
|
import javassist.ClassPool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines a visitor that inspects one class file at a time for specific inheritance and annotation
|
||||||
|
* constructs. Subclasses should provide an implementation for the {@link #visit(String)} method.
|
||||||
|
*
|
||||||
|
* @author indroneel
|
||||||
|
*/
|
||||||
|
|
||||||
|
public abstract class ClassVisitor {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The shared {@link MetaStore} instance that is used to store associations discovered during
|
||||||
|
* the visitation of classes.
|
||||||
|
*/
|
||||||
|
|
||||||
|
protected MetaStore metaStore;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The shared Javassist {@link ClassPool} instance that is used to obtain the class definition
|
||||||
|
* for a fully qualified name.
|
||||||
|
*/
|
||||||
|
|
||||||
|
protected ClassPool clsPool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the common {@link MetaStore} for this visitor.
|
||||||
|
*
|
||||||
|
* @param store the {@link MetaStore} to be set
|
||||||
|
*/
|
||||||
|
|
||||||
|
public final void setMetaStore(MetaStore store) {
|
||||||
|
metaStore = store;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the shared Javassist {@link ClassPool} for this visitor. The classpool is used to obtain the
|
||||||
|
* class definition for a fully qualified name.
|
||||||
|
*
|
||||||
|
* @param cp the {@link ClassPool} to be set
|
||||||
|
*/
|
||||||
|
|
||||||
|
public final void setClassPool(ClassPool cp) {
|
||||||
|
clsPool = cp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visits a class given its fully qualified class name (FQCN). Subclasses should implement this
|
||||||
|
* method to define the specific behavior when a class is visited. This behavior is usually to
|
||||||
|
* update the shared metastore with associations for the given class.
|
||||||
|
*
|
||||||
|
* @param fqcn the fully qualified name of the class to visit
|
||||||
|
*/
|
||||||
|
|
||||||
|
public abstract void visit(String fqcn);
|
||||||
|
}
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) DuxTechnica
|
||||||
|
*
|
||||||
|
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||||
|
* contacting:
|
||||||
|
*
|
||||||
|
* Dux Technica, [email protected]
|
||||||
|
*
|
||||||
|
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
|
* either express or implied. See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dux.scanpath.v1.support;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A file in the local filesystem that contains the bytecodes of a Java class.
|
||||||
|
*
|
||||||
|
* @author indroneel
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class DiskClassFile implements ClassFile {
|
||||||
|
|
||||||
|
private final File root;
|
||||||
|
private final File file;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new instance of DiskClassFile.
|
||||||
|
*
|
||||||
|
* @param root the root directory of the classpath that contains this file.
|
||||||
|
* @param file the file that contains the bytecodes of a Java class.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public DiskClassFile(File root, File file) {
|
||||||
|
this.root = root;
|
||||||
|
this.file = file;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Methods of interface ClassFile
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
String filepath = file.getPath();
|
||||||
|
if (filepath.startsWith(root.getPath())) {
|
||||||
|
filepath = filepath.substring(root.getPath().length() + 1);
|
||||||
|
}
|
||||||
|
int endpos = filepath.lastIndexOf('.');
|
||||||
|
return filepath.substring(0, endpos).replace(File.separatorChar, '.');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSimpleName() {
|
||||||
|
String filepath = file.getName();
|
||||||
|
int endpos = filepath.lastIndexOf('.');
|
||||||
|
return filepath.substring(0, endpos);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream openInputStream() throws IOException {
|
||||||
|
return new FileInputStream(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) DuxTechnica
|
||||||
|
*
|
||||||
|
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||||
|
* contacting:
|
||||||
|
*
|
||||||
|
* Dux Technica, [email protected]
|
||||||
|
*
|
||||||
|
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
|
* either express or implied. See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dux.scanpath.v1.support;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileFilter;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A ClassList implementation that scans a directory on disk for .class files.
|
||||||
|
*
|
||||||
|
* @author indroneel
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class DiskClassList implements ClassList {
|
||||||
|
|
||||||
|
private final File root;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new DiskClassList that will scan the given directory for .class files into a
|
||||||
|
* collection.
|
||||||
|
*
|
||||||
|
* @param dir the directory to scan for .class files.
|
||||||
|
* @throws RuntimeException if the given directory is not a directory or cannot be read.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public DiskClassList(File dir) {
|
||||||
|
if (dir != null && (!dir.isDirectory() || !dir.canRead())) {
|
||||||
|
throw new RuntimeException("cannot use dir " + dir);
|
||||||
|
}
|
||||||
|
root = dir;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Methods of interface ClassList
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<ClassFile> getFiles() {
|
||||||
|
List<ClassFile> resList = Collections.emptyList();
|
||||||
|
if (root != null && root.exists()) {
|
||||||
|
Stack<File> stack = new Stack<File>();
|
||||||
|
stack.addAll(listFiles(root));
|
||||||
|
|
||||||
|
resList = new LinkedList<>();
|
||||||
|
while (!stack.isEmpty()) {
|
||||||
|
final File file = stack.pop();
|
||||||
|
if (file.isDirectory()) {
|
||||||
|
stack.addAll(listFiles(file));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
resList.add(new DiskClassFile(root, file));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
// NOOP
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Helper methods
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of files in the given directory, filtered to include only directories and .class
|
||||||
|
* files.
|
||||||
|
*
|
||||||
|
* @param file the directory to scan for files.
|
||||||
|
* @return a list of files in the given directory, filtered to include only directories and .class
|
||||||
|
* files.
|
||||||
|
*/
|
||||||
|
|
||||||
|
private List<File> listFiles(final File file) {
|
||||||
|
FileFilter filter = new FileFilter() {
|
||||||
|
@Override
|
||||||
|
public boolean accept(File pathname) {
|
||||||
|
if(pathname.isDirectory()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return pathname.getName().toLowerCase().endsWith(".class");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
File[] files = file.listFiles(filter);
|
||||||
|
|
||||||
|
if (files != null)
|
||||||
|
return Arrays.asList(files);
|
||||||
|
else
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) DuxTechnica
|
||||||
|
*
|
||||||
|
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||||
|
* contacting:
|
||||||
|
*
|
||||||
|
* Dux Technica, [email protected]
|
||||||
|
*
|
||||||
|
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
|
* either express or implied. See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dux.scanpath.v1.support;
|
||||||
|
|
||||||
|
import javassist.CtClass;
|
||||||
|
import javassist.CtField;
|
||||||
|
import javassist.Modifier;
|
||||||
|
import javassist.NotFoundException;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visits classes and records the annotations declared on their fields and superclass hierarchy.
|
||||||
|
*
|
||||||
|
* @author indroneel
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class FieldAnnotationVisitor extends ClassVisitor {
|
||||||
|
|
||||||
|
private static final Logger _L = Logger.getLogger(FieldAnnotationVisitor.class.getName());
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Methods of base class ClassVisitor
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(String className) {
|
||||||
|
try {
|
||||||
|
CtClass cls = clsPool.get(className);
|
||||||
|
visitImpl(cls);
|
||||||
|
}
|
||||||
|
catch (ClassNotFoundException | NotFoundException exep) {
|
||||||
|
if(_L.isLoggable(Level.WARNING)) {
|
||||||
|
_L.log(Level.WARNING, "unable to visit class %s".formatted(className), exep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Helper methods
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers mappings for annotations found on the supplied class's fields and inherited fields.
|
||||||
|
* Static, final, and variable-arguments fields are ignored.
|
||||||
|
*
|
||||||
|
* @param cls the class whose fields should be inspected
|
||||||
|
* @throws ClassNotFoundException if an annotation type cannot be resolved
|
||||||
|
* @throws NotFoundException if a superclass or field cannot be resolved
|
||||||
|
*/
|
||||||
|
|
||||||
|
private void visitImpl(CtClass cls)
|
||||||
|
throws ClassNotFoundException, NotFoundException {
|
||||||
|
CtField[] fields = collateFields(cls);
|
||||||
|
for (CtField field : fields) {
|
||||||
|
int mod = field.getModifiers();
|
||||||
|
if (Modifier.isStatic(mod) || Modifier.isFinal(mod) || Modifier.isVarArgs(mod)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Object[] annotations = field.getAnnotations();
|
||||||
|
if (annotations != null) {
|
||||||
|
for (Object obj : annotations) {
|
||||||
|
Annotation ann = (Annotation) obj;
|
||||||
|
metaStore.addClassWithFieldAnnotation(
|
||||||
|
ann.annotationType().getName(),
|
||||||
|
cls.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collects the fields declared by the supplied class and its superclass hierarchy. Fields declared
|
||||||
|
* by the supplied class precede those inherited from its superclasses.
|
||||||
|
*
|
||||||
|
* @param type the class whose fields should be collected
|
||||||
|
* @return an array containing the supplied class's fields and its inherited fields
|
||||||
|
* @throws NotFoundException if a superclass cannot be found
|
||||||
|
*/
|
||||||
|
|
||||||
|
private CtField[] collateFields(CtClass type) throws NotFoundException {
|
||||||
|
CtField[] fields = type.getDeclaredFields();
|
||||||
|
|
||||||
|
CtClass supcls = type.getSuperclass();
|
||||||
|
if(supcls != null) {
|
||||||
|
CtField[] supFields = collateFields(supcls);
|
||||||
|
if(supFields.length > 0) {
|
||||||
|
CtField[] retFields = new CtField[fields.length + supFields.length];
|
||||||
|
System.arraycopy(fields, 0, retFields, 0, fields.length);
|
||||||
|
System.arraycopy(supFields, 0, retFields, fields.length, supFields.length);
|
||||||
|
return retFields;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// for everything else ...
|
||||||
|
return fields;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) DuxTechnica
|
||||||
|
*
|
||||||
|
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||||
|
* contacting:
|
||||||
|
*
|
||||||
|
* Dux Technica, [email protected]
|
||||||
|
*
|
||||||
|
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
|
* either express or implied. See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dux.scanpath.v1.support;
|
||||||
|
|
||||||
|
import javassist.CtClass;
|
||||||
|
import javassist.NotFoundException;
|
||||||
|
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visits classes and records the inheritance relationships between classes, interfaces, and
|
||||||
|
* superclasses.
|
||||||
|
*
|
||||||
|
* @author indroneel
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class InheritanceVisitor extends ClassVisitor {
|
||||||
|
|
||||||
|
private static final Logger _L = Logger.getLogger(InheritanceVisitor.class.getName());
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Methods of base class ClassVisitor
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(String className) {
|
||||||
|
try {
|
||||||
|
CtClass cls = clsPool.get(className);
|
||||||
|
visitImpl(className, cls);
|
||||||
|
}
|
||||||
|
catch (NotFoundException | RuntimeException exep) {
|
||||||
|
if(_L.isLoggable(Level.WARNING)) {
|
||||||
|
_L.log(Level.WARNING, String.format("unable to visit class %s", className), exep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Helper methods
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers mappings for inheritance relationships found on the supplied class's superclasses and
|
||||||
|
* interfaces.
|
||||||
|
*
|
||||||
|
* @param startClsName the name of the class being visited
|
||||||
|
* @param cls the class being visited
|
||||||
|
* @throws NotFoundException if a superclass or interface cannot be found
|
||||||
|
*/
|
||||||
|
|
||||||
|
private void visitImpl(String startClsName, CtClass cls) throws NotFoundException {
|
||||||
|
if(_L.isLoggable(Level.FINE)) {
|
||||||
|
_L.fine("visiting class " + startClsName);
|
||||||
|
}
|
||||||
|
CtClass supCls = cls.getSuperclass();
|
||||||
|
if (supCls != null && !supCls.getName().equals("java.lang.Object")) {
|
||||||
|
metaStore.addInheritance(supCls.getName(), startClsName);
|
||||||
|
visitImpl(startClsName, supCls);
|
||||||
|
}
|
||||||
|
|
||||||
|
CtClass[] ifaces = cls.getInterfaces();
|
||||||
|
for (CtClass iface : ifaces) {
|
||||||
|
visitInterfaces(startClsName, iface);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers mappings for inheritance relationships found on the supplied interface's superinterfaces.
|
||||||
|
* @param startClsName the name of the class being visited
|
||||||
|
* @param iface the interface being visited
|
||||||
|
* @throws NotFoundException if a superinterface is declared but the class cannot be found
|
||||||
|
*/
|
||||||
|
|
||||||
|
private void visitInterfaces(String startClsName, CtClass iface) throws NotFoundException {
|
||||||
|
metaStore.addInheritance(iface.getName(), startClsName);
|
||||||
|
CtClass[] ifaces = iface.getInterfaces();
|
||||||
|
for (CtClass niface : ifaces) {
|
||||||
|
visitInterfaces(startClsName, niface);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) DuxTechnica
|
||||||
|
*
|
||||||
|
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||||
|
* contacting:
|
||||||
|
*
|
||||||
|
* Dux Technica, [email protected]
|
||||||
|
*
|
||||||
|
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
|
* either express or implied. See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dux.scanpath.v1.support;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.jar.JarEntry;
|
||||||
|
import java.util.jar.JarFile;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An entry within a JAR file that can be used to read the bytecodes of a Java class.
|
||||||
|
*
|
||||||
|
* @author indroneel
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class JarClassFile implements ClassFile {
|
||||||
|
|
||||||
|
private final JarFile root;
|
||||||
|
private final JarEntry entry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new JAR class file entry.
|
||||||
|
*
|
||||||
|
* @param root the JAR file that contains the class file entry
|
||||||
|
* @param entry the JAR entry that represents the class file
|
||||||
|
*/
|
||||||
|
|
||||||
|
public JarClassFile(JarFile root, JarEntry entry) {
|
||||||
|
this.root = root;
|
||||||
|
this.entry = entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Methods of interface ClassFile
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
String name = entry.getName();
|
||||||
|
if(name.startsWith("/")) {
|
||||||
|
name = name.substring(1, name.lastIndexOf('.')).replace('/', '.');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
name = name.substring(0, name.lastIndexOf('.')).replace('/', '.');
|
||||||
|
}
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSimpleName() {
|
||||||
|
String fqcn = getName();
|
||||||
|
return fqcn.substring(fqcn.lastIndexOf('.') + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream openInputStream() throws IOException {
|
||||||
|
return root.getInputStream(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return root.getName() + "!" + java.io.File.separatorChar + entry.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) DuxTechnica
|
||||||
|
*
|
||||||
|
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||||
|
* contacting:
|
||||||
|
*
|
||||||
|
* Dux Technica, [email protected]
|
||||||
|
*
|
||||||
|
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
|
* either express or implied. See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dux.scanpath.v1.support;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.jar.JarEntry;
|
||||||
|
import java.util.jar.JarFile;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A collection of Java class files contained within a given JAR file.
|
||||||
|
*
|
||||||
|
* @author indroneel
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class JarClassList implements ClassList {
|
||||||
|
|
||||||
|
private static final Logger _L = Logger.getLogger(JarClassList.class.getName());
|
||||||
|
|
||||||
|
private final JarFile jarFile;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new JarClassList that will read class files from the specified JAR file.
|
||||||
|
*
|
||||||
|
* @param file the JAR file to read class files from
|
||||||
|
*/
|
||||||
|
|
||||||
|
public JarClassList(JarFile file) {
|
||||||
|
jarFile = file;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Methods of interface ClassArchive
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<ClassFile> getFiles() {
|
||||||
|
LinkedList<ClassFile> ll = new LinkedList<>();
|
||||||
|
Enumeration<? extends JarEntry> entries = jarFile.entries();
|
||||||
|
while (entries.hasMoreElements()) {
|
||||||
|
JarEntry entry = entries.nextElement();
|
||||||
|
if (!entry.isDirectory()) {
|
||||||
|
String entryName = entry.getName().toLowerCase();
|
||||||
|
if(entryName.endsWith(".class")) {
|
||||||
|
ll.add(new JarClassFile(jarFile, entry));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ll;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
try {
|
||||||
|
jarFile.close();
|
||||||
|
}
|
||||||
|
catch (IOException exep) {
|
||||||
|
if(_L.isLoggable(Level.WARNING)) {
|
||||||
|
_L.log(Level.WARNING, "unable to release jar file: " + jarFile.getName(), exep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return jarFile.getName();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,274 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) DuxTechnica
|
||||||
|
*
|
||||||
|
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||||
|
* contacting:
|
||||||
|
*
|
||||||
|
* Dux Technica, [email protected]
|
||||||
|
*
|
||||||
|
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
|
* either express or implied. See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dux.scanpath.v1.support;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores metadata about classes discovered on the classpath, including inheritance relationships
|
||||||
|
* and annotations on classes, fields, and methods.
|
||||||
|
*
|
||||||
|
* @author indroneel
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class MetaStore {
|
||||||
|
|
||||||
|
private static final Logger _L = Logger.getLogger(MetaStore.class.getName());
|
||||||
|
|
||||||
|
private ClassLoader cload;
|
||||||
|
private final Map<String, Set<String>> inheritList;
|
||||||
|
private final Map<String, Set<String>> annList;
|
||||||
|
private final Map<String, Set<String>> fldAnnList;
|
||||||
|
private final Map<String, Set<String>> mthdAnnList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new MetaStore instance that will use the current thread's context class loader to load
|
||||||
|
* classes.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public MetaStore() {
|
||||||
|
cload = Thread.currentThread().getContextClassLoader();
|
||||||
|
if(cload == null) {
|
||||||
|
cload = getClass().getClassLoader();
|
||||||
|
}
|
||||||
|
|
||||||
|
inheritList = new HashMap<>();
|
||||||
|
annList = new HashMap<>();
|
||||||
|
fldAnnList = new HashMap<>();
|
||||||
|
mthdAnnList = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the class loader to use when loading classes from the metadata store. If the provided class
|
||||||
|
* loader is null, the current thread's context class loader will be used instead.
|
||||||
|
*
|
||||||
|
* @param cl the class loader to use for loading classes, or null to use the current thread's
|
||||||
|
* context class loader
|
||||||
|
*/
|
||||||
|
|
||||||
|
public void setClassLoader(ClassLoader cl) {
|
||||||
|
cload = cl;
|
||||||
|
if(cload == null) {
|
||||||
|
cload = Thread.currentThread().getContextClassLoader();
|
||||||
|
}
|
||||||
|
if(cload == null) {
|
||||||
|
cload = getClass().getClassLoader();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an inheritance relationship between a base class and a derived class to the metadata store.
|
||||||
|
*
|
||||||
|
* @param baseName the fully qualified name of the base class
|
||||||
|
* @param clsName the fully qualified name of the derived class
|
||||||
|
*/
|
||||||
|
|
||||||
|
public void addInheritance(String baseName, String clsName) {
|
||||||
|
if(inheritList.containsKey(baseName)) {
|
||||||
|
inheritList.get(baseName).add(clsName);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
HashSet<String> set = new HashSet<>();
|
||||||
|
set.add(clsName);
|
||||||
|
inheritList.put(baseName, set);
|
||||||
|
}
|
||||||
|
if(_L.isLoggable(Level.FINE)) {
|
||||||
|
_L.fine("parent = %s, inherited_by = %s".formatted(baseName, clsName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a class with a specific annotation to the metadata store. The annotation is applied on the
|
||||||
|
* class itself, or any of its parent class/interface.
|
||||||
|
*
|
||||||
|
* @param ann the fully qualified name of the annotation
|
||||||
|
* @param clsName the fully qualified name of the class that has the annotation
|
||||||
|
*/
|
||||||
|
|
||||||
|
public void addClassWithAnnotation(String ann, String clsName) {
|
||||||
|
if(annList.containsKey(ann)) {
|
||||||
|
annList.get(ann).add(clsName);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
HashSet<String> set = new HashSet<>();
|
||||||
|
set.add(clsName);
|
||||||
|
annList.put(ann, set);
|
||||||
|
}
|
||||||
|
if(_L.isLoggable(Level.FINE)) {
|
||||||
|
_L.fine(String.format("annotation = %s, present_in = %s", ann, clsName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a class with a specific field annotation to the metadata store. The annotation is applied on
|
||||||
|
* a field of the class.
|
||||||
|
*
|
||||||
|
* @param ann the fully qualified name of the field annotation
|
||||||
|
* @param clsName the fully qualified name of the class that has the field annotation
|
||||||
|
*/
|
||||||
|
|
||||||
|
public void addClassWithFieldAnnotation(String ann, String clsName) {
|
||||||
|
if(fldAnnList.containsKey(ann)) {
|
||||||
|
fldAnnList.get(ann).add(clsName);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
HashSet<String> set = new HashSet<>();
|
||||||
|
set.add(clsName);
|
||||||
|
fldAnnList.put(ann, set);
|
||||||
|
}
|
||||||
|
if(_L.isLoggable(Level.FINE)) {
|
||||||
|
_L.fine(String.format("field_annotation = %s, present_in = %s", ann, clsName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a class with a specific method annotation to the metadata store. The annotation is applied
|
||||||
|
* on a method of the class.
|
||||||
|
*
|
||||||
|
* @param ann the fully qualified name of the method annotation
|
||||||
|
* @param clsName the fully qualified name of the class that has the method annotation
|
||||||
|
*/
|
||||||
|
|
||||||
|
public void addClassWithMethodAnnotation(String ann, String clsName) {
|
||||||
|
if(mthdAnnList.containsKey(ann)) {
|
||||||
|
mthdAnnList.get(ann).add(clsName);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
HashSet<String> set = new HashSet<>();
|
||||||
|
set.add(clsName);
|
||||||
|
mthdAnnList.put(ann, set);
|
||||||
|
}
|
||||||
|
if(_L.isLoggable(Level.FINE)) {
|
||||||
|
_L.fine(String.format("method_annotation = %s, present_in = %s", ann, clsName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a list of Java types that inherit from or implement the specified base type.
|
||||||
|
*
|
||||||
|
* @param baseName the fully qualified name of the base type
|
||||||
|
* @return a list of Java types that inherit from or implement the specified base type
|
||||||
|
*/
|
||||||
|
|
||||||
|
public List<Class<?>> listInheritingClasses(String baseName) {
|
||||||
|
List<Class<?>> result = new ArrayList<>();
|
||||||
|
Set<String> clsNames = inheritList.get(baseName);
|
||||||
|
if(clsNames == null) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
for(String name : clsNames) {
|
||||||
|
try {
|
||||||
|
result.add(cload.loadClass(name));
|
||||||
|
}
|
||||||
|
catch (ClassNotFoundException exep) {
|
||||||
|
if(_L.isLoggable(Level.WARNING)) {
|
||||||
|
_L.log(Level.WARNING, String.format("unable to load class %s", name), exep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a list of Java types that are annotated with the specified annotation. Alternatively,
|
||||||
|
* the list may also include types that inherit from or implement at least one Java type that is
|
||||||
|
* annotated with the specified annotation.
|
||||||
|
*
|
||||||
|
* @param ann the fully qualified name of the annotation
|
||||||
|
* @return a list of annotated Java types
|
||||||
|
*/
|
||||||
|
|
||||||
|
public List<Class<?>> listAnnotatedClasses(String ann) {
|
||||||
|
List<Class<?>> result = new ArrayList<>();
|
||||||
|
Set<String> clsNames = annList.get(ann);
|
||||||
|
if(clsNames == null) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
for(String name : clsNames) {
|
||||||
|
try {
|
||||||
|
result.add(cload.loadClass(name));
|
||||||
|
}
|
||||||
|
catch (ClassNotFoundException exep) {
|
||||||
|
if(_L.isLoggable(Level.WARNING)) {
|
||||||
|
_L.log(Level.WARNING, String.format("unable to load class %s", name), exep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a list of Java types that have at least one field annotated with the specified
|
||||||
|
* annotation. Alternatively, the list may also include types that inherit from or implement at
|
||||||
|
* least one Java type that has at least one field annotated with the specified annotation.
|
||||||
|
*
|
||||||
|
* @param ann the fully qualified name of the field annotation
|
||||||
|
* @return a list of Java types with annotated fields
|
||||||
|
*/
|
||||||
|
|
||||||
|
public List<Class<?>> listClassesWithAnnotatedFields(String ann) {
|
||||||
|
List<Class<?>> result = new ArrayList<>();
|
||||||
|
Set<String> clsNames = fldAnnList.get(ann);
|
||||||
|
if(clsNames == null) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
for(String name : clsNames) {
|
||||||
|
try {
|
||||||
|
result.add(cload.loadClass(name));
|
||||||
|
}
|
||||||
|
catch (ClassNotFoundException exep) {
|
||||||
|
if(_L.isLoggable(Level.WARNING)) {
|
||||||
|
_L.log(Level.WARNING, String.format("unable to load class %s", name), exep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a list of Java types that have at least one method annotated with the specified
|
||||||
|
* annotation. Alternatively, the list may also include types that inherit from or implement at
|
||||||
|
* least one Java type that has at least one method annotated with the specified annotation.
|
||||||
|
*
|
||||||
|
* @param ann the fully qualified name of the method annotation
|
||||||
|
* @return a list of Java types with annotated methods
|
||||||
|
*/
|
||||||
|
|
||||||
|
public List<Class<?>> listClassesWithAnnotatedMethods(String ann) {
|
||||||
|
List<Class<?>> result = new ArrayList<>();
|
||||||
|
Set<String> clsNames = mthdAnnList.get(ann);
|
||||||
|
if(clsNames == null) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
for(String name : clsNames) {
|
||||||
|
try {
|
||||||
|
result.add(cload.loadClass(name));
|
||||||
|
}
|
||||||
|
catch (ClassNotFoundException exep) {
|
||||||
|
if(_L.isLoggable(Level.WARNING)) {
|
||||||
|
_L.log(Level.WARNING, String.format("unable to load class %s", name), exep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) DuxTechnica
|
||||||
|
*
|
||||||
|
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||||
|
* contacting:
|
||||||
|
*
|
||||||
|
* Dux Technica, [email protected]
|
||||||
|
*
|
||||||
|
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
|
* either express or implied. See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dux.scanpath.v1.support;
|
||||||
|
|
||||||
|
import javassist.CtClass;
|
||||||
|
import javassist.CtMethod;
|
||||||
|
import javassist.Modifier;
|
||||||
|
import javassist.NotFoundException;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visits all methods of a class and records the annotations on each method.
|
||||||
|
*
|
||||||
|
* @author indroneel
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class MethodAnnotationVisitor extends ClassVisitor {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = Logger.getLogger(MethodAnnotationVisitor.class.getName());
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Methods of base class ClassVisitor
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(String className) {
|
||||||
|
try {
|
||||||
|
CtClass cls = clsPool.get(className);
|
||||||
|
visitImpl(cls);
|
||||||
|
}
|
||||||
|
catch (ClassNotFoundException | NotFoundException exep) {
|
||||||
|
LOGGER.log(Level.WARNING, String.format("unable to visit class %s", className), exep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Helper methods
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visits the given class and records the annotations on each method.
|
||||||
|
*
|
||||||
|
* @param cls the class to visit
|
||||||
|
* @throws ClassNotFoundException if the class cannot be found
|
||||||
|
* @throws NotFoundException if a superclass cannot be found
|
||||||
|
*/
|
||||||
|
|
||||||
|
private void visitImpl(CtClass cls) throws ClassNotFoundException, NotFoundException {
|
||||||
|
CtMethod[] methods = collateMethods(cls);
|
||||||
|
for(CtMethod method : methods) {
|
||||||
|
int mod = method.getModifiers();
|
||||||
|
if(Modifier.isStatic(mod) || Modifier.isAbstract(mod)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Object[] annotations = method.getAnnotations();
|
||||||
|
if(annotations != null) {
|
||||||
|
for(Object obj : annotations) {
|
||||||
|
Annotation ann = (Annotation) obj;
|
||||||
|
metaStore.addClassWithMethodAnnotation(
|
||||||
|
ann.annotationType().getName(),
|
||||||
|
cls.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collates all methods of the given class and its superclasses, excluding methods from
|
||||||
|
* java.lang.Object.
|
||||||
|
*
|
||||||
|
* @param cls the class to collate methods from
|
||||||
|
* @return an array of CtMethod objects representing all methods of the class and its superclasses
|
||||||
|
* @throws NotFoundException if a superclass cannot be found
|
||||||
|
*/
|
||||||
|
|
||||||
|
private CtMethod[] collateMethods(CtClass cls) throws NotFoundException {
|
||||||
|
CtMethod[] methods = cls.getDeclaredMethods();
|
||||||
|
CtClass supcls = cls.getSuperclass();
|
||||||
|
if(supcls != null && !supcls.getName().equals("java.lang.Object")) {
|
||||||
|
CtMethod[] supMethods = collateMethods(supcls);
|
||||||
|
if(supMethods.length > 0) {
|
||||||
|
CtMethod[] retMethods = new CtMethod[methods.length + supMethods.length];
|
||||||
|
System.arraycopy(methods, 0, retMethods, 0, methods.length);
|
||||||
|
System.arraycopy(supMethods, 0, retMethods, methods.length, supMethods.length);
|
||||||
|
return retMethods;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// for everything else ...
|
||||||
|
return methods;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) DuxTechnica
|
||||||
|
*
|
||||||
|
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||||
|
* contacting:
|
||||||
|
*
|
||||||
|
* Dux Technica, [email protected]
|
||||||
|
*
|
||||||
|
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
|
* either express or implied. See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dux.scanpath.v1.support;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipFile;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An entry within a ZIP file that can be used to read the bytecodes of a Java class.
|
||||||
|
*
|
||||||
|
* @author indroneel
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class ZipClassFile implements ClassFile {
|
||||||
|
|
||||||
|
private final ZipFile root;
|
||||||
|
private final ZipEntry entry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new ZipClassFile instance.
|
||||||
|
*
|
||||||
|
* @param root the ZIP file that contains the class file
|
||||||
|
* @param entry the ZIP entry that represents the class file
|
||||||
|
*/
|
||||||
|
|
||||||
|
public ZipClassFile(ZipFile root, ZipEntry entry) {
|
||||||
|
this.root = root;
|
||||||
|
this.entry = entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Methods of interface ClassFile
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
String name = entry.getName();
|
||||||
|
if(name.startsWith("/")) {
|
||||||
|
name = name.substring(1, name.lastIndexOf('.')).replace('/', '.');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
name = name.substring(0, name.lastIndexOf('.')).replace('/', '.');
|
||||||
|
}
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSimpleName() {
|
||||||
|
String fqcn = getName();
|
||||||
|
return fqcn.substring(fqcn.lastIndexOf('.') + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream openInputStream() throws IOException {
|
||||||
|
return root.getInputStream(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return root.getName() + "!" + java.io.File.separatorChar + entry.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) DuxTechnica
|
||||||
|
*
|
||||||
|
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||||
|
* contacting:
|
||||||
|
*
|
||||||
|
* Dux Technica, [email protected]
|
||||||
|
*
|
||||||
|
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
|
* either express or implied. See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dux.scanpath.v1.support;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipFile;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A collection of Java class files contained within a ZIP file.
|
||||||
|
*
|
||||||
|
* @author indroneel
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class ZipClassList implements ClassList {
|
||||||
|
|
||||||
|
private static final Logger _L = Logger.getLogger(ZipClassList.class.getName());
|
||||||
|
|
||||||
|
private final ZipFile zipFile;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new ZipClassList instance.
|
||||||
|
*
|
||||||
|
* @param file the ZIP file that contains the class files
|
||||||
|
*/
|
||||||
|
|
||||||
|
public ZipClassList(ZipFile file) {
|
||||||
|
zipFile = file;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Methods of interface ClassArchive
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<ClassFile> getFiles() {
|
||||||
|
LinkedList<ClassFile> ll = new LinkedList<>();
|
||||||
|
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
||||||
|
while (entries.hasMoreElements()) {
|
||||||
|
ZipEntry entry = entries.nextElement();
|
||||||
|
if (!entry.isDirectory()) {
|
||||||
|
String entryName = entry.getName().toLowerCase();
|
||||||
|
if(entryName.endsWith(".class")) {
|
||||||
|
ll.add(new ZipClassFile(zipFile, entry));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ll;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
try {
|
||||||
|
zipFile.close();
|
||||||
|
}
|
||||||
|
catch (IOException exep) {
|
||||||
|
if(_L.isLoggable(Level.WARNING)) {
|
||||||
|
_L.log(Level.WARNING, "unable to release zip file: " + zipFile.getName(), exep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return zipFile.getName();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user