Warning about use of top-level private classes.
authorJesse Glick <jglick@netbeans.org>
Mon, 07 Nov 2011 21:06:27 -0500
changeset 17663041daef44624
parent 17662 6494a399b320
child 17664 38f9173a2ed7
Warning about use of top-level private classes.
javahints/nbproject/project.properties
javahints/src/org/netbeans/modules/javahints/PrivateTopLevelClass.java
javahints/src/org/netbeans/modules/javahints/resources/layer.xml
     1.1 --- a/javahints/nbproject/project.properties	Mon Nov 07 11:40:20 2011 -0500
     1.2 +++ b/javahints/nbproject/project.properties	Mon Nov 07 21:06:27 2011 -0500
     1.3 @@ -49,8 +49,8 @@
     1.4  auxiliary.org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.tab-size=4
     1.5  auxiliary.org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-limit-width=80
     1.6  javac.compilerargs=-Xlint:unchecked
     1.7 -javac.source=1.5
     1.8 -spec.version.base=2.49.0
     1.9 +javac.source=1.6
    1.10 +spec.version.base=2.50.0
    1.11  
    1.12  nbm.needs.restart=true
    1.13  requires.nb.javac=true
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/javahints/src/org/netbeans/modules/javahints/PrivateTopLevelClass.java	Mon Nov 07 21:06:27 2011 -0500
     2.3 @@ -0,0 +1,124 @@
     2.4 +/*
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
     2.8 + *
     2.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    2.10 + * Other names may be trademarks of their respective owners.
    2.11 + *
    2.12 + * The contents of this file are subject to the terms of either the GNU
    2.13 + * General Public License Version 2 only ("GPL") or the Common
    2.14 + * Development and Distribution License("CDDL") (collectively, the
    2.15 + * "License"). You may not use this file except in compliance with the
    2.16 + * License. You can obtain a copy of the License at
    2.17 + * http://www.netbeans.org/cddl-gplv2.html
    2.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.19 + * specific language governing permissions and limitations under the
    2.20 + * License.  When distributing the software, include this License Header
    2.21 + * Notice in each file and include the License file at
    2.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    2.23 + * particular file as subject to the "Classpath" exception as provided
    2.24 + * by Oracle in the GPL Version 2 section of the License file that
    2.25 + * accompanied this code. If applicable, add the following below the
    2.26 + * License Header, with the fields enclosed by brackets [] replaced by
    2.27 + * your own identifying information:
    2.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    2.29 + *
    2.30 + * If you wish your version of this file to be governed by only the CDDL
    2.31 + * or only the GPL Version 2, indicate your decision by adding
    2.32 + * "[Contributor] elects to include this software in this distribution
    2.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    2.34 + * single choice of license, a recipient has the option to distribute
    2.35 + * your version of this file under either the CDDL, the GPL Version 2 or
    2.36 + * to extend the choice of license to its licensees as provided above.
    2.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    2.38 + * Version 2 license, then the option applies only if the new code is
    2.39 + * made subject to such option by the copyright holder.
    2.40 + *
    2.41 + * Contributor(s):
    2.42 + *
    2.43 + * Portions Copyrighted 2011 Sun Microsystems, Inc.
    2.44 + */
    2.45 +
    2.46 +package org.netbeans.modules.javahints;
    2.47 +
    2.48 +import org.netbeans.api.java.source.CompilationInfo;
    2.49 +import org.netbeans.modules.java.hints.spi.AbstractHint;
    2.50 +import org.netbeans.spi.editor.hints.ErrorDescription;
    2.51 +import com.sun.source.tree.ClassTree;
    2.52 +import com.sun.source.tree.Tree;
    2.53 +import com.sun.source.tree.Tree.Kind;
    2.54 +import com.sun.source.util.TreePath;
    2.55 +import com.sun.source.util.TreePathScanner;
    2.56 +import java.util.LinkedList;
    2.57 +import java.util.List;
    2.58 +import java.util.Set;
    2.59 +import org.netbeans.api.java.source.TreeUtilities;
    2.60 +import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
    2.61 +import org.openide.util.NbBundle.Messages;
    2.62 +
    2.63 +public class PrivateTopLevelClass extends AbstractHint {
    2.64 +
    2.65 +    public PrivateTopLevelClass() {
    2.66 +        super(true, true, HintSeverity.WARNING);
    2.67 +    }
    2.68 +
    2.69 +    @Override public String getDescription() {
    2.70 +        return getDisplayName(); // XXX how do these differ?
    2.71 +    }
    2.72 +
    2.73 +    @Override public Set<Kind> getTreeKinds() {
    2.74 +        return TreeUtilities.CLASS_TREE_KINDS;
    2.75 +    }
    2.76 +
    2.77 +    @Override public List<ErrorDescription> run(CompilationInfo info, TreePath treePath) {
    2.78 +        VisitorImpl v = new VisitorImpl(info);
    2.79 +        v.scan(treePath, null);
    2.80 +        return v.warnings;
    2.81 +    }
    2.82 +
    2.83 +    @Override public String getId() {
    2.84 +        return PrivateTopLevelClass.class.getName();
    2.85 +    }
    2.86 +
    2.87 +    @Messages("PrivateTopLevelClass.displayName=JDK 1.0-style top-level private class")
    2.88 +    @Override public String getDisplayName() {
    2.89 +        return Bundle.PrivateTopLevelClass_displayName();
    2.90 +    }
    2.91 +
    2.92 +    @Override public void cancel() {}
    2.93 +
    2.94 +    private final class VisitorImpl extends TreePathScanner<Void,Void> {
    2.95 +
    2.96 +        private final CompilationInfo info;
    2.97 +        private final List<ErrorDescription> warnings;
    2.98 +
    2.99 +        VisitorImpl(CompilationInfo info) {
   2.100 +            this.info = info;
   2.101 +            this.warnings = new LinkedList<ErrorDescription>();
   2.102 +        }
   2.103 +
   2.104 +        @Override public Void visitClass(ClassTree node, Void p) {
   2.105 +            if (!node.getSimpleName().contentEquals(info.getFileObject().getName())) {
   2.106 +                for (Tree t : info.getTrees().getPath(info.getCompilationUnit(), node)) {
   2.107 +                    if (t != node && TreeUtilities.CLASS_TREE_KINDS.contains(t.getKind())) {
   2.108 +                        return null;
   2.109 +                    }
   2.110 +                }
   2.111 +                warnings.add(ErrorDescriptionFactory.createErrorDescription(
   2.112 +                        getSeverity().toEditorSeverity(),
   2.113 +                        getDisplayName(),
   2.114 +                        // XXX add a fix to make into a private static nested class
   2.115 +                        // e.g.: https://issues.apache.org/jira/secure/attachment/12502864/LUCENE-3525.diff
   2.116 +                        info.getFileObject(),
   2.117 +                        // XXX works only if there are no modifiers; how to find location of the identifier?
   2.118 +                        (int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), node) + 6,
   2.119 +                        (int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), node) + node.getSimpleName().length() + 6
   2.120 +                        ));
   2.121 +            }
   2.122 +            return null;
   2.123 +        }
   2.124 +
   2.125 +    }
   2.126 +
   2.127 +}
     3.1 --- a/javahints/src/org/netbeans/modules/javahints/resources/layer.xml	Mon Nov 07 11:40:20 2011 -0500
     3.2 +++ b/javahints/src/org/netbeans/modules/javahints/resources/layer.xml	Mon Nov 07 21:06:27 2011 -0500
     3.3 @@ -63,6 +63,7 @@
     3.4                      <file name="org-netbeans-modules-javahints-NoDOGetCookie.instance"/>
     3.5                      <file name="org-netbeans-modules-javahints-OrganizeImports.instance"/>
     3.6  		    <file name="org-netbeans-modules-javahints-Excluded.instance"/>
     3.7 +		    <file name="org-netbeans-modules-javahints-PrivateTopLevelClass.instance"/>
     3.8                  </folder>
     3.9              </folder>
    3.10