src/share/sample/javac/processing/src/CheckNamesProcessor.java

Print this page




 121     @Override
 122     public boolean process(Set<? extends TypeElement> annotations,
 123             RoundEnvironment roundEnv) {
 124         if (!roundEnv.processingOver()) {
 125             for (Element element : roundEnv.getRootElements() )
 126                 nameChecker.checkNames(element);
 127         }
 128         return false; // Allow other processors to examine files too.
 129     }
 130 
 131     @Override
 132     public void init(ProcessingEnvironment processingEnv) {
 133         super.init(processingEnv);
 134         nameChecker = new NameChecker(processingEnv);
 135     }
 136 
 137     @Override
 138     public SourceVersion getSupportedSourceVersion() {
 139         /*
 140          * Return latest source version instead of a fixed version
 141          * like RELEASE_6.  To return a fixed version, this class
 142          * could be annotated with a SupportedSourceVersion
 143          * annotation.
 144          *
 145          * Warnings will be issued if any unknown language constructs
 146          * are encountered.
 147          */
 148         return SourceVersion.latest();
 149     }
 150 
 151     /**
 152      * Provide checks that an element and its enclosed elements follow
 153      * the usual naming conventions.
 154      *
 155      * <p> Conventions from JLSv3 section 6.8:
 156      *
 157      * <ul>
 158      * <li> Classes and interfaces: camel case, first letter is uppercase
 159      * <li> Methods: camel case, first letter is lowercase
 160      * <li> Type variables: one uppercase letter
 161      * <li> Fields


 173         NameCheckScanner nameCheckScanner = new NameCheckScanner();
 174 
 175         NameChecker(ProcessingEnvironment processsingEnv) {
 176             this.messager  = processsingEnv.getMessager();
 177             this.typeUtils = processsingEnv.getTypeUtils();
 178         }
 179 
 180         /**
 181          * If the name of the argument or its enclosed elements
 182          * violates the naming conventions, report a warning.
 183          */
 184         public void checkNames(Element element) {
 185             // Implement name checks with a visitor, but expose that
 186             // functionality through this method instead.
 187             nameCheckScanner.scan(element);
 188         }
 189 
 190         /**
 191          * Visitor to implement name checks.
 192          */
 193         private class NameCheckScanner extends ElementScanner6<Void, Void> {
 194             // The visitor could be enhanced to return true/false if
 195             // there were warnings reported or a count of the number
 196             // of warnings.  This could be facilitated by using
 197             // Boolean or Integer instead of Void for the actual type
 198             // arguments.  In more detail, one way to tally the number
 199             // of warnings would be for each method to return the sum
 200             // of the warnings it and the methods it called issued, a
 201             // bottom-up computation.  In that case, the first type
 202             // argument would be Integer and the second type argument
 203             // would still be Void.  Alternatively, the current count
 204             // could be passed along in Integer parameter p and each
 205             // method could return the Integer sum of p and the
 206             // warnings the method issued.  Some computations are more
 207             // naturally expressed in one form instead of the other.
 208             // If greater control is needed over traversal order, a
 209             // SimpleElementVisitor can be extended instead of an
 210             // ElementScanner.
 211 
 212             /**
 213              * Check the name of a type and its enclosed elements and


 295                 // elements, is a design decision based on what a
 296                 // PackageElemement is used to mean in this context.
 297                 // A PackageElement can represent a whole package, so
 298                 // it can provide a concise way to indicate many
 299                 // user-defined types should be visited.  However, a
 300                 // PackageElement can also represent a
 301                 // package-info.java file, as would be in the case if
 302                 // the PackageElement came from
 303                 // RoundEnvironment.getRootElements.  In that case,
 304                 // the package-info file and other files in that
 305                 // package could be passed in.  Therefore, without
 306                 // further checks, types in a package could be visited
 307                 // more than once if a package's elements were visited
 308                 // too.
 309                 return null;
 310             }
 311 
 312             @Override
 313             public Void visitUnknown(Element e, Void p) {
 314                 // This method will be called if a kind of element
 315                 // added after JDK 6 is visited.  Since as of this
 316                 // writing the conventions for such constructs aren't
 317                 // known, issue a warning.
 318                 messager.printMessage(WARNING,
 319                                       "Unknown kind of element, " + e.getKind() +
 320                                       ", no name checking performed.", e);
 321                 return null;
 322             }
 323 
 324             // All the name checking methods assume the examined names
 325             // are syntactically well-formed identifiers.
 326 
 327             /**
 328              * Return {@code true} if this variable is a field named
 329              * "serialVersionUID"; false otherwise.  A true
 330              * serialVersionUID of a class has type {@code long} and
 331              * is static and final.
 332              *
 333              * <p>To check that a Serializable class defines a proper
 334              * serialVersionUID, run javac with -Xlint:serial.
 335              *




 121     @Override
 122     public boolean process(Set<? extends TypeElement> annotations,
 123             RoundEnvironment roundEnv) {
 124         if (!roundEnv.processingOver()) {
 125             for (Element element : roundEnv.getRootElements() )
 126                 nameChecker.checkNames(element);
 127         }
 128         return false; // Allow other processors to examine files too.
 129     }
 130 
 131     @Override
 132     public void init(ProcessingEnvironment processingEnv) {
 133         super.init(processingEnv);
 134         nameChecker = new NameChecker(processingEnv);
 135     }
 136 
 137     @Override
 138     public SourceVersion getSupportedSourceVersion() {
 139         /*
 140          * Return latest source version instead of a fixed version
 141          * like RELEASE_7.  To return a fixed version, this class
 142          * could be annotated with a SupportedSourceVersion
 143          * annotation.
 144          *
 145          * Warnings will be issued if any unknown language constructs
 146          * are encountered.
 147          */
 148         return SourceVersion.latest();
 149     }
 150 
 151     /**
 152      * Provide checks that an element and its enclosed elements follow
 153      * the usual naming conventions.
 154      *
 155      * <p> Conventions from JLSv3 section 6.8:
 156      *
 157      * <ul>
 158      * <li> Classes and interfaces: camel case, first letter is uppercase
 159      * <li> Methods: camel case, first letter is lowercase
 160      * <li> Type variables: one uppercase letter
 161      * <li> Fields


 173         NameCheckScanner nameCheckScanner = new NameCheckScanner();
 174 
 175         NameChecker(ProcessingEnvironment processsingEnv) {
 176             this.messager  = processsingEnv.getMessager();
 177             this.typeUtils = processsingEnv.getTypeUtils();
 178         }
 179 
 180         /**
 181          * If the name of the argument or its enclosed elements
 182          * violates the naming conventions, report a warning.
 183          */
 184         public void checkNames(Element element) {
 185             // Implement name checks with a visitor, but expose that
 186             // functionality through this method instead.
 187             nameCheckScanner.scan(element);
 188         }
 189 
 190         /**
 191          * Visitor to implement name checks.
 192          */
 193         private class NameCheckScanner extends ElementScanner7<Void, Void> {
 194             // The visitor could be enhanced to return true/false if
 195             // there were warnings reported or a count of the number
 196             // of warnings.  This could be facilitated by using
 197             // Boolean or Integer instead of Void for the actual type
 198             // arguments.  In more detail, one way to tally the number
 199             // of warnings would be for each method to return the sum
 200             // of the warnings it and the methods it called issued, a
 201             // bottom-up computation.  In that case, the first type
 202             // argument would be Integer and the second type argument
 203             // would still be Void.  Alternatively, the current count
 204             // could be passed along in Integer parameter p and each
 205             // method could return the Integer sum of p and the
 206             // warnings the method issued.  Some computations are more
 207             // naturally expressed in one form instead of the other.
 208             // If greater control is needed over traversal order, a
 209             // SimpleElementVisitor can be extended instead of an
 210             // ElementScanner.
 211 
 212             /**
 213              * Check the name of a type and its enclosed elements and


 295                 // elements, is a design decision based on what a
 296                 // PackageElemement is used to mean in this context.
 297                 // A PackageElement can represent a whole package, so
 298                 // it can provide a concise way to indicate many
 299                 // user-defined types should be visited.  However, a
 300                 // PackageElement can also represent a
 301                 // package-info.java file, as would be in the case if
 302                 // the PackageElement came from
 303                 // RoundEnvironment.getRootElements.  In that case,
 304                 // the package-info file and other files in that
 305                 // package could be passed in.  Therefore, without
 306                 // further checks, types in a package could be visited
 307                 // more than once if a package's elements were visited
 308                 // too.
 309                 return null;
 310             }
 311 
 312             @Override
 313             public Void visitUnknown(Element e, Void p) {
 314                 // This method will be called if a kind of element
 315                 // added after JDK 7 is visited.  Since as of this
 316                 // writing the conventions for such constructs aren't
 317                 // known, issue a warning.
 318                 messager.printMessage(WARNING,
 319                                       "Unknown kind of element, " + e.getKind() +
 320                                       ", no name checking performed.", e);
 321                 return null;
 322             }
 323 
 324             // All the name checking methods assume the examined names
 325             // are syntactically well-formed identifiers.
 326 
 327             /**
 328              * Return {@code true} if this variable is a field named
 329              * "serialVersionUID"; false otherwise.  A true
 330              * serialVersionUID of a class has type {@code long} and
 331              * is static and final.
 332              *
 333              * <p>To check that a Serializable class defines a proper
 334              * serialVersionUID, run javac with -Xlint:serial.
 335              *