jlahoda closed pull request #1074: Have only one copy of TreeShims and copy it to java.hints and java.co… URL: https://github.com/apache/incubator-netbeans/pull/1074
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/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 267a3ef2f4..0000000000 --- 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 0000000000..3633bdb386 --- /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"); + }}; +} ---------------------------------------------------------------- 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
