diff --git a/java.hints/src/org/netbeans/modules/java/hints/SerialVersionUID.java b/java.hints/src/org/netbeans/modules/java/hints/SerialVersionUID.java --- a/java.hints/src/org/netbeans/modules/java/hints/SerialVersionUID.java +++ b/java.hints/src/org/netbeans/modules/java/hints/SerialVersionUID.java @@ -24,8 +24,11 @@ package org.netbeans.modules.java.hints; import com.sun.source.tree.ClassTree; +import com.sun.source.tree.NewClassTree; +import com.sun.source.tree.Tree; import com.sun.source.tree.Tree.Kind; import com.sun.source.tree.VariableTree; +import com.sun.source.util.SourcePositions; import com.sun.source.util.TreePath; import java.util.ArrayList; import java.util.Collections; @@ -35,6 +38,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import javax.lang.model.element.ElementKind; import javax.lang.model.element.Modifier; +import javax.lang.model.element.NestingKind; import static javax.lang.model.element.Modifier.*; import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; @@ -67,6 +71,7 @@ */ public class SerialVersionUID extends AbstractHint { + private static final Set TREE_KINDS = EnumSet.of(Tree.Kind.CLASS); private static final String SERIAL = "serial"; //NOI18N private static final String SVUID = "serialVersionUID"; //NOI18N private static final String SERIALIZABLE = "java.io.Serializable"; //NOI18N @@ -82,7 +87,7 @@ } public Set getTreeKinds() { - return EnumSet.of(Kind.CLASS); + return TREE_KINDS; } public List run(CompilationInfo info, TreePath treePath) { @@ -99,14 +104,31 @@ List fixes = new ArrayList(); fixes.add(new FixImpl(TreePathHandle.create(treePath, info), false)); // fixes.add(new FixImpl(TreePathHandle.create(treePath, info), true)); - fixes.addAll(FixFactory.createSuppressWarnings(info, treePath, SERIAL)); - int[] span = info.getTreeUtilities().findNameSpan((ClassTree) treePath.getLeaf()); - if (span == null) { //span cannot be found, do not show anything - return null; + ErrorDescription ed = null; + String desc = NbBundle.getMessage(getClass(), "ERR_SerialVersionUID"); //NOI18N + if (type.getNestingKind().equals(NestingKind.ANONYMOUS)) { + SourcePositions pos = info.getTrees().getSourcePositions(); + Tree clazzTree = null; + for (Tree tree : treePath) { + if (tree.getKind().equals(Tree.Kind.NEW_CLASS)) { + clazzTree = ((NewClassTree) tree).getIdentifier(); + break; + } + } + // clazzTree = treePath.getParentPath().getLeaf() to mark all implementation! + if (clazzTree == null) return null; // return nothing + long start = pos.getStartPosition(info.getCompilationUnit(), clazzTree); + long end = pos.getEndPosition(info.getCompilationUnit(), clazzTree); + ed = ErrorDescriptionFactory.createErrorDescription( + getSeverity().toEditorSeverity(), desc, fixes, info.getFileObject(), (int) start, (int) end); + } else { + // add SuppressWarning only to non-anonymous class + fixes.addAll(FixFactory.createSuppressWarnings(info, treePath, SERIAL)); + int[] span = info.getTreeUtilities().findNameSpan((ClassTree) treePath.getLeaf()); + ed = ErrorDescriptionFactory.createErrorDescription( + getSeverity().toEditorSeverity(), desc, fixes, info.getFileObject(), span[0], span[1]); } - String desc = NbBundle.getMessage(getClass(), "ERR_SerialVersionUID"); //NOI18N - ErrorDescription ed = ErrorDescriptionFactory.createErrorDescription(getSeverity().toEditorSeverity(), desc, fixes, info.getFileObject(), span[0], span[1]); if (cancel.get()) { return null; }