From 4911d3109c67a7f99fb651d26640f681c5c493a4 Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Wed, 27 Sep 2023 05:36:38 +0200 Subject: [PATCH] Remove TreeShims mechanism. The class was mostly empty at this point and was copied around using an annotation processor. --- .../java/editor/base/semantic/Utilities.java | 5 +- .../modules/editor/java/GoToSupport.java | 2 +- .../jdk/ConvertToSwitchPatternInstanceOf.java | 21 +- .../ConvertToSwitchPatternInstanceOfTest.java | 1 - .../modules/java/source/TreeShims.java | 494 ------------------ .../modules/java/source/TreeShimsCopier.java | 106 ---- .../modules/java/source/save/CasualDiff.java | 5 +- .../transform/ImmutableDocTreeTranslator.java | 1 - 8 files changed, 15 insertions(+), 620 deletions(-) delete mode 100644 java/java.source.base/src/org/netbeans/modules/java/source/TreeShims.java delete mode 100644 java/java.source.base/src/org/netbeans/modules/java/source/TreeShimsCopier.java diff --git a/java/java.editor.base/src/org/netbeans/modules/java/editor/base/semantic/Utilities.java b/java/java.editor.base/src/org/netbeans/modules/java/editor/base/semantic/Utilities.java index f04f7b8fa6a3..0901d96e3fd8 100644 --- a/java/java.editor.base/src/org/netbeans/modules/java/editor/base/semantic/Utilities.java +++ b/java/java.editor.base/src/org/netbeans/modules/java/editor/base/semantic/Utilities.java @@ -26,7 +26,6 @@ import com.sun.source.tree.CompilationUnitTree; import com.sun.source.tree.ContinueTree; import com.sun.source.tree.ExpressionTree; -import com.sun.source.tree.InstanceOfTree; import com.sun.source.tree.LabeledStatementTree; import com.sun.source.tree.MemberReferenceTree; import com.sun.source.tree.MemberSelectTree; @@ -667,11 +666,11 @@ public static boolean isPrivateElement(Element el) { } public static Element toRecordComponent(Element el) { - if (el == null ||el.getKind() != ElementKind.FIELD) { + if (el == null || el.getKind() != ElementKind.FIELD) { return el; } TypeElement owner = (TypeElement) el.getEnclosingElement(); - if (!"RECORD".equals(owner.getKind().name())) { + if (!ElementKind.RECORD.equals(owner.getKind())) { return el; } for (Element encl : owner.getEnclosedElements()) { diff --git a/java/java.editor/src/org/netbeans/modules/editor/java/GoToSupport.java b/java/java.editor/src/org/netbeans/modules/editor/java/GoToSupport.java index 95f30da04e2b..8a60126a7266 100644 --- a/java/java.editor/src/org/netbeans/modules/editor/java/GoToSupport.java +++ b/java/java.editor/src/org/netbeans/modules/editor/java/GoToSupport.java @@ -881,7 +881,7 @@ private boolean process() { } private boolean process(TreePath path) { - Element resolved = TreeShims.toRecordComponent(info.getTrees().getElement(path)); + Element resolved = org.netbeans.modules.java.editor.base.semantic.Utilities.toRecordComponent(info.getTrees().getElement(path)); if (toFind.equals(resolved)) { found = getCurrentPath(); return true; diff --git a/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOf.java b/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOf.java index 85f133e549dc..a295ed153f65 100644 --- a/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOf.java +++ b/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOf.java @@ -21,8 +21,6 @@ import com.sun.source.tree.BlockTree; import com.sun.source.tree.CaseTree; import com.sun.source.tree.ExpressionStatementTree; -import com.sun.source.tree.ExpressionTree; -import com.sun.source.tree.IdentifierTree; import com.sun.source.tree.IfTree; import com.sun.source.tree.InstanceOfTree; import com.sun.source.tree.ParenthesizedTree; @@ -32,8 +30,6 @@ import com.sun.source.tree.Tree; import com.sun.source.tree.VariableTree; import com.sun.source.util.TreePath; -import com.sun.tools.javac.tree.JCTree; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collections; import java.util.EnumSet; @@ -41,12 +37,10 @@ import java.util.List; import java.util.Set; import javax.lang.model.element.Modifier; -import javax.lang.model.element.Name; import javax.lang.model.type.TypeMirror; import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.api.java.source.TreeMaker; import org.netbeans.api.java.source.WorkingCopy; -import org.netbeans.modules.java.source.TreeShims; import org.netbeans.spi.editor.hints.ErrorDescription; import org.netbeans.spi.editor.hints.Fix; import org.netbeans.spi.java.hints.ErrorDescriptionFactory; @@ -57,7 +51,6 @@ import org.netbeans.spi.java.hints.TriggerPattern; import org.netbeans.spi.java.hints.TriggerPatterns; import org.netbeans.spi.java.hints.TriggerTreeKind; -import org.openide.util.Exceptions; import org.openide.util.NbBundle; /** @@ -274,9 +267,7 @@ protected void performRewrite(JavaFix.TransformationContext ctx) { public static ErrorDescription switchPatternMatchToSwitchNull(HintContext ctx) { SwitchTree switchTree = (SwitchTree) ctx.getPath().getLeaf(); - boolean isPatternMatch = false; - isPatternMatch = TreeShims.isPatternMatch(switchTree); - if (!isPatternMatch) { + if (!isPatternMatch(switchTree)) { return null; } Tree expression = ((ParenthesizedTree) switchTree.getExpression()).getExpression(); @@ -297,6 +288,16 @@ public static ErrorDescription switchPatternMatchToSwitchNull(HintContext ctx) { } + public static boolean isPatternMatch(Tree node) { + try { + return node.getClass().getField("patternSwitch").getBoolean(node); + } catch (NoSuchFieldException e){ + return false; + } catch (IllegalArgumentException | IllegalAccessException | SecurityException ex) { + throw new RuntimeException(ex); + } + } + private static final class FixSwitchPatternMatchToSwitchNull extends JavaFix { private final int indexOf; diff --git a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOfTest.java b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOfTest.java index f15dc6351885..4e25857d72f4 100644 --- a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOfTest.java +++ b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOfTest.java @@ -20,7 +20,6 @@ import org.netbeans.junit.NbTestCase; import org.netbeans.modules.java.hints.test.api.HintTest; -import javax.lang.model.SourceVersion; import org.testng.annotations.Test; /** diff --git a/java/java.source.base/src/org/netbeans/modules/java/source/TreeShims.java b/java/java.source.base/src/org/netbeans/modules/java/source/TreeShims.java deleted file mode 100644 index 1702191252f2..000000000000 --- a/java/java.source.base/src/org/netbeans/modules/java/source/TreeShims.java +++ /dev/null @@ -1,494 +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.source; - -import com.sun.source.doctree.DocTree; -import com.sun.source.doctree.TextTree; -import com.sun.source.doctree.ReferenceTree; -import com.sun.source.tree.BreakTree; -import com.sun.source.tree.CaseTree; -import com.sun.source.tree.ClassTree; -import com.sun.source.tree.CompilationUnitTree; -import com.sun.source.tree.ExpressionTree; -import com.sun.source.tree.InstanceOfTree; -import com.sun.source.tree.ModuleTree; -import com.sun.source.tree.SwitchTree; -import com.sun.source.tree.Tree; -import com.sun.source.tree.VariableTree; -import com.sun.tools.javac.tree.DocTreeMaker; -import com.sun.tools.javac.tree.JCTree; -import com.sun.tools.javac.tree.JCTree.JCClassDecl; -import com.sun.tools.javac.util.Names; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import javax.lang.model.SourceVersion; -import javax.lang.model.element.Name; -import javax.lang.model.element.Element; -import javax.lang.model.element.ElementKind; -import javax.lang.model.element.TypeElement; -import javax.lang.model.util.ElementFilter; - -public class TreeShims { - -// public static final String BINDING_PATTERN = "BINDING_PATTERN"; //NOI18N -// public static final String SWITCH_EXPRESSION = "SWITCH_EXPRESSION"; //NOI18N -// public static final String YIELD = "YIELD"; //NOI18N -// public static final String BINDING_VARIABLE = "BINDING_VARIABLE"; //NOI18N -// public static final String RECORD = "RECORD"; //NOI18N -// public static final int PATTERN_MATCHING_INSTANCEOF_PREVIEW_JDK_VERSION = 15; //NOI18N -// public static final String DEFAULT_CASE_LABEL = "DEFAULT_CASE_LABEL"; //NOI18N -// public static final String NULL_LITERAL = "NULL_LITERAL"; //NOI18N -// public static final String PARENTHESIZED_PATTERN = "PARENTHESIZED_PATTERN"; //NOI18N -// public static final String GUARDED_PATTERN = "GUARDED_PATTERN"; //NOI18N -// public static final String DECONSTRUCTION_PATTERN = "DECONSTRUCTION_PATTERN"; -// public static final String RECORDPATTERN = "RECORDPATTERN"; -// -// public static List getExpressions(CaseTree node) { -// try { -// Method getExpressions = CaseTree.class.getDeclaredMethod("getExpressions"); -// return (List) getExpressions.invoke(node); -// } catch (NoSuchMethodException ex) { -// return Collections.singletonList(node.getExpression()); -// } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { -// throw TreeShims.throwAny(ex); -// } -// } -// -// public static List getLabels(CaseTree node) { -// try { -// Method getLabels = CaseTree.class.getDeclaredMethod("getLabels"); -// return (List) getLabels.invoke(node); -// } catch (NoSuchMethodException ex) { -// return null; -// } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { -// throw TreeShims.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.throwAny(ex); -// } -// } -// -// public static boolean isRuleCase(CaseTree node) { -// try { -// Method getCaseKind = CaseTree.class.getDeclaredMethod("getCaseKind"); -// return "RULE".equals(String.valueOf(getCaseKind.invoke(node))); -// } catch (NoSuchMethodException ex) { -// return false; -// } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { -// throw TreeShims.throwAny(ex); -// } -// } -// -// public static Tree getPattern(InstanceOfTree node) { -// try { -// Method getPattern = InstanceOfTree.class.getDeclaredMethod("getPattern"); -// return (Tree) getPattern.invoke(node); -// } catch (NoSuchMethodException ex) { -// return null; -// } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { -// throw TreeShims.throwAny(ex); -// } -// } -// -// public static List getExpressions(Tree node) { -// List exprTrees = new ArrayList<>(); -// -// switch (node.getKind().toString()) { -// case "CASE": -// exprTrees = getExpressions((CaseTree) node); -// break; -// case SWITCH_EXPRESSION: { -// try { -// Class swExprTreeClass = Class.forName("com.sun.source.tree.SwitchExpressionTree"); -// Method getExpressions = swExprTreeClass.getDeclaredMethod("getExpression"); -// exprTrees = Collections.singletonList((ExpressionTree) getExpressions.invoke(node)); -// } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { -// throw TreeShims.throwAny(ex); -// } -// break; -// } -// case "SWITCH": -// exprTrees = Collections.singletonList(((SwitchTree) node).getExpression()); -// break; -// default: -// break; -// } -// return exprTrees; -// } -// -// public static List getCases(Tree node) { -// List caseTrees = new ArrayList<>(); -// -// switch (node.getKind().toString()) { -// case "SWITCH": -// caseTrees = ((SwitchTree) node).getCases(); -// break; -// case "SWITCH_EXPRESSION": { -// try { -// Class swExprTreeClass = Class.forName("com.sun.source.tree.SwitchExpressionTree"); -// Method getCases = swExprTreeClass.getDeclaredMethod("getCases"); -// caseTrees = (List) getCases.invoke(node); -// } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { -// throw TreeShims.throwAny(ex); -// } -// } -// } -// return caseTrees; -// } -// -// public static ExpressionTree getValue(BreakTree node) { -// try { -// Method getExpression = BreakTree.class.getDeclaredMethod("getValue"); -// return (ExpressionTree) getExpression.invoke(node); -// } catch (NoSuchMethodException ex) { -// return null; -// } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { -// throw TreeShims.throwAny(ex); -// } -// } -// -// public static Name getBinding(Tree node) { -// try { -// Class bpt = Class.forName("com.sun.source.tree.BindingPatternTree"); //NOI18N -// return isJDKVersionSupportEnablePreview() -// ? (Name)bpt.getDeclaredMethod("getBinding").invoke(node) //NOI18N -// : ((VariableTree)bpt.getDeclaredMethod("getVariable").invoke(node)).getName(); //NOI18N -// -// } catch (NoSuchMethodException | ClassNotFoundException ex) { -// return null; -// } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { -// throw TreeShims.throwAny(ex); -// } -// } -// -// public static Tree getGuardedPattern(Tree node) { -// try { -// Class gpt = Class.forName("com.sun.source.tree.GuardedPatternTree"); //NOI18N -// return isJDKVersionRelease17_Or_Above() -// ? (Tree)gpt.getDeclaredMethod("getPattern").invoke(node) //NOI18N -// : null; -// -// } catch (NoSuchMethodException | ClassNotFoundException ex) { -// return null; -// } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { -// throw TreeShims.throwAny(ex); -// } -// } -// -// public static Tree getParenthesizedPattern(Tree node) { -// try { -// Class ppt = Class.forName("com.sun.source.tree.ParenthesizedPatternTree"); //NOI18N -// return isJDKVersionRelease17_Or_Above() -// ? (Tree)ppt.getDeclaredMethod("getPattern").invoke(node) //NOI18N -// : null; -// -// } catch (NoSuchMethodException | ClassNotFoundException ex) { -// return null; -// } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { -// throw TreeShims.throwAny(ex); -// } -// } -// -// public static ExpressionTree getGuardedExpression(Tree node) { -// try { -// Class gpt = Class.forName("com.sun.source.tree.GuardedPatternTree"); //NOI18N -// return isJDKVersionRelease17_Or_Above() -// ? (ExpressionTree)gpt.getDeclaredMethod("getExpression").invoke(node) //NOI18N -// : null; -// -// } catch (NoSuchMethodException | ClassNotFoundException ex) { -// return null; -// } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { -// throw TreeShims.throwAny(ex); -// } -// } -// -// public static List getPermits(ClassTree node) { -// List perms = null; -// try { -// Class classTree = Class.forName("com.sun.source.tree.ClassTree"); -// Method getPerms = classTree.getDeclaredMethod("getPermitsClause"); -// perms = (List) getPerms.invoke(node); -// } catch (ClassNotFoundException | NoSuchMethodException ex) { -// return null; -// } catch (SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { -// throw TreeShims.throwAny(ex); -// } -// return perms; -// } -// -// public static ReferenceTree getRefrenceTree(DocTreeMaker docMake, ExpressionTree qualExpr, CharSequence member, List paramTypes, Names names, List paramTypesList) { -// int NOPOS = -2; -// try { -// Class classTree = Class.forName("com.sun.tools.javac.tree.DocTreeMaker"); -// Method newReferenceTree = classTree.getDeclaredMethod("newReferenceTree", java.lang.String.class, com.sun.tools.javac.tree.JCTree.JCExpression.class, com.sun.tools.javac.tree.JCTree.class, javax.lang.model.element.Name.class, java.util.List.class); -// return (ReferenceTree) newReferenceTree.invoke(docMake.at(NOPOS), "", (JCTree.JCExpression) qualExpr, qualExpr == null ? null : ((JCTree.JCExpression) qualExpr).getTree(), member != null ? (com.sun.tools.javac.util.Name) names.fromString(member.toString()) : null, paramTypesList); -// } catch (ClassNotFoundException | NoSuchMethodException ex) { -// return null; -// } catch (SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { -// throw TreeShims.throwAny(ex); -// } -// } -// -// public static List getPermits(JCClassDecl newT) { -// List newPermitings = new ArrayList<>(); -// try { -// Class jCClassDecl = Class.forName("com.sun.tools.javac.tree.JCTree$JCClassDecl"); -// newPermitings = (com.sun.tools.javac.util.List) jCClassDecl.getDeclaredField("permitting").get(newT); -// } catch (ClassNotFoundException | NoSuchFieldException ex) { -// return null; -// } catch (IllegalArgumentException | IllegalAccessException ex) { -// throw TreeShims.throwAny(ex); -// } -// return newPermitings; -// } -// -// public static ExpressionTree getYieldValue(Tree node) { -// if (!node.getKind().toString().equals(YIELD)) { -// return null; -// } -// try { -// Class yieldTreeClass = Class.forName("com.sun.source.tree.YieldTree"); //NOI18N -// Method getExpression = yieldTreeClass.getDeclaredMethod("getValue"); //NOI18N -// return (ExpressionTree) getExpression.invoke(node); -// } catch (NoSuchMethodException ex) { -// return null; -// } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | ClassNotFoundException ex) { -// throw TreeShims.throwAny(ex); -// } -// } -// -// public static Tree getBindingPatternType(Tree node) { -// if (!node.getKind().toString().equals(BINDING_PATTERN)) { -// return null; -// } -// try { -// Class bpt = Class.forName("com.sun.source.tree.BindingPatternTree"); //NOI18N -// return isJDKVersionSupportEnablePreview() -// ? (Tree) bpt.getDeclaredMethod("getType").invoke(node) //NOI18N -// : ((VariableTree) bpt.getDeclaredMethod("getVariable").invoke(node)).getType(); //NOI18N -// -// } catch (NoSuchMethodException ex) { -// return null; -// } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | ClassNotFoundException ex) { -// throw TreeShims.throwAny(ex); -// } -// } -// -// public static boolean isRecord(Element el) { -// return el != null && "RECORD".equals(el.getKind().name()); -// } -// -// public static boolean isRecord(final N node) { -// return node != null && TreeShims.RECORD.equals(node.getKind().name()); -// } -// -// public static boolean isRecordComponent(Element el) { -// return el != null && "RECORD_COMPONENT".equals(el.getKind().name()); -// } -// -// -// public static boolean isRecordComponent(ElementKind kind) { -// return "RECORD_COMPONENT".equals(kind.name()); -// } -// -// public static ElementKind getRecordKind() { -// try { -// return ElementKind.valueOf(RECORD); //NOI18N -// } catch (IllegalArgumentException ex) { -// return null; -// } -// } -// -// public static Tree getTarget(Tree node) { -// if (!node.getKind().name().equals(YIELD)) { -// throw new IllegalStateException(); -// } -// try { -// Field target = node.getClass().getField("target"); -// return (Tree) target.get(node); -// } catch (NoSuchFieldException ex) { -// return null; -// } catch (IllegalAccessException | IllegalArgumentException ex) { -// throw TreeShims.throwAny(ex); -// } -// } -// -// -// public static boolean isJDKVersionSupportEnablePreview() { -// return Integer.valueOf(SourceVersion.latest().name().split("_")[1]).compareTo(PATTERN_MATCHING_INSTANCEOF_PREVIEW_JDK_VERSION) <= 0; -// } -// -// public static boolean isJDKVersionRelease16_Or_Above(){ -// return Integer.valueOf(SourceVersion.latest().name().split("_")[1]).compareTo(16) >= 0; -// } -// -// -// public static ModuleTree getModule(CompilationUnitTree cut) { -// try { -// return (ModuleTree) CompilationUnitTree.class.getDeclaredMethod("getModule").invoke(cut); -// } catch (NoSuchMethodException | SecurityException ex) { -// final List typeDecls = cut.getTypeDecls(); -// if (!typeDecls.isEmpty()) { -// final Tree typeDecl = typeDecls.get(0); -// if (typeDecl.getKind() == Tree.Kind.MODULE) { -// return (ModuleTree)typeDecl; -// } -// } -// } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { -// throwAny(ex); -// } -// return null; -// } -// public static List getSnippetDocTreeAttributes(DocTree node) { -// try { -// Class gpt = Class.forName("com.sun.source.doctree.SnippetTree"); //NOI18N -// return isJDKVersionRelease18_Or_Above() -// ? (List) gpt.getDeclaredMethod("getAttributes").invoke(node) //NOI18N -// : null; -// } catch (NoSuchMethodException | ClassNotFoundException ex) { -// return null; -// } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { -// throw TreeShims.throwAny(ex); -// } -// } -// -// public static TextTree getSnippetDocTreeText(DocTree node) { -// try { -// Class gpt = Class.forName("com.sun.source.doctree.SnippetTree"); //NOI18N -// return isJDKVersionRelease18_Or_Above() -// ? (TextTree) gpt.getDeclaredMethod("getBody").invoke(node) //NOI18N -// : null; -// } catch (NoSuchMethodException | ClassNotFoundException ex) { -// return null; -// } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { -// throw TreeShims.throwAny(ex); -// } -// } - -// public static ExpressionTree getDeconstructor(Tree node) { -// try { -// Class gpt = Class.forName("com.sun.source.tree.DeconstructionPatternTree"); //NOI18N -// return isJDKVersionRelease19_Or_Above() -// ? (ExpressionTree) gpt.getDeclaredMethod("getDeconstructor").invoke(node) //NOI18N -// : null; -// } catch (NoSuchMethodException | ClassNotFoundException ex) { -// return null; -// } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { -// throw TreeShims.throwAny(ex); -// } -// } - -// public static List getNestedPatterns(Tree node) { -// try { -// Class gpt = Class.forName("com.sun.source.tree.DeconstructionPatternTree"); //NOI18N -// return isJDKVersionRelease19_Or_Above() -// ? (List) gpt.getDeclaredMethod("getNestedPatterns").invoke(node) //NOI18N -// : null; -// } catch (NoSuchMethodException | ClassNotFoundException ex) { -// return null; -// } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { -// throw TreeShims.throwAny(ex); -// } -// } - -// public static VariableTree getVariable(Tree node) { -// try { -// Class gpt = Class.forName("com.sun.source.tree.DeconstructionPatternTree"); //NOI18N -// return isJDKVersionRelease19_Or_Above() -// ? (VariableTree) gpt.getDeclaredMethod("getVariable").invoke(node) //NOI18N -// : null; -// } catch (NoSuchMethodException | ClassNotFoundException ex) { -// return null; -// } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { -// throw TreeShims.throwAny(ex); -// } -// } - -// public static Tree RecordPattern(TreeMaker make, ExpressionTree deconstructor, List nested, VariableTree var) { -// ListBuffer nestedVar = new ListBuffer<>(); -// for (PatternTree t : nested) { -// nestedVar.append((JCTree.JCPattern) t); -// } -// try { -// Method getMethod = TreeMaker.class.getDeclaredMethod("RecordPattern", JCTree.JCExpression.class, com.sun.tools.javac.util.List.class, JCTree.JCVariableDecl.class); -// return (Tree) getMethod.invoke(make, (JCTree.JCExpression) deconstructor, nestedVar.toList(), (JCTree.JCVariableDecl) var); -// } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { -// throw TreeShims.throwAny(ex); -// } -// } - - public static Element toRecordComponent(Element el) { - if (el == null ||el.getKind() != ElementKind.FIELD) { - return el; - } - TypeElement owner = (TypeElement) el.getEnclosingElement(); - if (!"RECORD".equals(owner.getKind().name())) { - return el; - } - for (Element encl : ElementFilter.recordComponentsIn(owner.getEnclosedElements())) { - if (encl.getSimpleName().equals(el.getSimpleName())) { - return encl; - } - } - return el; - } - - public static boolean isPatternMatch(Tree node) { - if (isJDKVersionRelease17_Or_Above()) { - try { - return node.getClass().getField("patternSwitch").getBoolean(node); - } catch(NoSuchFieldException e){ - return false; - }catch (IllegalArgumentException | IllegalAccessException | SecurityException ex) { - throw TreeShims.throwAny(ex); - } - } - return false; - } - - public static boolean isJDKVersionRelease17_Or_Above(){ - return Integer.valueOf(SourceVersion.latest().name().split("_")[1]).compareTo(17) >= 0; - } - -// public static boolean isJDKVersionRelease19_Or_Above(){ -// return Integer.valueOf(SourceVersion.latest().name().split("_")[1]).compareTo(19) >= 0; -// } - -// public static boolean isJDKVersionRelease18_Or_Above() { -// return Integer.valueOf(SourceVersion.latest().name().split("_")[1]).compareTo(18) >= 0; -// } - - @SuppressWarnings("unchecked") - public static 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 deleted file mode 100644 index 9e35db3618b9..000000000000 --- a/java/java.source.base/src/org/netbeans/modules/java/source/TreeShimsCopier.java +++ /dev/null @@ -1,106 +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.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.nio.charset.StandardCharsets; -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.annotation.processing.SupportedSourceVersion; -import javax.lang.model.SourceVersion; -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("*") -@SupportedSourceVersion(SourceVersion.RELEASE_8) -public class TreeShimsCopier extends AbstractProcessor { - - @Override - public boolean process(Set 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(), StandardCharsets.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(StandardCharsets.UTF_8)); - } - } catch (IOException ex) { - throw new IllegalStateException(ex); - } - } - } - return false; - } - - private static final Map ALLOWED_CLASSES2TARGET_PACKAGE = new HashMap() {{ - 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.impl"); - put("org.netbeans.modules.editor.java.GoToSupport", "org.netbeans.modules.editor.java"); - put("org.netbeans.modules.java.editor.base.semantic.SemanticHighlighterBase", "org.netbeans.modules.java.editor.base.semantic"); - }}; -} diff --git a/java/java.source.base/src/org/netbeans/modules/java/source/save/CasualDiff.java b/java/java.source.base/src/org/netbeans/modules/java/source/save/CasualDiff.java index b6fc5320e630..a4df3d1ad29f 100644 --- a/java/java.source.base/src/org/netbeans/modules/java/source/save/CasualDiff.java +++ b/java/java.source.base/src/org/netbeans/modules/java/source/save/CasualDiff.java @@ -136,13 +136,11 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.*; import java.util.Map.Entry; import static java.util.logging.Level.*; import java.util.logging.Logger; -import java.util.stream.Collectors; import javax.swing.text.BadLocationException; import javax.swing.text.Document; import org.netbeans.api.annotations.common.NullAllowed; @@ -175,7 +173,6 @@ import org.openide.util.NbBundle; import org.openide.util.NbCollections; import javax.lang.model.type.TypeKind; -import org.netbeans.modules.java.source.TreeShims; import org.netbeans.modules.java.source.transform.TreeHelpers; public class CasualDiff { @@ -5783,7 +5780,7 @@ private int diffTreeImpl0(JCTree oldT, JCTree newT, JCTree parent /*used only fo * Three sets representing different kind which can be matched. No need * to rewrite whole expression. Ensure that CompoundAssignementTrees, * UnaryTrees and BinaryTrees are matched, i.e. diff method is used - * instead of priting whole new tree. + * instead of printing whole new tree. */ private static final EnumSet compAssign = EnumSet.of( Kind.MULTIPLY_ASSIGNMENT, diff --git a/java/java.source.base/src/org/netbeans/modules/java/source/transform/ImmutableDocTreeTranslator.java b/java/java.source.base/src/org/netbeans/modules/java/source/transform/ImmutableDocTreeTranslator.java index 05507fe0e62a..99eb4d05840a 100644 --- a/java/java.source.base/src/org/netbeans/modules/java/source/transform/ImmutableDocTreeTranslator.java +++ b/java/java.source.base/src/org/netbeans/modules/java/source/transform/ImmutableDocTreeTranslator.java @@ -62,7 +62,6 @@ import java.util.List; import java.util.Map; import org.netbeans.api.java.source.WorkingCopy; -import org.netbeans.modules.java.source.TreeShims; /** *