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

Print this page


   1 /*
   2  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
   3  *
   4  * Redistribution and use in source and binary forms, with or without
   5  * modification, are permitted provided that the following conditions
   6  * are met:
   7  *
   8  *   - Redistributions of source code must retain the above copyright
   9  *     notice, this list of conditions and the following disclaimer.
  10  *
  11  *   - Redistributions in binary form must reproduce the above copyright
  12  *     notice, this list of conditions and the following disclaimer in the
  13  *     documentation and/or other materials provided with the distribution.
  14  *
  15  *   - Neither the name of Oracle nor the names of its
  16  *     contributors may be used to endorse or promote products derived
  17  *     from this software without specific prior written permission.
  18  *
  19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR


 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 section 6.8 of
 156      *     <cite>The Java&trade; Language Specification</cite>
 157      *
 158      * <ul>
 159      * <li> Classes and interfaces: camel case, first letter is uppercase
 160      * <li> Methods: camel case, first letter is lowercase
 161      * <li> Type variables: one uppercase letter


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


   1 /*
   2  * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
   3  *
   4  * Redistribution and use in source and binary forms, with or without
   5  * modification, are permitted provided that the following conditions
   6  * are met:
   7  *
   8  *   - Redistributions of source code must retain the above copyright
   9  *     notice, this list of conditions and the following disclaimer.
  10  *
  11  *   - Redistributions in binary form must reproduce the above copyright
  12  *     notice, this list of conditions and the following disclaimer in the
  13  *     documentation and/or other materials provided with the distribution.
  14  *
  15  *   - Neither the name of Oracle nor the names of its
  16  *     contributors may be used to endorse or promote products derived
  17  *     from this software without specific prior written permission.
  18  *
  19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR


 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_8.  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 section 6.8 of
 156      *     <cite>The Java&trade; Language Specification</cite>
 157      *
 158      * <ul>
 159      * <li> Classes and interfaces: camel case, first letter is uppercase
 160      * <li> Methods: camel case, first letter is lowercase
 161      * <li> Type variables: one uppercase letter


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