views:

81

answers:

2

through java reflection how to check that method name is in camelCase? e.g.

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.

+1  A: 

Use a regex to check if it matches camelCase. Related answers: http://stackoverflow.com/questions/815787/what-perl-regex-can-match-camelcase-words

Jason Hall
no that will not help me. what about these names main, getURL,etc.
Vivart
How about this? [a-z]+[a-zA-Z0-9]*
Jason Hall
but if somebody has written simplemethod, i have to return that its not in camelCase.
Vivart
Grab a dictionary. It's however going to be a lot of work. Good luck :)
BalusC
In that case, you'll also have to split on the camel-case words (http://stackoverflow.com/questions/773303/splitting-camelcase) and check that those words are in a dictionary.
Jason Hall
+3  A: 

This problem is ill-defined. It's easy to check if a string of letters starts with a lowercase ([a-z][a-zA-z]*), or to chop them up where uppercases are (see e.g. 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?
polygenelubricants
+1 That´s challenging
Juliano
thanks for explaining my question in a right way.
Vivart