1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | package Torello.Java; class IsJavaTypeStr { static boolean is(String s) { if (s.length() == 0) return false; // Java restricts the first character of a java-identifier to a smaller subset than the // other characters in an identifier. Use method 'isJavaIdentifierStart' if (! Character.isJavaIdentifierStart(s.charAt(0))) return false; int len = s.length(); char c = 0; for (int i=1; i < len; i++) if (! Character.isJavaIdentifierPart(c = s.charAt(i))) { if (c == '.') { // A second (subsequent) period-character (in a row) ==> FALSE if (s.charAt(i-1) == '.') return false; // The LAST character in the String is a period-character ==> FALSE if (i == (len-1)) return false; // The character immediately following a period isn't a valid Java Identifier // Start ==> FALSE if (! Character.isJavaIdentifierStart(s.charAt(++i))) return false; } else // Character is NEITHER a period, NOR a Java Identifier Part ==> FALSE return false; } // All metrics / tests have succeeded (which would have resulted in immediate exiting of // this method, and a FALSE return value) ... therefore return TRUE. return true; } } |