Skip to content
This repository has been archived by the owner on Sep 17, 2020. It is now read-only.

Commit

Permalink
Merge pull request #20 from volkovs/custom-detectors-demo
Browse files Browse the repository at this point in the history
Custom detectors support + demo
  • Loading branch information
amaembo committed Sep 19, 2016
2 parents 7648117 + c07254a commit 472aa63
Show file tree
Hide file tree
Showing 18 changed files with 628 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,6 @@
*/
package one.util.huntbugs.registry;

import java.io.PrintStream;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.strobel.assembler.ir.Instruction;
import com.strobel.assembler.ir.OpCode;
import com.strobel.assembler.metadata.MetadataSystem;
Expand All @@ -46,7 +30,6 @@
import com.strobel.decompiler.ast.Block;
import com.strobel.decompiler.ast.Lambda;
import com.strobel.decompiler.ast.Node;

import one.util.huntbugs.analysis.Context;
import one.util.huntbugs.analysis.ErrorMessage;
import one.util.huntbugs.db.FieldStats;
Expand All @@ -57,12 +40,30 @@
import one.util.huntbugs.registry.anno.WarningDefinition;
import one.util.huntbugs.repo.Repository;
import one.util.huntbugs.repo.RepositoryVisitor;
import one.util.huntbugs.spi.HuntBugsPlugin;
import one.util.huntbugs.util.NodeChain;
import one.util.huntbugs.util.Nodes;
import one.util.huntbugs.warning.Messages.Message;
import one.util.huntbugs.warning.Role.NumberRole;
import one.util.huntbugs.warning.WarningType;

import java.io.PrintStream;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* @author Tagir Valeev
*
Expand Down Expand Up @@ -102,9 +103,8 @@ public DetectorRegistry(Context ctx) {
}

