This is an automated email from the ASF dual-hosted git repository.
neilcsmith pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/master by this push:
new aa44bca [NETBEANS-3191] ensuring ElementHandle works even for
CompilationInfos that are based on classfiles (and hence don't have a
CompilationUnitTree).
new 27a09aa Merge pull request #1542 from jlahoda/NETBEANS-3191
aa44bca is described below
commit aa44bca26f792c0930f2845324216a5927de48d1
Author: Jan Lahoda <[email protected]>
AuthorDate: Wed Oct 2 18:44:59 2019 +0200
[NETBEANS-3191] ensuring ElementHandle works even for CompilationInfos that
are based on classfiles (and hence don't have a CompilationUnitTree).
---
.../netbeans/api/java/source/ElementHandle.java | 17 +++++--
.../api/java/source/ElementHandleTest.java | 55 ++++++++++++++++++++++
2 files changed, 69 insertions(+), 3 deletions(-)
diff --git
a/java/java.source.base/src/org/netbeans/api/java/source/ElementHandle.java
b/java/java.source.base/src/org/netbeans/api/java/source/ElementHandle.java
index a6211c9..8922585 100644
--- a/java/java.source.base/src/org/netbeans/api/java/source/ElementHandle.java
+++ b/java/java.source.base/src/org/netbeans/api/java/source/ElementHandle.java
@@ -114,9 +114,20 @@ public final class ElementHandle<T extends Element> {
@SuppressWarnings ("unchecked") // NOI18N
public @CheckForNull T resolve (@NonNull final CompilationInfo
compilationInfo) {
Parameters.notNull("compilationInfo", compilationInfo); // NOI18N
- ModuleElement module = compilationInfo.getFileObject() != null
- ?
((JCTree.JCCompilationUnit)compilationInfo.getCompilationUnit()).modle
- : null;
+ ModuleElement module;
+
+ if (compilationInfo.getFileObject() != null) {
+ JCTree.JCCompilationUnit cut =
(JCTree.JCCompilationUnit)compilationInfo.getCompilationUnit();
+ if (cut != null) {
+ module = cut.modle;
+ } else if
(compilationInfo.getTopLevelElements().iterator().hasNext()) {
+ module = ((Symbol)
compilationInfo.getTopLevelElements().iterator().next()).packge().modle;
+ } else {
+ module = null;
+ }
+ } else {
+ module = null;
+ }
T result = resolveImpl (module, compilationInfo.impl.getJavacTask());
if (result == null) {
if (log.isLoggable(Level.INFO))
diff --git
a/java/java.source.base/test/unit/src/org/netbeans/api/java/source/ElementHandleTest.java
b/java/java.source.base/test/unit/src/org/netbeans/api/java/source/ElementHandleTest.java
index b5e3268..5e2c51d 100644
---
a/java/java.source.base/test/unit/src/org/netbeans/api/java/source/ElementHandleTest.java
+++
b/java/java.source.base/test/unit/src/org/netbeans/api/java/source/ElementHandleTest.java
@@ -27,11 +27,13 @@ import java.io.PrintWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.HashMap;
import java.util.HashSet;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import junit.framework.*;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
@@ -41,13 +43,16 @@ import javax.lang.model.element.TypeElement;
import javax.lang.model.element.TypeParameterElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
+import javax.lang.model.util.Elements;
import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.junit.NbTestCase;
+import org.netbeans.modules.java.source.BootClassPathUtil;
import org.netbeans.modules.java.source.ElementUtils;
import org.netbeans.modules.java.source.TestUtil;
import org.netbeans.modules.java.source.usages.IndexUtil;
import org.netbeans.spi.java.classpath.ClassPathProvider;
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
+import org.netbeans.spi.java.queries.SourceLevelQueryImplementation;
import org.openide.filesystems.FileLock;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
@@ -85,6 +90,7 @@ public class ElementHandleTest extends NbTestCase {
Lookups.metaInfServices(l),
Lookups.singleton(l),
Lookups.singleton(ClassPathProviderImpl.getDefault()),
+ Lookups.singleton(SourceLevelQueryImpl.getDefault()),
});
}
@@ -607,6 +613,26 @@ public class ElementHandleTest extends NbTestCase {
}, true);
}
+ public void testHandleClassBasedCompilations() throws Exception {
+ ClassPath systemClasses = BootClassPathUtil.getModuleBootPath();
+ FileObject jlObject =
systemClasses.findResource("java/lang/Object.class");
+ assertNotNull(jlObject);
+ ClasspathInfo cpInfo = new
ClasspathInfo.Builder(BootClassPathUtil.getBootClassPath())
+
.setModuleBootPath(systemClasses)
+ .build();
+ JavaSource js = JavaSource.create(cpInfo, jlObject);
+ assertNotNull(js);
+ SourceLevelQueryImpl.getDefault().setSourceLevel(jlObject, "11");
+ js.runUserActionTask(cc -> {
+ cc.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
+ ElementHandle.create(cc.getTopLevelElements().get(0)).resolve(cc);
+ Elements elements = cc.getElements();
+ TypeElement te = elements.getTypeElement("java.lang.String");
+ ElementHandle.create(te).resolve(cc);
+ }, true);
+ SourceLevelQueryImpl.getDefault().setSourceLevel(jlObject, null);
+ }
+
private Element[] getStringElements (Element stringElement) {
List<? extends Element> members =
((TypeElement)stringElement).getEnclosedElements();
Element[] result = new Element[3];
@@ -690,6 +716,35 @@ public class ElementHandleTest extends NbTestCase {
}
}
+ private static class SourceLevelQueryImpl implements
SourceLevelQueryImplementation {
+
+ private static SourceLevelQueryImpl instance;
+
+ private final Map<FileObject, String> sourceLevels = new HashMap<>();
+
+ private SourceLevelQueryImpl() {}
+
+ @Override
+ public synchronized String getSourceLevel(FileObject javaFile) {
+ return sourceLevels.get(javaFile);
+ }
+
+ public synchronized void setSourceLevel(FileObject file, String
sourceLevel) {
+ if (sourceLevel != null) {
+ sourceLevels.put(file, sourceLevel);
+ } else {
+ sourceLevels.remove(file);
+ }
+ }
+
+ public static synchronized SourceLevelQueryImpl getDefault () {
+ if (instance == null) {
+ instance = new SourceLevelQueryImpl();
+ }
+ return instance;
+ }
+ }
+
static {
System.setProperty("CachingArchiveProvider.disableCtSym", "true");
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists