Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -119,22 +120,22 @@ private static List<Fix> 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;
}
Expand All @@ -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<ExpressionTree> arguments = new ArrayList<>();
for (ExpressionTree expressionTree : annotationTree.getArguments()) {
if (expressionTree.getKind() == Tree.Kind.ASSIGNMENT) {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -119,22 +120,22 @@ private static List<Fix> 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;
}
Expand All @@ -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<ExpressionTree> arguments = new ArrayList<>();
for (ExpressionTree expressionTree : annotationTree.getArguments()) {
if (expressionTree.getKind() == Tree.Kind.ASSIGNMENT) {
Expand All @@ -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);
Expand Down
Loading