简体   繁体   中英

check for naming convention in java

through java reflection how to check that method name is in camelCase? eg

import java.lang.reflect.*;

   public class TestClass {
      private int simpleMethod(
       Object p, int x) throws NullPointerException
      {
         if (p == null)
            throw new NullPointerException();
         return x;
      }

      public static void main(String args[])
      {
         try {
           Class cls = Class.forName("method1");

            Method methlist[] 
              = cls.getDeclaredMethods();
            for (int i = 0; i < methlist.length;
               i++) {  
               Method m = methlist[i];
               System.out.println("name= " + m.getName());

            }
         }
         catch (Throwable e) {
            System.err.println(e);
         }
      }
   }

here i am getting method names simpleMethod and main i have to check that these names are in camelCase.

This problem is ill-defined. It's easy to check if a string of letters starts with a lowercase ( [az][a-zA-z]* ), or to chop them up where uppercases are (see eg How do I convert CamelCase into human-readable names in Java? ) and verify that they are words from some given dictionary, and things like that, but unless you're told where to look for uppercases, it's almost impossible to check if a string is a semantically proper camel case or not.

  • Is bitterAFlop a proper camel case? Maybe it should've been bitTeraflop ?
  • What about getLinenUmber ? (Yes! "umber" is a word!)
  • What about words from other languages?

Use a regex to check if it matches camelCase. Related answers: What Perl regex can match CamelCase words?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM