geertjanw closed pull request #341: [NETBEANS-228] Attributing content of
anonymous classes whose superty?
URL: https://github.com/apache/incubator-netbeans/pull/341
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/lib.nbjavac/nbproject/project.properties
b/lib.nbjavac/nbproject/project.properties
index 1d921d9d2..39d938e01 100644
--- a/lib.nbjavac/nbproject/project.properties
+++ b/lib.nbjavac/nbproject/project.properties
@@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.
is.autoload=true
-javac.source=1.7
+javac.source=1.8
javac.compilerargs=-Xlint -Xlint:-serial
spec.version.base=1.17.0
requires.nb.javac=true
diff --git a/lib.nbjavac/nbproject/project.xml
b/lib.nbjavac/nbproject/project.xml
index 5be0a1688..494a64ffb 100644
--- a/lib.nbjavac/nbproject/project.xml
+++ b/lib.nbjavac/nbproject/project.xml
@@ -55,6 +55,10 @@
<recursive/>
<compile-dependency/>
</test-dependency>
+ <test-dependency>
+ <code-name-base>org.openide.util</code-name-base>
+ <compile-dependency/>
+ </test-dependency>
</test-type>
</test-dependencies>
<public-packages/>
diff --git a/lib.nbjavac/src/org/netbeans/lib/nbjavac/services/NBAttr.java
b/lib.nbjavac/src/org/netbeans/lib/nbjavac/services/NBAttr.java
index fc667e058..fae511d82 100644
--- a/lib.nbjavac/src/org/netbeans/lib/nbjavac/services/NBAttr.java
+++ b/lib.nbjavac/src/org/netbeans/lib/nbjavac/services/NBAttr.java
@@ -19,10 +19,16 @@
package org.netbeans.lib.nbjavac.services;
import com.sun.tools.javac.comp.Attr;
+import com.sun.tools.javac.comp.AttrContext;
+import com.sun.tools.javac.comp.Env;
import com.sun.tools.javac.tree.JCTree.JCBlock;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
+import com.sun.tools.javac.tree.JCTree.JCNewClass;
import com.sun.tools.javac.util.Context;
+import java.lang.reflect.Field;
+import java.util.logging.Level;
+import java.util.logging.Logger;
/**
*
@@ -63,4 +69,20 @@ public void visitBlock(JCBlock tree) {
super.visitBlock(tree);
}
+ @Override
+ public void visitNewClass(JCNewClass tree) {
+ super.visitNewClass(tree);
+ if (tree.def != null && tree.def.sym == null) {
+ try {
+ Field envField = Attr.class.getDeclaredField("env");
+ envField.setAccessible(true);
+ Env<AttrContext> env = (Env<AttrContext>) envField.get(this);
+ env = env.dup(tree);
+ attribStat(tree.def, env);
+ } catch (NoSuchFieldException | SecurityException |
IllegalArgumentException | IllegalAccessException ex) {
+ Logger.getLogger(NBAttr.class.getName()).log(Level.FINE, null,
ex);
+ }
+ }
+ }
+
}
diff --git
a/lib.nbjavac/test/unit/src/org/netbeans/lib/nbjavac/services/NBAttrTest.java
b/lib.nbjavac/test/unit/src/org/netbeans/lib/nbjavac/services/NBAttrTest.java
new file mode 100644
index 000000000..5ab20625a
--- /dev/null
+++
b/lib.nbjavac/test/unit/src/org/netbeans/lib/nbjavac/services/NBAttrTest.java
@@ -0,0 +1,109 @@
+/*
+ * 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.lib.nbjavac.services;
+
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.MethodInvocationTree;
+import com.sun.source.util.JavacTask;
+import com.sun.source.util.TreePathScanner;
+import com.sun.source.util.Trees;
+import com.sun.tools.javac.api.JavacTaskImpl;
+import com.sun.tools.javac.api.JavacTool;
+import com.sun.tools.javac.util.Context;
+import java.io.File;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Collections;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.StandardLocation;
+import javax.tools.ToolProvider;
+import org.netbeans.junit.NbTestCase;
+
+import static org.netbeans.lib.nbjavac.services.Utilities.DEV_NULL;
+import org.openide.util.Pair;
+
+/**
+ *
+ * @author lahvac
+ */
+public class NBAttrTest extends NbTestCase {
+
+ public NBAttrTest(String testName) {
+ super(testName);
+ }
+
+ public void testNETBEANS_228() throws Exception {
+ String code = "package test; public class Test { Object t() { return
new Undef() { public void test() { test(); } }; } }";
+ Pair<JavacTask, CompilationUnitTree> parsed = compile(code);
+
+ new TreePathScanner<Void, Void>() {
+ public Void visitMethodInvocation(MethodInvocationTree tree, Void
p) {
+ Trees trees = Trees.instance(parsed.first());
+ assertNotNull(trees.getElement(getCurrentPath()));
+ return super.visitMethodInvocation(tree, p);
+ }
+ }.scan(parsed.second(), null);
+ }
+
+ //<editor-fold defaultstate="collapsed" desc=" Test Infrastructure ">
+ private static class MyFileObject extends SimpleJavaFileObject {
+ private String text;
+
+ public MyFileObject(String text) {
+ super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
+ this.text = text;
+ }
+
+ @Override
+ public CharSequence getCharContent(boolean ignoreEncodingErrors) {
+ return text;
+ }
+ }
+
+ private File workingDir;
+
+ @Override
+ protected void setUp() throws Exception {
+ workingDir = getWorkDir();
+ }
+
+ private Pair<JavacTask, CompilationUnitTree> compile(String code) throws
Exception {
+ final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
+ assert tool != null;
+
+ StandardJavaFileManager std = tool.getStandardFileManager(null, null,
null);
+
+ std.setLocation(StandardLocation.CLASS_OUTPUT,
Collections.singleton(workingDir));
+
+ Context context = new Context();
+ NBMessager.preRegister(context, null, DEV_NULL, DEV_NULL, DEV_NULL);
+ NBAttr.preRegister(context);
+ final JavacTaskImpl ct = (JavacTaskImpl)
((JavacTool)tool).getTask(null, std, null, Arrays.asList("-source", "1.6",
"-target", "1.6"), null, Arrays.asList(new MyFileObject(code)), context);
+
+ CompilationUnitTree cut = ct.parse().iterator().next();
+
+ ct.analyze();
+
+ return Pair.<JavacTask, CompilationUnitTree>of(ct, cut);
+ }
+ //</editor-fold>
+}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
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