You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by jl...@apache.org on 2019/01/12 07:33:30 UTC
[incubator-netbeans] branch master updated: Have only one copy of
TreeShims and copy it to java.hints and java.completion.
This is an automated email from the ASF dual-hosted git repository.
jlahoda pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-netbeans.git
The following commit(s) were added to refs/heads/master by this push:
new c338e76 Have only one copy of TreeShims and copy it to java.hints and java.completion.
c338e76 is described below
commit c338e7656cd0d56c98ba4a7142628045f979b58b
Author: Jan Lahoda <jl...@netbeans.org>
AuthorDate: Sun Jan 6 08:29:10 2019 +0100
Have only one copy of TreeShims and copy it to java.hints and java.completion.
---
.../org/netbeans/modules/java/hints/TreeShims.java | 57 ------------
.../modules/java/source/TreeShimsCopier.java | 100 +++++++++++++++++++++
2 files changed, 100 insertions(+), 57 deletions(-)
diff --git a/java/java.hints/src/org/netbeans/modules/java/hints/TreeShims.java b/java/java.hints/src/org/netbeans/modules/java/hints/TreeShims.java
deleted file mode 100644
index 267a3ef..0000000
--- a/java/java.hints/src/org/netbeans/modules/java/hints/TreeShims.java
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * 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.java.hints;
-
-import com.sun.source.tree.CaseTree;
-import com.sun.source.tree.ExpressionTree;
-import com.sun.source.tree.Tree;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.util.Collections;
-import java.util.List;
-
-public class TreeShims {
-
- public static List<? extends ExpressionTree> getExpressions(CaseTree node) {
- try {
- Method getExpressions = CaseTree.class.getDeclaredMethod("getExpressions");
- return (List<? extends ExpressionTree>) getExpressions.invoke(node);
- } catch (NoSuchMethodException ex) {
- return Collections.singletonList(node.getExpression());
- } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
- throw TreeShims.<RuntimeException>throwAny(ex);
- }
- }
-
- public static Tree getBody(CaseTree node) {
- try {
- Method getBody = CaseTree.class.getDeclaredMethod("getBody");
- return (Tree) getBody.invoke(node);
- } catch (NoSuchMethodException ex) {
- return null;
- } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
- throw TreeShims.<RuntimeException>throwAny(ex);
- }
- }
-
- @SuppressWarnings("unchecked")
- private static <T extends Throwable> RuntimeException throwAny(Throwable t) throws T {
- throw (T) t;
- }
-}
diff --git a/java/java.source.base/src/org/netbeans/modules/java/source/TreeShimsCopier.java b/java/java.source.base/src/org/netbeans/modules/java/source/TreeShimsCopier.java
new file mode 100644
index 0000000..3633bdb
--- /dev/null
+++ b/java/java.source.base/src/org/netbeans/modules/java/source/TreeShimsCopier.java
@@ -0,0 +1,100 @@
+/*
+ * 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.java.source;
+
+import com.sun.source.tree.CaseTree;
+import com.sun.source.tree.ExpressionTree;
+import com.sun.source.tree.Tree;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.URI;
+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.annotation.processing.AbstractProcessor;
+import javax.annotation.processing.Filer;
+import javax.annotation.processing.Processor;
+import javax.annotation.processing.RoundEnvironment;
+import javax.annotation.processing.SupportedAnnotationTypes;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.ElementKind;
+import javax.lang.model.element.PackageElement;
+import javax.lang.model.element.TypeElement;
+import javax.tools.FileObject;
+import javax.tools.StandardLocation;
+import org.openide.util.lookup.ServiceProvider;
+
+@ServiceProvider(service=Processor.class)
+@SupportedAnnotationTypes("*")
+public class TreeShimsCopier extends AbstractProcessor {
+
+ @Override
+ public boolean process(Set<? extends TypeElement> annos, RoundEnvironment roundEnv) {
+ for (Element el : roundEnv.getRootElements()) {
+ if (el.getKind() != ElementKind.CLASS)
+ continue;
+ TypeElement type = (TypeElement) el;
+ String qualName = type.getQualifiedName().toString();
+ String targetPackage = ALLOWED_CLASSES2TARGET_PACKAGE.get(qualName);
+ if (targetPackage != null) {
+ try {
+ Filer filer = processingEnv.getFiler();
+ FileObject fo = filer.getResource(StandardLocation.SOURCE_PATH, ((PackageElement) type.getEnclosingElement()).getQualifiedName().toString(), type.getSimpleName() + ".java");
+ URI source = fo.toUri();
+ StringBuilder path2Shims = new StringBuilder();
+ int p = qualName.split("\\.").length;
+ for (int i = 0; i < p; i++) {
+ path2Shims.append("../");
+ }
+ path2Shims.append("../java.source.base/src/org/netbeans/modules/java/source/TreeShims.java");
+ URI treeShims = source.resolve(path2Shims.toString());
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ try (InputStream in = treeShims.toURL().openStream()) {
+ int r;
+
+ while ((r = in.read()) != (-1)) {
+ baos.write(r);
+ }
+ }
+ String content = new String(baos.toByteArray(), "UTF-8");
+ content = content.replace("package org.netbeans.modules.java.source;", "package " + targetPackage + ";");
+ try (OutputStream out = filer.createSourceFile(targetPackage + ".TreeShims", type).openOutputStream()) {
+ out.write(content.getBytes("UTF-8"));
+ }
+ } catch (IOException ex) {
+ throw new IllegalStateException(ex);
+ }
+ }
+ }
+ return false;
+ }
+
+ private static final Map<String, String> ALLOWED_CLASSES2TARGET_PACKAGE = new HashMap<String, String>() {{
+ put("org.netbeans.modules.java.hints.infrastructure.ErrorHintsProvider", "org.netbeans.modules.java.hints");
+ put("org.netbeans.modules.java.completion.JavaCompletionTask", "org.netbeans.modules.java.completion");
+ }};
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists