langtools/src/sample/share/javac/processing/src/CheckNamesProcessor.java
changeset 36777 28d33fb9097f
parent 25874 83c19f00452c
equal deleted inserted replaced
36776:5e260fa5208a 36777:28d33fb9097f
     1 /*
     1 /*
     2  * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
     3  *
     3  *
     4  * Redistribution and use in source and binary forms, with or without
     4  * Redistribution and use in source and binary forms, with or without
     5  * modification, are permitted provided that the following conditions
     5  * modification, are permitted provided that the following conditions
     6  * are met:
     6  * are met:
     7  *
     7  *
    72  * <li> Such a {@code jar} file can now be used with the <i>discovery
    72  * <li> Such a {@code jar} file can now be used with the <i>discovery
    73  * process</i> without explicitly naming the processor to run:<br>
    73  * process</i> without explicitly naming the processor to run:<br>
    74  * {@code javac -processorpath procdir -proc:only CheckNamesProcessor.java}
    74  * {@code javac -processorpath procdir -proc:only CheckNamesProcessor.java}
    75  *
    75  *
    76  * </ol>
    76  * </ol>
    77  *
       
    78  * For some notes on how to run an annotation processor inside
       
    79  * NetBeans, see http://wiki.java.net/bin/view/Netbeans/FaqApt.
       
    80  *
    77  *
    81  * <h3>Possible Enhancements</h3>
    78  * <h3>Possible Enhancements</h3>
    82  * <ul>
    79  * <ul>
    83  *
    80  *
    84  * <li> Support an annotation processor option to control checking
    81  * <li> Support an annotation processor option to control checking
   136 
   133 
   137     @Override
   134     @Override
   138     public SourceVersion getSupportedSourceVersion() {
   135     public SourceVersion getSupportedSourceVersion() {
   139         /*
   136         /*
   140          * Return latest source version instead of a fixed version
   137          * Return latest source version instead of a fixed version
   141          * like RELEASE_9.  To return a fixed version, this class
   138          * like RELEASE_9. To return a fixed version, this class could
   142          * could be annotated with a SupportedSourceVersion
   139          * be annotated with a SupportedSourceVersion annotation.
   143          * annotation.
       
   144          *
   140          *
   145          * Warnings will be issued if any unknown language constructs
   141          * Warnings will be issued if any unknown language constructs
   146          * are encountered.
   142          * are encountered.
   147          */
   143          */
   148         return SourceVersion.latest();
   144         return SourceVersion.latest();
   309                 // more than once if a package's elements were visited
   305                 // more than once if a package's elements were visited
   310                 // too.
   306                 // too.
   311                 return null;
   307                 return null;
   312             }
   308             }
   313 
   309 
       
   310             /**
       
   311              * Check the name of a module.
       
   312              */
       
   313             @Override
       
   314             public Void visitModule(ModuleElement e, Void p) {
       
   315                 /*
       
   316                  * Implementing the checks of package names is left as
       
   317                  * an exercise for the reader.
       
   318                  */
       
   319 
       
   320                 // Similar to the options of how visiting a package
       
   321                 // could be handled, whether or not this method should
       
   322                 // call super and scan, etc. is a design choice on
       
   323                 // whether it is desired for a ModuleElement to
       
   324                 // represent a module-info file or for the
       
   325                 // ModuleElement to represent the entire contents of a
       
   326                 // module, including its packages.
       
   327                 return null;
       
   328             }
       
   329 
   314             @Override
   330             @Override
   315             public Void visitUnknown(Element e, Void p) {
   331             public Void visitUnknown(Element e, Void p) {
   316                 // This method will be called if a kind of element
   332                 // This method will be called if a kind of element
   317                 // added after JDK 7 is visited.  Since as of this
   333                 // added after JDK 9 is visited.  Since as of this
   318                 // writing the conventions for such constructs aren't
   334                 // writing the conventions for such constructs aren't
   319                 // known, issue a warning.
   335                 // known, issue a warning.
   320                 messager.printMessage(WARNING,
   336                 messager.printMessage(WARNING,
   321                                       "Unknown kind of element, " + e.getKind() +
   337                                       "Unknown kind of element, " + e.getKind() +
   322                                       ", no name checking performed.", e);
   338                                       ", no name checking performed.", e);
   401 
   417 
   402                 if (Character.isUpperCase(firstCodePoint)) {
   418                 if (Character.isUpperCase(firstCodePoint)) {
   403                     previousUpper = true;
   419                     previousUpper = true;
   404                     if (!initialCaps) {
   420                     if (!initialCaps) {
   405                         messager.printMessage(WARNING,
   421                         messager.printMessage(WARNING,
   406                                               "Name, ``" + name + "'', should start in lowercase.", e);
   422                                               "Name ``" + name + "'' should start in lowercase.", e);
   407                         return;
   423                         return;
   408                     }
   424                     }
   409                 } else if (Character.isLowerCase(firstCodePoint)) {
   425                 } else if (Character.isLowerCase(firstCodePoint)) {
   410                     if (initialCaps) {
   426                     if (initialCaps) {
   411                         messager.printMessage(WARNING,
   427                         messager.printMessage(WARNING,
   412                                               "Name, ``" + name + "'', should start in uppercase.", e);
   428                                               "Name ``" + name + "'' should start in uppercase.", e);
   413                         return;
   429                         return;
   414                     }
   430                     }
   415                 } else // underscore, etc.
   431                 } else // underscore, etc.
   416                     conventional = false;
   432                     conventional = false;
   417 
   433 
   432                     }
   448                     }
   433                 }
   449                 }
   434 
   450 
   435                 if (!conventional)
   451                 if (!conventional)
   436                     messager.printMessage(WARNING,
   452                     messager.printMessage(WARNING,
   437                                           "Name, ``" + name + "'', should be in camel case.", e);
   453                                           "Name ``" + name + "'', should be in camel case.", e);
   438             }
   454             }
   439 
   455 
   440             /**
   456             /**
   441              * Print a warning if the element's name is not a sequence
   457              * Print a warning if the element's name is not a sequence
   442              * of uppercase letters separated by underscores ("_").
   458              * of uppercase letters separated by underscores ("_").
   443              *
   459              *
   444              * @param e the element whose name will be checked
   460              * @param e the element whose name will be checked
   445              */
   461              */
   446             private void checkAllCaps(Element e) {
   462             private void checkAllCaps(Element e) {
   447                 String name = e.getSimpleName().toString();
   463                 String name = e.getSimpleName().toString();
   448                 if (e.getKind() == TYPE_PARAMETER) { // Should be one character
   464                 /*
       
   465                  * Traditionally type variables are recommended to
       
   466                  * have one-character names. As an exercise for the
       
   467                  * reader, a more nuanced policy can be implemented.
       
   468                  */
       
   469                 if (e.getKind() == TYPE_PARAMETER) {
   449                     if (name.codePointCount(0, name.length()) > 1 ||
   470                     if (name.codePointCount(0, name.length()) > 1 ||
   450                         // Assume names are non-empty
   471                         // Assume names are non-empty
   451                         !Character.isUpperCase(name.codePointAt(0)))
   472                         !Character.isUpperCase(name.codePointAt(0)))
   452                         messager.printMessage(WARNING,
   473                         messager.printMessage(WARNING,
   453                                               "A type variable's name,``" + name +
   474                                               "A type variable's name,``" + name +
   495     }
   516     }
   496 }
   517 }
   497 
   518 
   498 /**
   519 /**
   499  * Lots of bad names.  Don't write code like this!
   520  * Lots of bad names.  Don't write code like this!
       
   521  *
       
   522  * The unmodified name checks will print 11 warnings for this class.
   500  */
   523  */
   501 class BADLY_NAMED_CODE {
   524 class BADLY_NAMED_CODE {
   502     enum colors {
   525     enum colors {
   503         red,
   526         red,
   504         blue,
   527         blue,