private Map<String, WarningType> createWarningMap(Stream<WarningType> stream) {
Map<String, WarningType> systemWarnings = stream.map(ctx.getOptions().getRule()::adjust).collect(
return stream.map(ctx.getOptions().getRule()::adjust).collect(
Collectors.toMap(WarningType::getName, Function.identity()));
return systemWarnings;
}

private List<WarningDefinition> getDefinitions(Class<?> clazz) {
Expand Down Expand Up @@ -145,26 +145,19 @@ private Detector createDetector(Class<?> clazz, Map<String, WarningType> wts) th
}

void init() {
Repository repo = Repository.createSelfRepository();

// adding HuntBugs built-in detectors
Repository selfRepo = Repository.createSelfRepository();
String pkg = DETECTORS_PACKAGE.replace('.', '/');
repo.visit(pkg, new RepositoryVisitor() {
@Override
public boolean visitPackage(String packageName) {
return packageName.equals(pkg);
}
selfRepo.visit(pkg, new DetectorVisitor(pkg, false));

// adding HuntBugs 3-rd party detectors if any
for (HuntBugsPlugin huntBugsPlugin : ServiceLoader.load(HuntBugsPlugin.class)) {
Repository pluginRepository = Repository.createPluginRepository(huntBugsPlugin);
String pluginDetectorPackage = huntBugsPlugin.detectorPackage().replace('.', '/');
pluginRepository.visit(pluginDetectorPackage, new DetectorVisitor(pluginDetectorPackage, true));
}

@Override
public void visitClass(String className) {
String name = className.replace('/', '.');
try {
ctx.incStat("Detectors.Total");
if (addDetector(MetadataSystem.class.getClassLoader().loadClass(name)))
ctx.incStat("Detectors");
} catch (ClassNotFoundException e) {
ctx.addError(new ErrorMessage(name, null, null, null, -1, e));
}
}
});
}

private void visitChildren(Node node, NodeChain parents, List<MethodContext> list, MethodData mdata) {
Expand Down Expand Up @@ -310,7 +303,7 @@ private void sortConstructors(List<MethodDefinition> ctors) {
if(body != null) {
for(Instruction instr : body.getInstructions()) {
if(instr.getOpCode() == OpCode.INVOKESPECIAL) {
MethodReference mr = (MethodReference)instr.getOperand(0);
MethodReference mr = instr.getOperand(0);
if(mr.getDeclaringType().isEquivalentTo(ctor.getDeclaringType()) && mr.isConstructor()) {
deps.put(ctor, mr.resolve());
}
Expand Down Expand Up @@ -357,9 +350,9 @@ public void reportWarningTypes(PrintStream out) {
List<String> result = new ArrayList<>();

String arrow = " --> ";
typeToDetector.forEach((wt, detector) -> {
result.add(wt.getCategory() + arrow + wt.getName() + arrow + detector);
});
typeToDetector.forEach((wt, detector) ->
result.add(wt.getCategory() + arrow + wt.getName() + arrow + detector)
);
printTree(out, result, arrow);
out.println("Total types: " + typeToDetector.size());
}
Expand Down Expand Up @@ -400,4 +393,37 @@ public WarningType getWarningType(String typeName) {
public Stream<WarningType> warningTypes() {
return typeToDetector.keySet().stream();
}

private class DetectorVisitor implements RepositoryVisitor {

private String packageToVisit;

private boolean external;

DetectorVisitor(String packageToVisit, boolean external) {
this.packageToVisit = packageToVisit;
this.external = external;
}

@Override
public boolean visitPackage(String packageName) {
return packageName.equals(packageToVisit);
}

@Override
public void visitClass(String className) {
String name = className.replace('/', '.');
try {
ctx.incStat("Detectors.Total");
if (addDetector(MetadataSystem.class.getClassLoader().loadClass(name))) {
ctx.incStat("Detectors");
if (external) {
ctx.incStat("Detectors from HuntBugs plugins");
}
}
} catch (ClassNotFoundException e) {
ctx.addError(new ErrorMessage(name, null, null, null, -1, e));
}
}
}
}
60 changes: 40 additions & 20 deletions huntbugs/src/main/java/one/util/huntbugs/repo/Repository.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package one.util.huntbugs.repo;

import com.strobel.assembler.metadata.ITypeLoader;
import one.util.huntbugs.spi.HuntBugsPlugin;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
Expand All @@ -30,7 +33,7 @@
import java.util.Set;
import java.util.jar.JarFile;

import com.strobel.assembler.metadata.ITypeLoader;
import static java.lang.String.format;

/**
* @author Tagir Valeev
Expand All @@ -41,7 +44,7 @@ public interface Repository {

void visit(String rootPackage, RepositoryVisitor visitor);

public static Repository createSelfRepository() {
static Repository createSelfRepository() {
List<Repository> repos = new ArrayList<>();
Set<Path> paths = new HashSet<>();
try {
Expand All @@ -59,33 +62,50 @@ public static Repository createSelfRepository() {
} catch (IOException e) {
throw new RuntimeException(e);
}
CodeSource codeSource = CompositeRepository.class.getProtectionDomain().getCodeSource();
URL url = codeSource == null ? null : codeSource.getLocation();
if(url != null) {
try {
Path path = Paths.get(url.toURI());
if(paths.add(path)) {
if(Files.isDirectory(path))
repos.add(new DirRepository(path));
else if(Files.isRegularFile(path))
repos.add(new JarRepository(new JarFile(path.toFile())));

repos.add(createDetectorsRepo(CompositeRepository.class, "HuntBugs Detectors", paths));

return new CompositeRepository(repos);
}

static Repository createPluginRepository(HuntBugsPlugin huntBugsPlugin) {
Class<?> pluginClass = huntBugsPlugin.getClass();
String pluginName = huntBugsPlugin.name();
return createDetectorsRepo(pluginClass, pluginName, new HashSet<>());
}

static Repository createDetectorsRepo(Class<?> clazz, String pluginName, Set<Path> paths) {
CodeSource codeSource = clazz.getProtectionDomain().getCodeSource();
if (codeSource == null) {
throw new RuntimeException(format("Initializing plugin '%s' could not get code source for class %s", pluginName, clazz.getName()));
}

URL url = codeSource.getLocation();
try {
Path path = Paths.get(url.toURI());
if(paths.add(path)) {
if(Files.isDirectory(path)) {
return new DirRepository(path);
} else {
return new JarRepository(new JarFile(path.toFile()));
}
} catch (URISyntaxException | FileSystemNotFoundException | IllegalArgumentException
| IOException | UnsupportedOperationException e) {
// ignore
} else {
return createNullRepository();
}
} catch (URISyntaxException | FileSystemNotFoundException | IllegalArgumentException
| IOException | UnsupportedOperationException e) {
String errorMessage = format("Error creating detector repository for plugin '%s'", pluginName);
throw new RuntimeException(errorMessage, e);
}
CompositeRepository repo = new CompositeRepository(repos);
return repo;
}
public static Repository createNullRepository() {

static Repository createNullRepository() {
return new Repository() {
@Override
public void visit(String rootPackage, RepositoryVisitor visitor) {
// nothing to do
}

@Override
public ITypeLoader createTypeLoader() {
return (internalName, buffer) -> false;
Expand Down
78 changes: 78 additions & 0 deletions huntbugs/src/main/java/one/util/huntbugs/spi/DataTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2016 HuntBugs contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 one.util.huntbugs.spi;

import one.util.huntbugs.analysis.AnalysisOptions;
import one.util.huntbugs.analysis.Context;
import one.util.huntbugs.analysis.ErrorMessage;
import one.util.huntbugs.analysis.HuntBugsResult;
import one.util.huntbugs.input.XmlReportReader;
import one.util.huntbugs.output.Reports;
import one.util.huntbugs.repo.CompositeRepository;
import one.util.huntbugs.repo.Repository;

import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ServiceLoader;
import java.util.stream.Collectors;

import static java.lang.String.format;

/**
* @author Tagir Valeev
*
*/
public abstract class DataTests {

public static void test(String packageToAnalyze) throws Exception {

// creating built-in and plugins repositories
List<Repository> repositories = new ArrayList<>();
repositories.add(Repository.createSelfRepository());
for (HuntBugsPlugin huntBugsPlugin : ServiceLoader.load(HuntBugsPlugin.class)) {
repositories.add(Repository.createPluginRepository(huntBugsPlugin));
}
CompositeRepository repository = new CompositeRepository(repositories);

Context ctx = new Context(repository, new AnalysisOptions());
ctx.analyzePackage(packageToAnalyze);
ctx.reportStats(System.out);
ctx.reportErrors(System.err);
ctx.reportWarnings(new PrintStream("target/testWarnings.out"));
Path xmlReport = Paths.get("target/testWarnings.xml");
Reports.write(xmlReport, Paths.get("target/testWarnings.html"), ctx);
System.out.println("Analyzed " + ctx.getClassesCount() + " classes");
if (ctx.getErrorCount() > 0) {
List<ErrorMessage> errorMessages = ctx.errors().collect(Collectors.toList());
throw new AssertionError(format("Analysis finished with %s errors: %s", ctx.getErrorCount(), errorMessages));
}
HuntBugsResult result = XmlReportReader.read(ctx, xmlReport);
Path rereadReport = Paths.get("target/testWarnings_reread.xml");
Reports.write(rereadReport, null, result);
byte[] expectedReport = Files.readAllBytes(xmlReport);
byte[] actualReport = Files.readAllBytes(rereadReport);
if (!Arrays.equals(expectedReport, actualReport)) {
String errorMessage = format("Expected: \n%s\n\nActual: \n%s\n\n", new String(expectedReport), new String(actualReport));
throw new AssertionError(errorMessage);
}
}

}
30 changes: 30 additions & 0 deletions huntbugs/src/main/java/one/util/huntbugs/spi/HuntBugsPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2016 HuntBugs contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 one.util.huntbugs.spi;

/**
* This is extension point for 3-rd party detector providers.
*
* @author Mihails Volkovs
*
*/
public interface HuntBugsPlugin {

String name();

String detectorPackage();

}
Loading

0 comments on commit 472aa63

Please sign in to comment.