From 48283fc2ed269fa24bbfcaf28ec374dffc3c348d Mon Sep 17 00:00:00 2001 From: Eric Milles Date: Mon, 13 Apr 2020 16:08:36 -0500 Subject: [PATCH] add defensive checks to fix excess logging Java Model Exception: Java Model Status [ does not exist] at org.eclipse.jdt.internal.core.JavaElement.newNotPresentException(JavaElement.java:575) at org.eclipse.jdt.internal.core.JavaModelManager.getPerProjectInfoCheckExistence(JavaModelManager.java:2528) at org.eclipse.jdt.internal.core.JavaProject.getPerProjectInfo(JavaProject.java:2395) at org.eclipse.jdt.internal.core.JavaProject.getOutputLocation(JavaProject.java:2209) at org.eclipse.jdt.internal.launching.RuntimeClasspathEntry.getLocation(RuntimeClasspathEntry.java:517) at org.codehaus.jdt.groovy.internal.compiler.GroovyClassLoaderFactory.getAbsoluteLocation(GroovyClassLoaderFactory.java:207) at org.codehaus.jdt.groovy.internal.compiler.GroovyClassLoaderFactory.calculateClasspath(GroovyClassLoaderFactory.java:191) Java Model Exception: Java Model Status [ does not exist] at org.eclipse.jdt.internal.core.JavaElement.newNotPresentException(JavaElement.java:575) at org.eclipse.jdt.internal.core.JavaModelManager.getPerProjectInfoCheckExistence(JavaModelManager.java:2528) at org.eclipse.jdt.internal.core.JavaProject.getPerProjectInfo(JavaProject.java:2395) at org.eclipse.jdt.internal.core.JavaProject.getRawClasspath(JavaProject.java:2424) at org.codehaus.groovy.eclipse.core.model.GroovyRuntime.findClasspathEntry(GroovyRuntime.java:156) at org.codehaus.groovy.eclipse.actions.AbstractClasspathContainerAction.selectionChanged(AbstractClasspathContainerAction.java:85) --- .../compiler/GroovyClassLoaderFactory.java | 22 +++++++++++-------- .../eclipse/core/model/GroovyRuntime.java | 3 ++- .../AbstractClasspathContainerAction.java | 6 ++--- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/internal/compiler/GroovyClassLoaderFactory.java b/base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/internal/compiler/GroovyClassLoaderFactory.java index 5d2ac00b78..a6c5ad0d3a 100644 --- a/base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/internal/compiler/GroovyClassLoaderFactory.java +++ b/base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/internal/compiler/GroovyClassLoaderFactory.java @@ -48,6 +48,7 @@ import org.eclipse.jdt.core.IClasspathEntry; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.groovy.core.util.ReflectionUtils; import org.eclipse.jdt.internal.compiler.Compiler; import org.eclipse.jdt.internal.compiler.batch.FileSystem; @@ -204,21 +205,24 @@ private static IRuntimeClasspathEntry[] resolveRuntimeClasspathEntry(IRuntimeCla } private static String getAbsoluteLocation(IRuntimeClasspathEntry classpathEntry) { - String location = classpathEntry.getLocation(); + if (classpathEntry.getType() == IRuntimeClasspathEntry.PROJECT) { + try { + // entry.getLocation() logs if project.getOutputLocation() throws, so test it first + ((IJavaProject) JavaCore.create(classpathEntry.getResource())).getOutputLocation(); + } catch (NullPointerException | JavaModelException ignore) { + return classpathEntry.getResource().getLocation().toOSString(); + } + } - Path path = new Path(location); - if (!path.toFile().exists()) { + String location = classpathEntry.getLocation(); + if (!new File(location).exists()) { + IPath path = new Path(location); IProject project = findProject(path.segment(0)); IResource resource = (path.segmentCount() == 1 ? project : project.getFile(path.removeFirstSegments(1))); - - IPath rawLocation = resource.getRawLocation(); - if (rawLocation != null) { - location = rawLocation.toOSString(); - } else if (resource.getLocation() != null) { + if (resource.getLocation() != null) { location = resource.getLocation().toOSString(); } } - return location; } diff --git a/ide/org.codehaus.groovy.eclipse.core/src/org/codehaus/groovy/eclipse/core/model/GroovyRuntime.java b/ide/org.codehaus.groovy.eclipse.core/src/org/codehaus/groovy/eclipse/core/model/GroovyRuntime.java index a652b6dc3d..1f57fe4a4a 100644 --- a/ide/org.codehaus.groovy.eclipse.core/src/org/codehaus/groovy/eclipse/core/model/GroovyRuntime.java +++ b/ide/org.codehaus.groovy.eclipse.core/src/org/codehaus/groovy/eclipse/core/model/GroovyRuntime.java @@ -37,6 +37,7 @@ import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.groovy.core.util.ArrayUtils; import org.eclipse.jdt.internal.core.ClasspathEntry; +import org.eclipse.jdt.internal.core.JavaProject; import org.eclipse.jdt.launching.JavaRuntime; /** @@ -153,7 +154,7 @@ public static void removeClasspathEntry(final IJavaProject javaProject, final IC } public static Optional findClasspathEntry(final IJavaProject javaProject, final Predicate p) throws JavaModelException { - return Arrays.stream(javaProject.getRawClasspath()).filter(p).findFirst(); + return JavaProject.hasJavaNature(javaProject.getProject()) ? Arrays.stream(javaProject.getRawClasspath()).filter(p).findFirst() : Optional.empty(); } //-------------------------------------------------------------------------- diff --git a/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/actions/AbstractClasspathContainerAction.java b/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/actions/AbstractClasspathContainerAction.java index 54bf88c07a..dea3ba89a8 100644 --- a/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/actions/AbstractClasspathContainerAction.java +++ b/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/actions/AbstractClasspathContainerAction.java @@ -67,9 +67,9 @@ public void selectionChanged(final IAction action, final ISelection selection) { if (selection instanceof IStructuredSelection) { Object selected = ((IStructuredSelection) selection).getFirstElement(); if (selected instanceof IProject) { - IProject projSelected = (IProject) selected; - if (GroovyNature.hasGroovyNature(projSelected)) { - targetProject = JavaCore.create(projSelected); + IProject selectedProject = (IProject) selected; + if (GroovyNature.hasGroovyNature(selectedProject)) { + targetProject = JavaCore.create(selectedProject); } } else if (selected instanceof IJavaProject) { IJavaProject selectedProject = (IJavaProject) selected;