/** * Create an annotation JAR for a JAR file * * @param jarFile JAR file to process * @return <code>true</code> if nullability information was found */ protected boolean createAnnotationJar(File jarFile) { File annotationJarFile = getTargetFile(jarFile); boolean result = withTempFile("annotations", ".zip", tempFile -> { try { boolean annotated = createAnnotationJar(jarFile, tempFile); if (annotated) { Files.move(tempFile, annotationJarFile); } else { Files.write(new byte[0], annotationJarFile); } return annotated; } catch (Exception e) { throw new GradleException(String.format("Could not read JAR '%s'.", jarFile), e); } }); return result; }
/** * Get the target file for an input file * * @param jarFile Source JAR file * @return Target file */ protected File getTargetFile(File jarFile) { String baseName = Files.getNameWithoutExtension(jarFile.getName()); File annotationJarFile = new File(getDestinationDir(), baseName + "-annotations.zip"); return annotationJarFile; }
/** * Create a new task */ public EclipseAnnotationsTask() { this.jars = Collections.emptySet(); this.destinationDir = new File(getProject().getBuildDir(), "annotations"); }
/** * Create Eclipse annotations tasks * * @param tasks Task container * @param configurations Container to access configurations * @param buildDir Build directory */ @Mutate public void createEclipseAnnotationsTasks(ModelMap<Task> tasks, ConfigurationContainer configurations, @Path("buildDir") File buildDir) { tasks.create("eclipseAnnotations", EclipseAnnotationsTask.class, t -> { t.setDescription("Generates external nullability annotations for dependencies."); t.setGroup("IDE"); ConventionMapping parameters = t.getConventionMapping(); parameters.map("jars", () -> { Set<File> jars = configurations.stream() .filter(c -> c.isCanBeResolved() && !c.getName().equals(JavaConfigPlugin.ANNOTATIONS_CONFIGURATION)) .map(c -> c.getResolvedConfiguration().getLenientConfiguration()) .flatMap(c -> c.getArtifacts().stream() .filter(a -> !(a.getId() .getComponentIdentifier() instanceof ProjectComponentIdentifier) && a.getType().equals("jar")) .map(a -> a.getFile())) .collect(Collectors.toSet()); return jars; }); }); }
/** * Get the URLs for the classpath during annotation processing * * @return Classpath URLs */ @Input @Classpath protected Set<URL> getClasspathUrls() { Configuration compileConfiguration = getProject().getConfigurations() .getByName(EclipseConfigPlugin.ECLIPSE_ANNOTATIONS_CONFIGURATION); getLogger().info("Using classpath '{}'", compileConfiguration.getAsPath()); Set<URL> urls = compileConfiguration.getFiles().stream().map(f -> { try { return f.toURI().toURL(); } catch (MalformedURLException e) { throw new GradleException( String.format("Could not create classpath for annotations task %s.", getName()), e); } }).collect(Collectors.toSet()); return ImmutableSet.copyOf(urls); } }
/** * Create the annotation JARs for the input JARs * * @param inputs Task inputs */ @TaskAction public void createAnnotationJars(IncrementalTaskInputs inputs) { if (!inputs.isIncremental()) { for (File file : getAnnotationJars()) { file.delete(); } } inputs.outOfDate(file -> { createAnnotationJar(file.getFile()); }); inputs.removed(file -> { file.getFile().delete(); }); }
getLogger().info("Creating annotations JAR for '{}'", jarFile); Set<URL> classpathUrls = getClasspathUrls(); ClassLoader classLoader = new URLClassLoader(classpathUrls.toArray(new URL[classpathUrls.size()])); Set<String> knownErrors = new HashSet<>(); getLogger().debug("Adding annotations for type {}", type.getName()); getLogger().info(e.getMessage()); String message = Throwables.getCausalChain(e).stream().map(t -> t.getMessage()) .collect(Collectors.joining(": ")); getLogger().error(message);
/** * Get the created annotation JARs * * @return Created annotation JARs */ @OutputFiles public Set<File> getAnnotationJars() { return this.jars.stream().map(f -> getTargetFile(Validate.notNull(f))) .collect(Collectors.toCollection(() -> new HashSet<>())); }
/** * Create Eclipse annotations tasks * * @param tasks Task container * @param configurations Container to access configurations * @param buildDir Build directory */ @Mutate public void createEclipseAnnotationsTasks(ModelMap<Task> tasks, ConfigurationContainer configurations, @Path("buildDir") File buildDir) { tasks.create("eclipseAnnotations", EclipseAnnotationsTask.class, t -> { t.setDescription("Generates external nullability annotations for dependencies."); t.setGroup("IDE"); ConventionMapping parameters = t.getConventionMapping(); parameters.map("jars", () -> { Set<File> jars = configurations.stream() .filter(c -> c.isCanBeResolved() && !c.getName().equals(JavaConfigPlugin.ANNOTATIONS_CONFIGURATION)) .map(c -> c.getResolvedConfiguration().getLenientConfiguration()) .flatMap(c -> c.getArtifacts().stream() .filter(a -> !(a.getId() .getComponentIdentifier() instanceof ProjectComponentIdentifier) && a.getType().equals("jar")) .map(a -> a.getFile())) .collect(Collectors.toSet()); return jars; }); }); }
/** * Get the URLs for the classpath during annotation processing * * @return Classpath URLs */ @Input @Classpath protected Set<URL> getClasspathUrls() { Configuration compileConfiguration = getProject().getConfigurations() .getByName(EclipseConfigPlugin.ECLIPSE_ANNOTATIONS_CONFIGURATION); getLogger().info("Using classpath '{}'", compileConfiguration.getAsPath()); Set<URL> urls = compileConfiguration.getFiles().stream().map(f -> { try { return f.toURI().toURL(); } catch (MalformedURLException e) { throw new GradleException( String.format("Could not create classpath for annotations task %s.", getName()), e); } }).collect(Collectors.toSet()); return ImmutableSet.copyOf(urls); } }
/** * Create the annotation JARs for the input JARs * * @param inputs Task inputs */ @TaskAction public void createAnnotationJars(IncrementalTaskInputs inputs) { if (!inputs.isIncremental()) { for (File file : getAnnotationJars()) { file.delete(); } } inputs.outOfDate(file -> { createAnnotationJar(file.getFile()); }); inputs.removed(file -> { file.getFile().delete(); }); }
getLogger().info("Creating annotations JAR for '{}'", jarFile); Set<URL> classpathUrls = getClasspathUrls(); ClassLoader classLoader = new URLClassLoader(classpathUrls.toArray(new URL[classpathUrls.size()])); Set<String> knownErrors = new HashSet<>(); getLogger().debug("Adding annotations for type {}", type.getName()); getLogger().info(e.getMessage()); String message = Throwables.getCausalChain(e).stream().map(t -> t.getMessage()) .collect(Collectors.joining(": ")); getLogger().error(message);
/** * Get the created annotation JARs * * @return Created annotation JARs */ @OutputFiles public Set<File> getAnnotationJars() { return this.jars.stream().map(f -> getTargetFile(Validate.notNull(f))) .collect(Collectors.toCollection(() -> new HashSet<>())); }
/** * Create an annotation JAR for a JAR file * * @param jarFile JAR file to process * @return <code>true</code> if nullability information was found */ protected boolean createAnnotationJar(File jarFile) { File annotationJarFile = getTargetFile(jarFile); boolean result = withTempFile("annotations", ".zip", tempFile -> { try { boolean annotated = createAnnotationJar(jarFile, tempFile); if (annotated) { Files.move(tempFile, annotationJarFile); } else { Files.write(new byte[0], annotationJarFile); } return annotated; } catch (Exception e) { throw new GradleException(String.format("Could not read JAR '%s'.", jarFile), e); } }); return result; }
/** * Get the target file for an input file * * @param jarFile Source JAR file * @return Target file */ protected File getTargetFile(File jarFile) { String baseName = Files.getNameWithoutExtension(jarFile.getName()); File annotationJarFile = new File(getDestinationDir(), baseName + "-annotations.zip"); return annotationJarFile; }
/** * Create a new task */ public EclipseAnnotationsTask() { this.jars = Collections.emptySet(); this.destinationDir = new File(getProject().getBuildDir(), "annotations"); }