Search in sources :

Example 6 with AnalysisResult

use of org.revapi.AnalysisResult in project revapi by revapi.

the class Main method run.

@SuppressWarnings("ConstantConditions")
private static void run(File cacheDir, String[] extensionGAVs, List<FileArchive> oldArchives, List<FileArchive> oldSupplementaryArchives, List<FileArchive> newArchives, List<FileArchive> newSupplementaryArchives, String[] configFiles, Map<String, String> additionalConfig) throws Exception {
    ProjectModule.Builder bld = ProjectModule.build();
    bld.localRepository(cacheDir);
    if (extensionGAVs != null) {
        for (String gav : extensionGAVs) {
            bld.addDependency(gav);
        }
    }
    Properties libraryVersionsProps = new Properties();
    libraryVersionsProps.load(Main.class.getResourceAsStream("/library.versions"));
    Set<Artifact> globalArtifacts = libraryVersionsProps.stringPropertyNames().stream().map(p -> {
        String gav = p + ':' + libraryVersionsProps.get(p);
        return new DefaultArtifact(gav);
    }).collect(toSet());
    bld.moduleSpecController(new ModuleSpecController() {

        private boolean override;

        private String currentModuleName;

        @Override
        public void start(String moduleName) {
            currentModuleName = moduleName;
        }

        // TODO add warnings when the deps depend on another revapi version than the one bundled...
        @Override
        public DependencySpec modifyDependency(String dependencyName, DependencySpec original) {
            boolean overrideThis = false;
            Artifact a = new DefaultArtifact(dependencyName);
            for (Artifact ga : globalArtifacts) {
                if (ga.getGroupId().equals(a.getGroupId()) && ga.getArtifactId().equals(a.getArtifactId()) && !ga.getVersion().equals(a.getVersion())) {
                    LOG.warn("Detected version conflict in dependencies of extension " + currentModuleName + ". The extension depends on " + a + " while the CLI has " + ga + " on global" + " classpath. This will likely cause problems.");
                }
            }
            override = override || overrideThis;
            return overrideThis ? null : original;
        }

        @Override
        public void modify(ModuleSpec.Builder bld) {
            if (override) {
                Set<String> revapiPaths = new HashSet<>(Arrays.asList("org/revapi", "org/revapi/configuration", "org/revapi/query", "org/revapi/simple"));
                bld.addDependency(DependencySpec.createSystemDependencySpec(revapiPaths));
                override = false;
            }
        }

        @Override
        public void end(String moduleName) {
            currentModuleName = null;
        }
    });
    LOG.info("Downloading extensions");
    Module project = bld.create();
    Revapi revapi = Revapi.builder().withAllExtensionsFrom(project.getClassLoader()).withAllExtensionsFromThreadContextClassLoader().build();
    AnalysisContext.Builder ctxBld = AnalysisContext.builder(revapi).withOldAPI(API.of(oldArchives).supportedBy(oldSupplementaryArchives).build()).withNewAPI(API.of(newArchives).supportedBy(newSupplementaryArchives).build());
    if (configFiles != null) {
        for (String cf : configFiles) {
            File f = new File(cf);
            checkCanRead(f, "Configuration file");
            try (FileInputStream is = new FileInputStream(f)) {
                ctxBld.mergeConfigurationFromJSONStream(is);
            }
        }
    }
    for (Map.Entry<String, String> e : additionalConfig.entrySet()) {
        String[] keyPath = e.getKey().split("\\.");
        ModelNode additionalNode = new ModelNode();
        ModelNode key = additionalNode.get(keyPath);
        String value = e.getValue();
        if (value.startsWith("[") && value.endsWith("]")) {
            String[] values = value.substring(1, value.length() - 1).split("\\s*,\\s*");
            for (String v : values) {
                key.add(v);
            }
        } else {
            key.set(value);
        }
        ctxBld.mergeConfiguration(additionalNode);
    }
    LOG.info("Starting analysis");
    try (AnalysisResult result = revapi.analyze(ctxBld.build())) {
        if (!result.isSuccess()) {
            throw result.getFailure();
        }
    }
}
Also used : Arrays(java.util.Arrays) FileArchive(org.revapi.simple.FileArchive) MavenBootstrap(pw.krejci.modules.maven.MavenBootstrap) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) DependencySpec(org.jboss.modules.DependencySpec) ModuleSpec(org.jboss.modules.ModuleSpec) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Map(java.util.Map) ArtifactResolver(org.revapi.maven.utils.ArtifactResolver) Getopt(gnu.getopt.Getopt) Collectors.toSet(java.util.stream.Collectors.toSet) Nullable(javax.annotation.Nullable) LongOpt(gnu.getopt.LongOpt) API(org.revapi.API) Properties(java.util.Properties) AnalysisContext(org.revapi.AnalysisContext) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Collections.emptyList(java.util.Collections.emptyList) Set(java.util.Set) Artifact(org.eclipse.aether.artifact.Artifact) AnalysisResult(org.revapi.AnalysisResult) FileInputStream(java.io.FileInputStream) Revapi(org.revapi.Revapi) File(java.io.File) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) List(java.util.List) Module(org.jboss.modules.Module) RepositoryException(org.eclipse.aether.RepositoryException) ProjectModule(pw.krejci.modules.maven.ProjectModule) ModuleSpecController(pw.krejci.modules.maven.ModuleSpecController) ScopeDependencyTraverser(org.revapi.maven.utils.ScopeDependencyTraverser) ModelNode(org.jboss.dmr.ModelNode) ScopeDependencySelector(org.revapi.maven.utils.ScopeDependencySelector) RepositorySystem(org.eclipse.aether.RepositorySystem) HashSet(java.util.HashSet) Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) Revapi(org.revapi.Revapi) AnalysisContext(org.revapi.AnalysisContext) Properties(java.util.Properties) ModuleSpec(org.jboss.modules.ModuleSpec) DependencySpec(org.jboss.modules.DependencySpec) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) FileInputStream(java.io.FileInputStream) AnalysisResult(org.revapi.AnalysisResult) ProjectModule(pw.krejci.modules.maven.ProjectModule) ModuleSpecController(pw.krejci.modules.maven.ModuleSpecController) Module(org.jboss.modules.Module) ProjectModule(pw.krejci.modules.maven.ProjectModule) ModelNode(org.jboss.dmr.ModelNode) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 7 with AnalysisResult

