private static void toJarArchiveComparatorClassPathMode(Options options, JarArchiveComparatorOptions comparatorOptions) { switch (options.getClassPathMode()) { case TWO_SEPARATE_CLASSPATHS: comparatorOptions.setClassPathMode(ClassPathMode.TWO_SEPARATE_CLASSPATHS); break; case ONE_COMMON_CLASSPATH: comparatorOptions.setClassPathMode(ClassPathMode.ONE_COMMON_CLASSPATH); break; default: throw new JApiCmpException(JApiCmpException.Reason.IllegalState, "Unknown classPathMode: " + options.getClassPathMode()); } }
public static JarArchiveComparatorOptions of(Options options) { JarArchiveComparatorOptions comparatorOptions = new JarArchiveComparatorOptions(); comparatorOptions.getFilters().getExcludes().addAll(options.getExcludes()); comparatorOptions.getFilters().getIncludes().addAll(options.getIncludes()); comparatorOptions.setAccessModifier(options.getAccessModifier()); comparatorOptions.setIncludeSynthetic(options.isIncludeSynthetic()); comparatorOptions.setIgnoreMissingClasses(options.getIgnoreMissingClasses()); toJarArchiveComparatorClassPathMode(options, comparatorOptions); toJarArchiveComparatorClassPath(options.getOldClassPath(), comparatorOptions.getOldClassPath()); toJarArchiveComparatorClassPath(options.getNewClassPath(), comparatorOptions.getNewClassPath()); comparatorOptions.setNoAnnotations(options.isNoAnnotations()); return comparatorOptions; }
private void setupClasspaths() { if (this.options.getClassPathMode() == JarArchiveComparatorOptions.ClassPathMode.ONE_COMMON_CLASSPATH) { commonClassPool = new ClassPool(); commonClassPathAsString = setupClasspath(commonClassPool, this.options.getClassPathEntries()); } else if (this.options.getClassPathMode() == JarArchiveComparatorOptions.ClassPathMode.TWO_SEPARATE_CLASSPATHS) { oldClassPool = new ClassPool(); oldClassPathAsString = setupClasspath(oldClassPool, this.options.getOldClassPath()); newClassPool = new ClassPool(); newClassPathAsString = setupClasspath(newClassPool, this.options.getNewClassPath()); } else { throw new JApiCmpException(Reason.IllegalState, "Unknown classpath mode: " + this.options.getClassPathMode()); } }
private JarArchiveComparatorOptions createOptions() { JarArchiveComparatorOptions options = new JarArchiveComparatorOptions(); options.setClassPathMode(JarArchiveComparatorOptions.ClassPathMode.TWO_SEPARATE_CLASSPATHS); options.setIncludeSynthetic(includeSynthetic); options.getIgnoreMissingClasses().setIgnoreAllMissingClasses(ignoreMissingClasses); for (String packageInclude : packageIncludes) { options.getFilters().getIncludes().add(new JavadocLikePackageFilter(packageInclude)); options.getFilters().getExcludes().add(new JavadocLikePackageFilter(packageExclude)); options.getFilters().getIncludes().add(new JavaDocLikeClassFilter(classInclude)); options.getFilters().getExcludes().add(new JavaDocLikeClassFilter(classExclude)); options.getFilters().getIncludes().add(new JavadocLikeBehaviorFilter(methodInclude)); options.getFilters().getExcludes().add(new JavadocLikeBehaviorFilter(methodExclude)); options.getFilters().getIncludes().add(new JavadocLikeFieldFilter(fieldInclude)); options.getFilters().getExcludes().add(new JavadocLikeFieldFilter(fieldExclude)); options.getFilters().getIncludes().add(new AnnotationClassFilter(annotationInclude)); options.getFilters().getIncludes().add(new AnnotationFieldFilter(annotationInclude)); options.getFilters().getIncludes().add(new AnnotationBehaviorFilter(annotationInclude)); options.getFilters().getExcludes().add(new AnnotationClassFilter(annotationExclude)); options.getFilters().getExcludes().add(new AnnotationFieldFilter(annotationExclude)); options.getFilters().getExcludes().add(new AnnotationBehaviorFilter(annotationExclude));
List<JApiCmpArchive> jApiCmpArchives = resolveDependencyToFile("dependencies", dependency, mavenParameters, true, pluginParameters, ConfigurationVersion.NEW); for (JApiCmpArchive jApiCmpArchive : jApiCmpArchives) { comparatorOptions.getClassPathEntries().add(jApiCmpArchive.getFile().getAbsolutePath()); comparatorOptions.setClassPathMode(JarArchiveComparatorOptions.ClassPathMode.ONE_COMMON_CLASSPATH); List<JApiCmpArchive> jApiCmpArchives = resolveDependencyToFile("oldClassPathDependencies", dependency, mavenParameters, true, pluginParameters, ConfigurationVersion.OLD); for (JApiCmpArchive archive : jApiCmpArchives) { comparatorOptions.getOldClassPath().add(archive.getFile().getAbsolutePath()); List<JApiCmpArchive> jApiCmpArchives = resolveDependencyToFile("newClassPathDependencies", dependency, mavenParameters, true, pluginParameters, ConfigurationVersion.NEW); for (JApiCmpArchive archive : jApiCmpArchives) { comparatorOptions.getNewClassPath().add(archive.getFile().getAbsolutePath()); comparatorOptions.setClassPathMode(JarArchiveComparatorOptions.ClassPathMode.TWO_SEPARATE_CLASSPATHS); } else { if (getLog().isDebugEnabled()) { getLog().debug("None of the elements <oldClassPathDependencies/>, <newClassPathDependencies/> or <dependencies/> found. Using " + JApiCli.ClassPathMode.ONE_COMMON_CLASSPATH); comparatorOptions.setClassPathMode(JarArchiveComparatorOptions.ClassPathMode.ONE_COMMON_CLASSPATH);
private List<CtClass> applyFilter(JarArchiveComparatorOptions options, List<CtClass> ctClasses) { List<CtClass> newList = new ArrayList<>(ctClasses.size()); for (CtClass ctClass : ctClasses) { if (options.getFilters().includeClass(ctClass)) { newList.add(ctClass); } } return newList; }
@Override public void execute() { if (oldJar == null) { throw new BuildException("Path to old jar must be specified using the oldjar attribute."); } if (newJar == null) { throw new BuildException("Path to new jar must be specified using the newjar attribute."); } Options options = createOptionsFromAntAttrs(); JarArchiveComparator jarArchiveComparator = new JarArchiveComparator(JarArchiveComparatorOptions.of(options)); List<JApiClass> jApiClasses = jarArchiveComparator.compare(options.getOldArchives(), options.getNewArchives()); generateOutput(options, jApiClasses, jarArchiveComparator); }
if (this.options.getClassPathMode() == JarArchiveComparatorOptions.ClassPathMode.ONE_COMMON_CLASSPATH) { try { loadedClass = Optional.of(commonClassPool.get(name)); } catch (NotFoundException e) { if (!options.getIgnoreMissingClasses().ignoreClass(e.getMessage())) { throw JApiCmpException.forClassLoading(e, name, this); } else if (this.options.getClassPathMode() == JarArchiveComparatorOptions.ClassPathMode.TWO_SEPARATE_CLASSPATHS) { if (archiveType == ArchiveType.OLD) { try { loadedClass = Optional.of(oldClassPool.get(name)); } catch (NotFoundException e) { if (!options.getIgnoreMissingClasses().ignoreClass(e.getMessage())) { throw JApiCmpException.forClassLoading(e, name, this); loadedClass = Optional.of(newClassPool.get(name)); } catch (NotFoundException e) { if (!options.getIgnoreMissingClasses().ignoreClass(e.getMessage())) { throw JApiCmpException.forClassLoading(e, name, this); throw new JApiCmpException(Reason.IllegalState, "Unknown classpath mode: " + this.options.getClassPathMode());
private static boolean isCtClassSerializable(JarArchiveComparatorOptions options, CtClass clazz, JarArchiveComparator jarArchiveComparator) { ClassPool pool = clazz.getClassPool(); try { return clazz.subtypeOf(pool.get("java.io.Serializable")); } catch (NotFoundException e) { if (options.getIgnoreMissingClasses().ignoreClass(e.getMessage())) { return false; } else { throw JApiCmpException.forClassLoading(e, clazz.getName(), jarArchiveComparator); } } }
private void setUpClassPathUsingMavenProject(JarArchiveComparatorOptions comparatorOptions, MavenParameters mavenParameters, PluginParameters pluginParameters, ConfigurationVersion configurationVersion) throws MojoFailureException { notNull(mavenParameters.getMavenProject(), "Maven parameter mavenProject should be provided by maven container."); Set<Artifact> dependencyArtifacts = mavenParameters.getMavenProject().getArtifacts(); Set<String> classPathEntries = new HashSet<>(); for (Artifact artifact : dependencyArtifacts) { String scope = artifact.getScope(); if (!"test".equals(scope) && !artifact.isOptional()) { Set<Artifact> artifacts = resolveArtifact(artifact, mavenParameters, false, pluginParameters, configurationVersion); for (Artifact resolvedArtifact : artifacts) { File resolvedFile = resolvedArtifact.getFile(); if (resolvedFile != null) { String absolutePath = resolvedFile.getAbsolutePath(); if (!classPathEntries.contains(absolutePath)) { if (getLog().isDebugEnabled()) { getLog().debug("Adding to classpath: " + absolutePath + "; scope: " + scope); } classPathEntries.add(absolutePath); } } } } } for (String classPathEntry : classPathEntries) { comparatorOptions.getClassPathEntries().add(classPathEntry); } }
private List<CtClass> applyFilter(JarArchiveComparatorOptions options, List<CtClass> ctClasses) { List<CtClass> newList = new ArrayList<>(ctClasses.size()); for (CtClass ctClass : ctClasses) { if (options.getFilters().includeClass(ctClass)) { newList.add(ctClass); } } return newList; }
JarArchiveComparatorOptions comparatorOptions = JarArchiveComparatorOptions.of(options); setUpClassPath(comparatorOptions, pluginParameters, mavenParameters); setUpOverrideCompatibilityChanges(comparatorOptions, pluginParameters);
if (this.options.getClassPathMode() == JarArchiveComparatorOptions.ClassPathMode.ONE_COMMON_CLASSPATH) { try { loadedClass = Optional.of(commonClassPool.get(name)); } catch (NotFoundException e) { if (!options.getIgnoreMissingClasses().ignoreClass(e.getMessage())) { throw JApiCmpException.forClassLoading(e, name, this); } else if (this.options.getClassPathMode() == JarArchiveComparatorOptions.ClassPathMode.TWO_SEPARATE_CLASSPATHS) { if (archiveType == ArchiveType.OLD) { try { loadedClass = Optional.of(oldClassPool.get(name)); } catch (NotFoundException e) { if (!options.getIgnoreMissingClasses().ignoreClass(e.getMessage())) { throw JApiCmpException.forClassLoading(e, name, this); loadedClass = Optional.of(newClassPool.get(name)); } catch (NotFoundException e) { if (!options.getIgnoreMissingClasses().ignoreClass(e.getMessage())) { throw JApiCmpException.forClassLoading(e, name, this); throw new JApiCmpException(Reason.IllegalState, "Unknown classpath mode: " + this.options.getClassPathMode());
private static boolean isCtClassSerializable(JarArchiveComparatorOptions options, CtClass clazz, JarArchiveComparator jarArchiveComparator) { ClassPool pool = clazz.getClassPool(); try { return clazz.subtypeOf(pool.get("java.io.Serializable")); } catch (NotFoundException e) { if (options.getIgnoreMissingClasses().ignoreClass(e.getMessage())) { return false; } else { throw JApiCmpException.forClassLoading(e, clazz.getName(), jarArchiveComparator); } } }
public static JarArchiveComparatorOptions of(Options options) { JarArchiveComparatorOptions comparatorOptions = new JarArchiveComparatorOptions(); comparatorOptions.getFilters().getExcludes().addAll(options.getExcludes()); comparatorOptions.getFilters().getIncludes().addAll(options.getIncludes()); comparatorOptions.setAccessModifier(options.getAccessModifier()); comparatorOptions.setIncludeSynthetic(options.isIncludeSynthetic()); comparatorOptions.setIgnoreMissingClasses(options.getIgnoreMissingClasses()); toJarArchiveComparatorClassPathMode(options, comparatorOptions); toJarArchiveComparatorClassPath(options.getOldClassPath(), comparatorOptions.getOldClassPath()); toJarArchiveComparatorClassPath(options.getNewClassPath(), comparatorOptions.getNewClassPath()); comparatorOptions.setNoAnnotations(options.isNoAnnotations()); return comparatorOptions; }
private void setupClasspaths() { if (this.options.getClassPathMode() == JarArchiveComparatorOptions.ClassPathMode.ONE_COMMON_CLASSPATH) { commonClassPool = new ClassPool(); commonClassPathAsString = setupClasspath(commonClassPool, this.options.getClassPathEntries()); } else if (this.options.getClassPathMode() == JarArchiveComparatorOptions.ClassPathMode.TWO_SEPARATE_CLASSPATHS) { oldClassPool = new ClassPool(); oldClassPathAsString = setupClasspath(oldClassPool, this.options.getOldClassPath()); newClassPool = new ClassPool(); newClassPathAsString = setupClasspath(newClassPool, this.options.getNewClassPath()); } else { throw new JApiCmpException(Reason.IllegalState, "Unknown classpath mode: " + this.options.getClassPathMode()); } }
private Map<String, CtField> buildFieldMap(CtClass ctClass) { Map<String, CtField> fieldMap = new HashMap<>(); CtField[] declaredFields = ctClass.getDeclaredFields(); for (CtField field : declaredFields) { if (options.getFilters().includeField(field)) { String name = field.getName(); fieldMap.put(name, field); } } return fieldMap; }
public void run(String[] args) { CliParser cliParser = new CliParser(); Options options = cliParser.parse(args); if (options.isHelpRequested()) { CliParser.printHelp(); return; } JarArchiveComparator jarArchiveComparator = new JarArchiveComparator(JarArchiveComparatorOptions.of(options)); List<JApiClass> jApiClasses = jarArchiveComparator.compare(options.getOldArchives(), options.getNewArchives()); generateOutput(options, jApiClasses, jarArchiveComparator); }
Optional<CtClass> oldClassOptional = Optional.absent(); Optional<CtClass> newClassOptional = Optional.absent(); JarArchiveComparatorOptions.ClassPathMode classPathMode = this.jarArchiveComparator.getJarArchiveComparatorOptions().getClassPathMode(); if (classPathMode == JarArchiveComparatorOptions.ClassPathMode.ONE_COMMON_CLASSPATH) { ClassPool classPool = this.jarArchiveComparator.getCommonClassPool(); if (!this.jarArchiveComparator.getJarArchiveComparatorOptions().getIgnoreMissingClasses().ignoreClass(newSuperclassName)) { throw JApiCmpException.forClassLoading(newSuperclassName, this.jarArchiveComparator);
private void buildInterfaceMap(CtClass ctClass, JarArchiveComparator.ArchiveType archiveType, Map<String, CtClass> map) { try { CtClass[] interfaces = ctClass.getInterfaces(); for (CtClass ctInterface : interfaces) { map.put(ctInterface.getName(), ctInterface); buildInterfaceMap(archiveType, map, ctInterface); } Optional<CtClass> superClassOptional = getSuperclass(ctClass); if (superClassOptional.isPresent()) { buildInterfaceMap(superClassOptional.get(), archiveType, map); } } catch (NotFoundException e) { if (!options.getIgnoreMissingClasses().ignoreClass(e.getMessage())) { throw JApiCmpException.forClassLoading(e, "Class not found: " + e.getMessage(), jarArchiveComparator); } } }