Skip to content

Commit

Permalink
[NETBEANS-4278][NETBEANS-4279] Add support for Generated sources and …
Browse files Browse the repository at this point in the history
…annotation processor path
  • Loading branch information
lkishalmi committed Jul 2, 2020
1 parent 21e19a3 commit 13bac58
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ class NbProjectInfoBuilder {

Map<String, File> ib = new HashMap<>();
println "Gradle Version: $gradleVersion"
if (gradleVersion.compareTo(VersionNumber.parse('3.1')) >= 0) {
sinceGradle '3.1' {
for(IncludedBuild p: project.gradle.includedBuilds) {
println "Include Build: ${p.name}"
ib.put(p.name, p.projectDir);
}
}
model.info.project_includedBuilds = ib;

if (gradleVersion.compareTo(VersionNumber.parse('3.3')) >= 0) {
sinceGradle '3.3' {
model.info.project_display_name = project.displayName;
}
try {
Expand Down Expand Up @@ -158,11 +158,12 @@ class NbProjectInfoBuilder {

private void detectTests(NbProjectInfoModel model) {
Set<File> testClassesRoots = new HashSet<>()
if (gradleVersion.compareTo(VersionNumber.parse('4.0')) >= 0) {
sinceGradle '4.0' {
project.tasks.withType(Test) { task ->
task.testClassesDirs.each() { it -> testClassesRoots.add(it) }
task.testClassesDirs.each() { dir -> testClassesRoots.add(dir) }
}
} else {
}
beforeGradle '4.0' {
project.tasks.withType(Test) { task ->
testClassesRoots.add(task.testClassesDir)
}
Expand Down Expand Up @@ -219,26 +220,38 @@ class NbProjectInfoBuilder {
model.info["sourceset_${sourceSet.name}_JAVA"] = storeSet(sourceSet.java.srcDirs);
model.info["sourceset_${sourceSet.name}_RESOURCES"] = storeSet(sourceSet.resources.srcDirs);
if (hasGroovy)
model.info["sourceset_${sourceSet.name}_GROOVY"] = storeSet(sourceSet.groovy.srcDirs);
model.info["sourceset_${sourceSet.name}_GROOVY"] = storeSet(sourceSet.groovy.srcDirs);
if (hasScala)
model.info["sourceset_${sourceSet.name}_SCALA"] = storeSet(sourceSet.scala.srcDirs);
if (gradleVersion.compareTo(VersionNumber.parse('4.0')) >= 0) {
model.info["sourceset_${sourceSet.name}_SCALA"] = storeSet(sourceSet.scala.srcDirs);
sinceGradle '4.0' {
def dirs = new LinkedHashSet<File>();
// classesDirs is just an iteratable
// classesDirs is just an iterable
for (def dir in sourceSet.output.classesDirs) {
dirs.add(dir);
}
model.info["sourceset_${sourceSet.name}_output_classes"] = storeSet(dirs);
} else {
}
beforeGradle '4.0' {
model.info["sourceset_${sourceSet.name}_output_classes"] = Collections.singleton(sourceSet.output.classesDir);
}
model.info["sourceset_${sourceSet.name}_output_resources"] = sourceSet.output.resourcesDir;
sinceGradle '5.2' {
model.info["sourceset_${sourceSet.name}_output_generated"] = storeSet(sourceSet.output.generatedSourcesDirs);
}
try {
model.info["sourceset_${sourceSet.name}_classpath_compile"] = storeSet(sourceSet.compileClasspath.files);
model.info["sourceset_${sourceSet.name}_classpath_runtime"] = storeSet(sourceSet.runtimeClasspath.files);
} catch(Exception e) {
model.noteProblem(e)
}
sinceGradle '4.6' {
try {
model.info["sourceset_${sourceSet.name}_classpath_annotation"] = storeSet(sourceSet.annotationProcessorPath.files);
} catch(Exception e) {
model.noteProblem(e)
}
model.info["sourceset_${sourceSet.name}_configuration_annotation"] = sourceSet.annotationProcessorConfigurationName();
}
model.info["sourceset_${sourceSet.name}_configuration_compile"] = sourceSet.compileConfigurationName;
model.info["sourceset_${sourceSet.name}_configuration_runtime"] = sourceSet.runtimeConfigurationName;
}
Expand All @@ -260,7 +273,7 @@ class NbProjectInfoBuilder {
model.info.webapp_dir = project.webAppDir
model.info.webxml = project.war.webXml
try {
model.info.exploded_war_dir = project.explodedWar.destinationDir
model.info.exploded_war_dir = project.explodedWar.destinationDir
} catch(Exception e) {
model.noteProblem(e)
}
Expand Down Expand Up @@ -444,16 +457,16 @@ class NbProjectInfoBuilder {
deps.addAll(model.info["configuration_${confiurationName}_unresolved"])
deps.addAll(model.info["configuration_${confiurationName}_files"])
}
model.info["configuration_${confiurationName}_extendsFrom"].each {
collectModuleDependencies(model, it, true, deps)
}
model.info["configuration_${confiurationName}_extendsFrom"].each {
collectModuleDependencies(model, it, true, deps)
}
}

private static <T extends Serializable> Set<T> storeSet(Collection<? extends T> c) {
switch (c.size()) {
case 0: return Collections.emptySet();
case 1: return Collections.singleton(c.first())
default: return new LinkedHashSet(c)
case 0: return Collections.emptySet();
case 1: return Collections.singleton(c.first())
default: return new LinkedHashSet(c)
}
}

Expand All @@ -467,5 +480,18 @@ class NbProjectInfoBuilder {
}
return ret
}

private void sinceGradle(String version, Closure cl) {
if (gradleVersion.compareTo(VersionNumber.parse(version)) >= 0) {
cl()
}
}

private void beforeGradle(String version, Closure cl) {
if (gradleVersion.compareTo(VersionNumber.parse(version)) < 0) {
cl()
}
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.netbeans.modules.gradle.java.classpath;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.netbeans.api.project.Project;
import org.netbeans.modules.gradle.java.api.GradleJavaSourceSet;
import static org.netbeans.modules.gradle.java.classpath.AbstractGradleClassPathImpl.addAllFile;

/**
*
* @author lkishalmi
*/
public final class AnnotationProcessorPathImpl extends AbstractSourceSetClassPathImpl {

public AnnotationProcessorPathImpl(Project proj, String sourceSetName) {
super(proj, sourceSetName);
}

@Override
protected List<URL> createPath() {
List<URL> ret = new ArrayList<>();
GradleJavaSourceSet ss = getSourceSet();
if ((ss != null) && (ss.getCompileClassPath() != null)) {
addAllFile(ret, ss.getAnnotationProcessorPath());
}
return ret;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ void processSourceSets() {
}
sourceSet.compileClassPath = (Set<File>) info.get("sourceset_" + name + "_classpath_compile");
sourceSet.runtimeClassPath = (Set<File>) info.get("sourceset_" + name + "_classpath_runtime");
sourceSet.annotationProcessorPath = (Set<File>) info.get("sourceset_" + name + "_classpath_annotation");
sourceSet.compileConfigurationName = (String) info.get("sourceset_" + name + "_configuration_compile");
sourceSet.runtimeConfigurationName = (String) info.get("sourceset_" + name + "_configuration_runtime");
sourceSet.annotationProcessorConfigurationName = (String) info.get("sourceset_" + name + "_configuration_annotation");
sourceSet.outputClassDirs = (Set<File>) info.get("sourceset_" + name + "_output_classes");
sourceSet.outputResources = (File) info.get("sourceset_" + name + "_output_resources");
sourceSet.generatedSourcesDirs = (Set<File>) info.get("sourceset_" + name + "_output_generated");

Map<SourceType, String> sourceComp = new EnumMap<>(SourceType.class);
Map<SourceType, String> targetComp = new EnumMap<>(SourceType.class);
Map<SourceType, List<String>> compilerArgs = new EnumMap<>(SourceType.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class GradleJavaSourceSet implements Serializable {
public static enum SourceType {

JAVA, GROOVY, SCALA, RESOURCES;

ZZ
@Override
public String toString() {
switch (this) {
Expand All @@ -72,14 +72,18 @@ public static enum ClassPathType {
String name;
String runtimeConfigurationName;
String compileConfigurationName;
String annotationProcessorConfigurationName;

Map<SourceType, String> sourcesCompatibility = Collections.emptyMap();
Map<SourceType, String> targetCompatibility = Collections.emptyMap();
Map<SourceType, List<String>> compilerArgs = Collections.emptyMap();
boolean testSourceSet;
Set<File> outputClassDirs;
Set<File> generatedSourcesDirs;
File outputResources;
//Add silent support for webapp docroot.
File webApp;
Set<File> annotationProcessorPath;
Set<File> compileClassPath;
Set<File> runtimeClassPath;
Set<GradleJavaSourceSet> sourceDependencies = Collections.emptySet();
Expand Down Expand Up @@ -160,6 +164,15 @@ public String getCompileConfigurationName() {
return compileConfigurationName;
}

/**
* The name of the annotation processor path configuration.
* @return
* @since 1.7
*/
public String getAnnotationProcessorConfigurationName() {
return annotationProcessorConfigurationName;
}

public Set<File> getSourceDirs(SourceType type) {
Set<File> ret = sources.get(type);
return ret != null ? ret : Collections.<File>emptySet();
Expand Down Expand Up @@ -233,6 +246,18 @@ public Set<File> getRuntimeClassPath() {
return runtimeClassPath != null ? runtimeClassPath : getCompileClassPath();
}

/**
* The annotation processor path configured for this source set. If not
* defined it returns with the compile classpath.
*
* @return the annotation processor path for this sourceset.
* @since 1.7
*/
public Set<File> getAnnotationProcessorPath() {
Set<File> ret = annotationProcessorPath != null ? annotationProcessorPath : Collections.<File>emptySet();
return ret.isEmpty() ? getCompileClassPath() : ret;
}

/**
* Returns the {@link SourceType} of the given file or {@code null} if that
* file cannot be associated with one.
Expand Down Expand Up @@ -275,8 +300,17 @@ public Set<SourceType> getSourceTypes(File f) {
return ret;
}

/**
* Checks if a file most probably belongs to the output of this source set.
* It checks the output class dirs the output resource dir and since 1.7
* the generated source dirs for the file.
*
* @param f a file
* @return true if the file is in one of the output dirs of this source set.
*/
public boolean outputContains(File f) {
List<File> checkList = new LinkedList<>(getOutputClassDirs());
checkList.addAll(getGeneratedSourcesDirs());
if (outputResources != null) {
checkList.add(outputResources);
}
Expand All @@ -292,6 +326,17 @@ public Set<File> getOutputClassDirs() {
return outputClassDirs != null ? outputClassDirs : Collections.<File>emptySet();
}

/**
* The directories where sources are generated, e.g. by annotation processors.
* It only works for builds with Gradle 5.2 and above.
* @return
*
* @since 1.7
*/
public Set<File> getGeneratedSourcesDirs() {
return generatedSourcesDirs != null ? generatedSourcesDirs : Collections.<File>emptySet();
}

/**
* Return the directory of resources output.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public final class ClassPathProviderImpl extends ProjectOpenedHook implements Cl
MODULE_COMPILE_PATH,
MODULE_CLASS_PATH,
MODULE_EXECUTE_PATH,
MODULE_EXECUTE_CLASS_PATH
MODULE_EXECUTE_CLASS_PATH,
MODULE_PROCESSOR_PATH
));


Expand Down Expand Up @@ -168,9 +169,11 @@ private class SourceSetCP {

ClassPath compileTime;
ClassPath runTime;
ClassPath annotationProcessor;

ClassPath moduleBoot;
ClassPath moduleCompile;
ClassPath moduleAnnotationProcessor;
ClassPath moduleLegacy;
ClassPath moduleExecute;
ClassPath moduleLegacyRuntime;
Expand All @@ -195,7 +198,8 @@ public ClassPath getClassPath(String type) {
case MODULE_EXECUTE_PATH: return getModuleExecutePath();
case MODULE_EXECUTE_CLASS_PATH: return getModuleLegacyRuntimeClassPath();

case PROCESSOR_PATH: return getCompileTimeClasspath();
case PROCESSOR_PATH: return getJava8AnnotationProcessorPath();
case MODULE_PROCESSOR_PATH: return getModuleAnnotationProcessorPath();

default: return null;
}
Expand Down Expand Up @@ -278,6 +282,13 @@ private synchronized ClassPath getJava8CompileClassPath() {
return compile;
}

private synchronized ClassPath getJava8AnnotationProcessorPath() {
if (annotationProcessor == null) {
annotationProcessor = ClassPathFactory.createClassPath(new AnnotationProcessorPathImpl(project, group));
}
return annotationProcessor;
}

private synchronized ClassPath getPlatformModulesPath() {
if (platformModules == null) {
platformModules = ClassPathFactory.createClassPath(new BootClassPathImpl(project, true));
Expand Down Expand Up @@ -307,6 +318,14 @@ private synchronized ClassPath getModuleCompilePath() {
return moduleCompile;
}

private synchronized ClassPath getModuleAnnotationProcessorPath() {
if (moduleAnnotationProcessor == null) {
//TODO: This one is pretty identical to the Java8 annotation processor path, maybe it should be removed?
moduleAnnotationProcessor = createMultiplexClassPath(getJava8AnnotationProcessorPath(), getJava8AnnotationProcessorPath());
}
return moduleAnnotationProcessor;
}

private ClassPath createMultiplexClassPath(ClassPath modulePath, ClassPath classPath) {
SourceSetAwareSelector selector = new SourceSetAwareSelector(modulePath, classPath);
selectors.add(selector);
Expand Down
Loading

0 comments on commit 13bac58

Please sign in to comment.