use of org.revapi.AnalysisResult in project revapi by revapi.

the class AbstractVersionModifyingMojo method analyzeProject.

private AnalysisResults analyzeProject(MavenProject project) throws MojoExecutionException {
    Analyzer analyzer = prepareAnalyzer(project, ApiBreakageHintingReporter.class, Collections.emptyMap());
    try {
        analyzer.resolveArtifacts();
        if (analyzer.getResolvedOldApi() == null) {
            return null;
        } else {
            try (AnalysisResult res = analyzer.analyze()) {
                res.throwIfFailed();
                ApiBreakageHintingReporter reporter = res.getExtensions().getFirstExtension(ApiBreakageHintingReporter.class, null);
                ApiChangeLevel level = reporter.getChangeLevel();
                String baseVersion = ((MavenArchive) analyzer.getResolvedOldApi().getArchives().iterator().next()).getVersion();
                return new AnalysisResults(level, baseVersion);
            }
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Analysis failure", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) AnalysisResult(org.revapi.AnalysisResult) NavException(com.ximpleware.NavException) TranscodeException(com.ximpleware.TranscodeException) XPathEvalException(com.ximpleware.XPathEvalException) XPathParseException(com.ximpleware.XPathParseException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ModifyException(com.ximpleware.ModifyException)

Example 8 with AnalysisResult

use of org.revapi.AnalysisResult in project revapi by revapi.

the class ReportMojo method ensureAnalyzed.

private void ensureAnalyzed(Locale locale) {
    if (!skip && analysisResult == null) {
        Analyzer analyzer = prepareAnalyzer(locale);
        if (analyzer == null) {
            return;
        }
        try (AnalysisResult res = analyzer.analyze()) {
            res.throwIfFailed();
            oldAPI = analyzer.getResolvedOldApi();
            newAPI = analyzer.getResolvedNewApi();
            analysisResult = res;
        } catch (Exception e) {
            throw new IllegalStateException("Failed to generate report.", e);
        }
    }
}
Also used : AnalysisResult(org.revapi.AnalysisResult) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 9 with AnalysisResult

use of org.revapi.AnalysisResult in project revapi by revapi.

the class AbstractJavaElementAnalyzerTest method runAnalysis.

protected <R extends Reporter> R runAnalysis(Class<R> reporterType, String configurationJSON, String[] v1Source, String[] v2Source) throws Exception {
    boolean doV1 = v1Source != null && v1Source.length > 0;
    boolean doV2 = v2Source != null && v2Source.length > 0;
    ArchiveAndCompilationPath v1Archive = doV1 ? createCompiledJar("v1", v1Source) : new ArchiveAndCompilationPath(null, null);
    ArchiveAndCompilationPath v2Archive = doV2 ? createCompiledJar("v2", v2Source) : new ArchiveAndCompilationPath(null, null);
    Revapi revapi = createRevapi(reporterType);
    AnalysisContext.Builder bld = AnalysisContext.builder(revapi).withOldAPI(doV1 ? API.of(new ShrinkwrapArchive(v1Archive.archive)).build() : API.builder().build()).withNewAPI(doV2 ? API.of(new ShrinkwrapArchive(v2Archive.archive)).build() : API.builder().build());
    if (configurationJSON != null) {
        bld.withConfigurationFromJSON(configurationJSON);
    }
    AnalysisContext ctx = bld.build();
    revapi.validateConfiguration(ctx);
    try (AnalysisResult result = revapi.analyze(ctx)) {
        result.throwIfFailed();
        return result.getExtensions().getFirstExtension(reporterType, null);
    } finally {
        if (doV1) {
            deleteDir(v1Archive.compilationPath);
        }
        if (doV2) {
            deleteDir(v2Archive.compilationPath);
        }
    }
}
Also used : Revapi(org.revapi.Revapi) AnalysisContext(org.revapi.AnalysisContext) AnalysisResult(org.revapi.AnalysisResult)

Example 10 with AnalysisResult

use of org.revapi.AnalysisResult in project revapi by revapi.

the class SupplementaryJarsTest method testExcludedClassesInAPI.

@Test
public void testExcludedClassesInAPI() throws Exception {
    List<Report> allReports;
    Revapi revapi = createRevapi(CollectingReporter.class);
    AnalysisContext ctx = AnalysisContext.builder(revapi).withOldAPI(API.of(new ShrinkwrapArchive(apiV1)).supportedBy(new ShrinkwrapArchive(supV1)).build()).withNewAPI(API.of(new ShrinkwrapArchive(apiV2)).supportedBy(new ShrinkwrapArchive(supV2)).build()).withConfigurationFromJSON("{\"revapi\": {\"java\": {" + "\"filter\": {\"classes\": {\"exclude\": [\"C\", \"B.T$2\"]}}}}}").build();
    try (AnalysisResult res = revapi.analyze(ctx)) {
        allReports = res.getExtensions().getFirstExtension(CollectingReporter.class, null).getReports();
    }
    Assert.assertEquals(3, allReports.size());
    Assert.assertFalse(containsDifference(allReports, null, "class B.T$1.Private", Code.CLASS_NON_PUBLIC_PART_OF_API.code()));
    Assert.assertFalse(containsDifference(allReports, null, "field B.T$2.f2", Code.FIELD_ADDED.code()));
    Assert.assertTrue(containsDifference(allReports, null, "field A.f3", Code.FIELD_ADDED.code()));
    Assert.assertFalse(containsDifference(allReports, "class B.T$2", "class B.T$2", Code.CLASS_NOW_FINAL.code()));
    Assert.assertTrue(containsDifference(allReports, null, "class B.T$3", Code.CLASS_ADDED.code()));
    Assert.assertTrue(containsDifference(allReports, null, "class B.PrivateUsedClass", Code.CLASS_NON_PUBLIC_PART_OF_API.code()));
    Assert.assertFalse(containsDifference(allReports, "class B.UsedByIgnoredClass", "class B.UsedByIgnoredClass", Code.CLASS_KIND_CHANGED.code()));
    Assert.assertFalse(containsDifference(allReports, "method void B.UsedByIgnoredClass::<init>()", null, Code.METHOD_REMOVED.code()));
}
Also used : Revapi(org.revapi.Revapi) Report(org.revapi.Report) AnalysisContext(org.revapi.AnalysisContext) AnalysisResult(org.revapi.AnalysisResult) Test(org.junit.Test)

Aggregations

AnalysisResult (org.revapi.AnalysisResult)11 Revapi (org.revapi.Revapi)7 AnalysisContext (org.revapi.AnalysisContext)6 Test (org.junit.Test)4 Report (org.revapi.Report)4 List (java.util.List)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 MavenReportException (org.apache.maven.reporting.MavenReportException)2 API (org.revapi.API)2 ModifyException (com.ximpleware.ModifyException)1 NavException (com.ximpleware.NavException)1 TranscodeException (com.ximpleware.TranscodeException)1 XPathEvalException (com.ximpleware.XPathEvalException)1 XPathParseException (com.ximpleware.XPathParseException)1 Getopt (gnu.getopt.Getopt)1 LongOpt (gnu.getopt.LongOpt)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1