diff --git a/enterprise/web.jsf/src/org/netbeans/modules/web/jsf/hints/rules/JakartaFacesBeanIsGonnaBeDeprecated.java b/enterprise/web.jsf/src/org/netbeans/modules/web/jsf/hints/rules/JakartaFacesBeanIsGonnaBeDeprecated.java index 987e2d884f18..63b6e360a872 100644 --- a/enterprise/web.jsf/src/org/netbeans/modules/web/jsf/hints/rules/JakartaFacesBeanIsGonnaBeDeprecated.java +++ b/enterprise/web.jsf/src/org/netbeans/modules/web/jsf/hints/rules/JakartaFacesBeanIsGonnaBeDeprecated.java @@ -38,6 +38,7 @@ import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.api.java.source.JavaSource; import org.netbeans.api.java.source.TreeMaker; +import org.netbeans.api.java.source.TreePathHandle; import org.netbeans.api.java.source.WorkingCopy; import org.netbeans.modules.j2ee.core.api.support.java.GenerationUtils; import org.netbeans.modules.web.jsf.hints.JsfHintsContext; @@ -119,22 +120,22 @@ private static List getFixesForType(CompilationInfo info, TypeElement typeE String annotationType = am.getAnnotationType().toString(); if (DEPRECATED_TO_FIX.containsKey(annotationType)) { TreePath path = info.getTrees().getPath(typeElement, am); - fixes.add(new ChangeClassFix(info, path, typeElement, am, annotationType, DEPRECATED_TO_FIX.get(annotationType)).toEditorFix()); + fixes.add(new ChangeClassFix(info, path, typeElement, MANAGED_BEAN.equals(annotationType), annotationType, DEPRECATED_TO_FIX.get(annotationType)).toEditorFix()); } return fixes; } private static final class ChangeClassFix extends JavaFix { - private final TypeElement element; - private final AnnotationMirror annotation; + private final TreePathHandle element; + private final boolean managedBean; private final String deprecatedClass; private final String replacingClass; - public ChangeClassFix(CompilationInfo info, TreePath path, TypeElement element, AnnotationMirror annotation, String deprecatedClass, String replacingClass) { + public ChangeClassFix(CompilationInfo info, TreePath path, TypeElement element, boolean managedBean, String deprecatedClass, String replacingClass) { super(info, path); - this.element = element; - this.annotation = annotation; + this.element = TreePathHandle.create(element, info); + this.managedBean = managedBean; this.deprecatedClass = deprecatedClass; this.replacingClass = replacingClass; } @@ -152,11 +153,12 @@ protected void performRewrite(TransformationContext ctx) throws Exception { WorkingCopy wc = ctx.getWorkingCopy(); wc.toPhase(JavaSource.Phase.RESOLVED); TreeMaker make = wc.getTreeMaker(); + TreePath elementPath = element.resolve(wc); // rewrite annotations in case of ManagedBean - if (MANAGED_BEAN.equals(annotation.getAnnotationType().toString())) { - ModifiersTree modifiers = ((ClassTree) wc.getTrees().getTree(element)).getModifiers(); - AnnotationTree annotationTree = (AnnotationTree) wc.getTrees().getTree(element, annotation); + if (managedBean) { + ModifiersTree modifiers = ((ClassTree) elementPath.getLeaf()).getModifiers(); + AnnotationTree annotationTree = (AnnotationTree) ctx.getPath().getLeaf(); List arguments = new ArrayList<>(); for (ExpressionTree expressionTree : annotationTree.getArguments()) { if (expressionTree.getKind() == Tree.Kind.ASSIGNMENT) { @@ -168,7 +170,7 @@ protected void performRewrite(TransformationContext ctx) throws Exception { } } } - ModifiersTree newModifiersTree = make.removeModifiersAnnotation(modifiers, (AnnotationTree) wc.getTrees().getTree(element, annotation)); + ModifiersTree newModifiersTree = make.removeModifiersAnnotation(modifiers, annotationTree); AnnotationTree newTree = GenerationUtils.newInstance(wc).createAnnotation(replacingClass, arguments); newModifiersTree = make.addModifiersAnnotation(newModifiersTree, newTree); wc.rewrite(modifiers, newModifiersTree); diff --git a/enterprise/web.jsf/src/org/netbeans/modules/web/jsf/hints/rules/JavaxFacesBeanIsGonnaBeDeprecated.java b/enterprise/web.jsf/src/org/netbeans/modules/web/jsf/hints/rules/JavaxFacesBeanIsGonnaBeDeprecated.java index b94230a458f8..9caf6e86e9bc 100644 --- a/enterprise/web.jsf/src/org/netbeans/modules/web/jsf/hints/rules/JavaxFacesBeanIsGonnaBeDeprecated.java +++ b/enterprise/web.jsf/src/org/netbeans/modules/web/jsf/hints/rules/JavaxFacesBeanIsGonnaBeDeprecated.java @@ -38,6 +38,7 @@ import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.api.java.source.JavaSource; import org.netbeans.api.java.source.TreeMaker; +import org.netbeans.api.java.source.TreePathHandle; import org.netbeans.api.java.source.WorkingCopy; import org.netbeans.modules.j2ee.core.api.support.java.GenerationUtils; import org.netbeans.modules.web.jsf.hints.JsfHintsContext; @@ -119,22 +120,22 @@ private static List getFixesForType(CompilationInfo info, TypeElement typeE String annotationType = am.getAnnotationType().toString(); if (DEPRECATED_TO_FIX.containsKey(annotationType)) { TreePath path = info.getTrees().getPath(typeElement, am); - fixes.add(new ChangeClassFix(info, path, typeElement, am, annotationType, DEPRECATED_TO_FIX.get(annotationType)).toEditorFix()); + fixes.add(new ChangeClassFix(info, path, typeElement, MANAGED_BEAN.equals(annotationType), annotationType, DEPRECATED_TO_FIX.get(annotationType)).toEditorFix()); } return fixes; } private static final class ChangeClassFix extends JavaFix { - private final TypeElement element; - private final AnnotationMirror annotation; + private final TreePathHandle element; + private final boolean managedBean; private final String deprecatedClass; private final String replacingClass; - public ChangeClassFix(CompilationInfo info, TreePath path, TypeElement element, AnnotationMirror annotation, String deprecatedClass, String replacingClass) { + public ChangeClassFix(CompilationInfo info, TreePath path, TypeElement element, boolean managedBean, String deprecatedClass, String replacingClass) { super(info, path); - this.element = element; - this.annotation = annotation; + this.element = TreePathHandle.create(element, info); + this.managedBean = managedBean; this.deprecatedClass = deprecatedClass; this.replacingClass = replacingClass; } @@ -152,11 +153,12 @@ protected void performRewrite(TransformationContext ctx) throws Exception { WorkingCopy wc = ctx.getWorkingCopy(); wc.toPhase(JavaSource.Phase.RESOLVED); TreeMaker make = wc.getTreeMaker(); + TreePath elementPath = element.resolve(wc); // rewrite annotations in case of ManagedBean - if (MANAGED_BEAN.equals(annotation.getAnnotationType().toString())) { - ModifiersTree modifiers = ((ClassTree) wc.getTrees().getTree(element)).getModifiers(); - AnnotationTree annotationTree = (AnnotationTree) wc.getTrees().getTree(element, annotation); + if (managedBean && elementPath != null) { + ModifiersTree modifiers = ((ClassTree) elementPath.getLeaf()).getModifiers(); + AnnotationTree annotationTree = (AnnotationTree) ctx.getPath().getLeaf(); List arguments = new ArrayList<>(); for (ExpressionTree expressionTree : annotationTree.getArguments()) { if (expressionTree.getKind() == Tree.Kind.ASSIGNMENT) { @@ -168,7 +170,7 @@ protected void performRewrite(TransformationContext ctx) throws Exception { } } } - ModifiersTree newModifiersTree = make.removeModifiersAnnotation(modifiers, (AnnotationTree) wc.getTrees().getTree(element, annotation)); + ModifiersTree newModifiersTree = make.removeModifiersAnnotation(modifiers, annotationTree); AnnotationTree newTree = GenerationUtils.newInstance(wc).createAnnotation(replacingClass, arguments); newModifiersTree = make.addModifiersAnnotation(newModifiersTree, newTree); wc.rewrite(modifiers, newModifiersTree);