This is an automated email from the ASF dual-hosted git repository. lkishalmi pushed a commit to branch release110-gradle-patch-1 in repository https://gitbox.apache.org/repos/asf/netbeans.git
commit a12d7ce749703bab29c27d3be0b61f47fcb0a998 Author: Laszlo Kishalmi <laszlo.kisha...@gmail.com> AuthorDate: Sat Apr 6 15:23:06 2019 -0700 [NETBEANS-2293] Fix possible NPE on selected method detection. --- .../gradle/java/GradleJavaTokenProvider.java | 86 ++++++---------------- .../modules/gradle/java/action-mapping.xml | 4 +- .../gradle/java/api/output/LocationOpener.java | 58 +++++++-------- 3 files changed, 52 insertions(+), 96 deletions(-) diff --git a/groovy/gradle.java/src/org/netbeans/modules/gradle/java/GradleJavaTokenProvider.java b/groovy/gradle.java/src/org/netbeans/modules/gradle/java/GradleJavaTokenProvider.java index 87018fb..8e79ed4 100644 --- a/groovy/gradle.java/src/org/netbeans/modules/gradle/java/GradleJavaTokenProvider.java +++ b/groovy/gradle.java/src/org/netbeans/modules/gradle/java/GradleJavaTokenProvider.java @@ -24,24 +24,13 @@ import org.netbeans.modules.gradle.api.execute.RunUtils; import org.netbeans.modules.gradle.java.api.GradleJavaProject; import org.netbeans.modules.gradle.java.api.GradleJavaSourceSet; import org.netbeans.modules.gradle.spi.actions.ReplaceTokenProvider; -import com.sun.source.tree.CompilationUnitTree; -import com.sun.source.tree.Tree; -import com.sun.source.util.Trees; import java.io.File; -import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; -import javax.lang.model.element.Element; -import javax.lang.model.element.ElementKind; -import javax.lang.model.element.TypeElement; -import org.netbeans.api.java.source.CompilationController; -import org.netbeans.api.java.source.JavaSource; -import org.netbeans.api.java.source.Task; import org.netbeans.api.project.Project; import org.netbeans.spi.project.ProjectServiceProvider; import org.netbeans.spi.project.SingleMethod; @@ -85,32 +74,20 @@ public class GradleJavaTokenProvider implements ReplaceTokenProvider { private void processSelectedPackageAndClass(final Map<String, String> map, Lookup context) { FileObject fo = RunUtils.extractFileObjectfromLookup(context); GradleJavaProject gjp = GradleJavaProject.get(project); - if ((gjp != null) && (fo != null)) { - File f = FileUtil.toFile(fo); - GradleJavaSourceSet sourceSet = gjp.containingSourceSet(f); - if (sourceSet != null) { - if (f.isFile()) { - String relPath = sourceSet.relativePath(f); - String className = (relPath.lastIndexOf('.') > 0 ? - relPath.substring(0, relPath.lastIndexOf('.')) : - relPath).replace('/', '.'); - map.put("selectedClass", className); //NOI18N - f = f.getParentFile(); - } else { - String pkg = sourceSet.relativePath(f).replace('/', '.'); - map.put("selectedClass", pkg + "*"); //NOI18N - } - } + String className = evaluateClassName(gjp, fo); + if (className != null) { + map.put("selectedClass", className); } } - private static void processSelectedMethod(final Map<String, String> map, Lookup context) { + private void processSelectedMethod(final Map<String, String> map, Lookup context) { SingleMethod method = context.lookup(SingleMethod.class); FileObject fo = method != null ? method.getFile() : RunUtils.extractFileObjectfromLookup(context); - String methodName = method != null ? method.getMethodName() : null; - if (fo != null) { - String selectedMethod = evaluateSingleMethod(fo, methodName); - map.put("selectedMethod", selectedMethod); //NOI18N + if ((fo != null) && fo.isData()) { + GradleJavaProject gjp = GradleJavaProject.get(project); + String className = evaluateClassName(gjp, fo); + String selectedMethod = method != null ? className + '.' + method.getMethodName() : className; + map.put("selectedMethod", selectedMethod); } } @@ -137,37 +114,22 @@ public class GradleJavaTokenProvider implements ReplaceTokenProvider { } } - private static String evaluateSingleMethod(final FileObject fo, final String method) { - final Object[] ret = new Object[1]; - JavaSource javaSource = JavaSource.forFileObject(fo); - if (javaSource != null) { - try { - javaSource.runUserActionTask(new Task<CompilationController>() { - @Override - public void run(CompilationController compilationController) throws Exception { - compilationController.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); - Trees trees = compilationController.getTrees(); - CompilationUnitTree compilationUnitTree = compilationController.getCompilationUnit(); - List<? extends Tree> typeDecls = compilationUnitTree.getTypeDecls(); - for (Tree tree : typeDecls) { - Element element = trees.getElement(trees.getPath(compilationUnitTree, tree)); - if (element != null && element.getKind() == ElementKind.CLASS && element.getSimpleName().contentEquals(fo.getName())) { - TypeElement type = (TypeElement) element; - StringBuilder sb = new StringBuilder(type.getQualifiedName()); - if (method != null) { - sb.append('.').append(method); - } - ret[0] = sb.toString(); - break; - } - } - } - }, true); - return ret[0].toString(); - } catch (IOException ioe) { - //TODO: Do nothing? + private String evaluateClassName(GradleJavaProject gjp, FileObject fo) { + String ret = null; + if ((gjp != null) && (fo != null)) { + File f = FileUtil.toFile(fo); + GradleJavaSourceSet sourceSet = gjp.containingSourceSet(f); + if (sourceSet != null) { + String relPath = sourceSet.relativePath(f); + ret = (relPath.lastIndexOf('.') > 0 ? + relPath.substring(0, relPath.lastIndexOf('.')) : + relPath).replace('/', '.'); + if (fo.isFolder()) { + ret = ret + '*'; + } } } - return null; + return ret; } + } diff --git a/groovy/gradle.java/src/org/netbeans/modules/gradle/java/action-mapping.xml b/groovy/gradle.java/src/org/netbeans/modules/gradle/java/action-mapping.xml index 32d303a..3a616bd 100644 --- a/groovy/gradle.java/src/org/netbeans/modules/gradle/java/action-mapping.xml +++ b/groovy/gradle.java/src/org/netbeans/modules/gradle/java/action-mapping.xml @@ -40,12 +40,12 @@ </action> <action name="debug.fix" repeatable="false"> - <args>--offline --no-rebuild ${affectedBuildTasks}</args> + <args>--offline ${affectedBuildTasks}</args> <reload rule="NEVER"/> </action> <action name="compile.single" repetable="false"> - <args>--offline --no-rebuild ${affectedBuildTasks}</args> + <args>--offline ${affectedBuildTasks}</args> <reload rule="NEVER"/> </action> diff --git a/groovy/gradle.java/src/org/netbeans/modules/gradle/java/api/output/LocationOpener.java b/groovy/gradle.java/src/org/netbeans/modules/gradle/java/api/output/LocationOpener.java index 7cfc359..0974c80 100644 --- a/groovy/gradle.java/src/org/netbeans/modules/gradle/java/api/output/LocationOpener.java +++ b/groovy/gradle.java/src/org/netbeans/modules/gradle/java/api/output/LocationOpener.java @@ -85,23 +85,20 @@ public final class LocationOpener { JavaSource javaSource = JavaSource.forFileObject(fo); if (javaSource != null) { try { - javaSource.runUserActionTask(new Task<CompilationController>() { - @Override - public void run(CompilationController compilationController) throws Exception { - compilationController.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); - Trees trees = compilationController.getTrees(); - CompilationUnitTree compilationUnitTree = compilationController.getCompilationUnit(); - List<? extends Tree> typeDecls = compilationUnitTree.getTypeDecls(); - for (Tree tree : typeDecls) { - Element element = trees.getElement(trees.getPath(compilationUnitTree, tree)); - if (element != null && element.getKind() == ElementKind.CLASS && element.getSimpleName().contentEquals(fo.getName())) { - List<? extends ExecutableElement> methodElements = ElementFilter.methodsIn(element.getEnclosedElements()); - for (Element child : methodElements) { - if (child.getSimpleName().contentEquals(methodName)) { - long pos = trees.getSourcePositions().getStartPosition(compilationUnitTree, trees.getTree(child)); - line[0] = (int) compilationUnitTree.getLineMap().getLineNumber(pos); - break; - } + javaSource.runUserActionTask((CompilationController compilationController) -> { + compilationController.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); + Trees trees = compilationController.getTrees(); + CompilationUnitTree compilationUnitTree = compilationController.getCompilationUnit(); + List<? extends Tree> typeDecls = compilationUnitTree.getTypeDecls(); + for (Tree tree : typeDecls) { + Element element = trees.getElement(trees.getPath(compilationUnitTree, tree)); + if (element != null && element.getKind() == ElementKind.CLASS && element.getSimpleName().contentEquals(fo.getName())) { + List<? extends ExecutableElement> methodElements = ElementFilter.methodsIn(element.getEnclosedElements()); + for (Element child : methodElements) { + if (child.getSimpleName().contentEquals(methodName)) { + long pos = trees.getSourcePositions().getStartPosition(compilationUnitTree, trees.getTree(child)); + line[0] = (int) compilationUnitTree.getLineMap().getLineNumber(pos); + break; } } } @@ -120,20 +117,17 @@ public final class LocationOpener { JavaSource javaSource = JavaSource.forFileObject(fo); if (javaSource != null) { try { - javaSource.runUserActionTask(new Task<CompilationController>() { - @Override - public void run(CompilationController compilationController) throws Exception { - compilationController.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); - Trees trees = compilationController.getTrees(); - CompilationUnitTree compilationUnitTree = compilationController.getCompilationUnit(); - List<? extends Tree> typeDecls = compilationUnitTree.getTypeDecls(); - for (Tree tree : typeDecls) { - Element element = trees.getElement(trees.getPath(compilationUnitTree, tree)); - if (element != null && element.getKind() == ElementKind.CLASS && element.getSimpleName().contentEquals(fo.getName())) { - long pos = trees.getSourcePositions().getStartPosition(compilationUnitTree, tree); - line[0] = (int) compilationUnitTree.getLineMap().getLineNumber(pos); - break; - } + javaSource.runUserActionTask((CompilationController compilationController) -> { + compilationController.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); + Trees trees = compilationController.getTrees(); + CompilationUnitTree compilationUnitTree = compilationController.getCompilationUnit(); + List<? extends Tree> typeDecls = compilationUnitTree.getTypeDecls(); + for (Tree tree : typeDecls) { + Element element = trees.getElement(trees.getPath(compilationUnitTree, tree)); + if (element != null && element.getKind() == ElementKind.CLASS && element.getSimpleName().contentEquals(fo.getName())) { + long pos = trees.getSourcePositions().getStartPosition(compilationUnitTree, tree); + line[0] = (int) compilationUnitTree.getLineMap().getLineNumber(pos); + break; } } }, true); @@ -147,7 +141,7 @@ public final class LocationOpener { public static void openAtLine(FileObject file, final int line) { openAtLine(file, line, false); } - + public static void openAtLine(FileObject file, final int line, final boolean reuse) { try { DataObject data = DataObject.find(file); --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org For additional commands, e-mail: commits-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists