/** * Formats a string into "THIS_KIND_OF_FORMAT_ABC_DEF". * * @return * Always return a string but there's no guarantee that * the generated code is a valid Java identifier. */ public String toConstantName(String s) { return toConstantName(toWordList(s)); }
protected int classify(char c0) { if(c0=='_') return NameUtil.OTHER_LETTER; return super.classify(c0); } };
public String toVariableName(String s) { return toMixedCaseName(toWordList(s), false); } public String toInterfaceName( String token ) {
if (!isPunct(s.charAt(i))) break; i++; int b = nextBreak(s, i); String w = (b == -1) ? s.substring(i) : s.substring(i, b); ss.add(escape(capitalize(w))); if (b == -1) break; i = b;
/** * Capitalizes the first character of the specified string, * and de-capitalize the rest of characters. */ public String capitalize(String s) { if (!isLower(s.charAt(0))) return s; StringBuilder sb = new StringBuilder(s.length()); sb.append(toUpperCase(s.charAt(0))); sb.append(toLowerCase(s.substring(1))); return sb.toString(); }
private int nextBreak(String s, int start) { int n = s.length(); char c1 = s.charAt(start); int t1 = classify(c1); for (int i=start+1; i<n; i++) { // shift (c1,t1) into (c0,t0) // char c0 = c1; --- conceptually, but c0 won't be used int t0 = t1; c1 = s.charAt(i); t1 = classify(c1); switch(actionTable[t0*5+t1]) { case ACTION_CHECK_PUNCT: if(isPunct(c1)) return i; break; case ACTION_CHECK_C2: if (i < n-1) { char c2 = s.charAt(i+1); if (isLower(c2)) return i; } break; case ACTION_BREAK: return i; } } return -1; }
/** * Computes a Java identifier from a local name. * * <p> * This method faithfully implements the name mangling rule as specified in the JAXB spec. * * <p> * In JAXB, a collision with a Java reserved word (such as "return") never happens. * Accordingly, this method may return an identifier that collides with reserved words. * * <p> * Use {@code JJavaName.isJavaIdentifier(String)} to check for such collision. * * @return * Typically, this method returns "nameLikeThis". */ public static @NotNull String mangleNameToVariableName(@NotNull String localName) { return NameConverter.standard.toVariableName(localName); }
/** * Computes a Java class name from a local name. * * <p> * This method faithfully implements the name mangling rule as specified in the JAXB spec. * This method works like {@link #mangleNameToClassName(String)} except that it looks * for "getClass" and returns something else. * * @return * Typically, this method returns "NameLikeThis". */ public static @NotNull String mangleNameToPropertyName(@NotNull String localName) { return NameConverter.standard.toPropertyName(localName); }
protected String toMixedCaseVariableName(String[] ss, boolean startUpper, boolean cdrUpper) { if (cdrUpper) for (int i = 1; i < ss.length; i++) ss[i] = capitalize(ss[i]); StringBuilder sb = new StringBuilder(); if( ss.length>0 ) { sb.append(startUpper ? ss[0] : toLowerCase(ss[0])); for (int i = 1; i < ss.length; i++) sb.append(ss[i]); } return sb.toString(); }
/** * Computes a Java class name from a local name. * * <p> * This method faithfully implements the name mangling rule as specified in the JAXB spec. * * @return * Typically, this method returns "NameLikeThis". */ public static @NotNull String mangleNameToClassName(@NotNull String localName) { return NameConverter.standard.toClassName(localName); }
public String toInterfaceName( String token ) { return toClassName(token); } public String toPropertyName(String s) {
public String toConstantName( String token ) { return super.toConstantName(token); } /**
protected boolean isLetter(char c) { return super.isLetter(c) || c=='_'; }
/** * Decide the action to be taken given * the classification of the preceding character 't0' and * the classification of the next character 't1'. */ private static byte decideAction( int t0, int t1 ) { if(t0==OTHER && t1==OTHER) return ACTION_CHECK_PUNCT; if(!xor(t0==DIGIT,t1==DIGIT)) return ACTION_BREAK; if(t0==LOWER_LETTER && t1!=LOWER_LETTER) return ACTION_BREAK; if(!xor(t0<=OTHER_LETTER,t1<=OTHER_LETTER)) return ACTION_BREAK; if(!xor(t0==OTHER_LETTER,t1==OTHER_LETTER)) return ACTION_BREAK; if(t0==UPPER_LETTER && t1==UPPER_LETTER) return ACTION_CHECK_C2; return ACTION_NOBREAK; }
public String toConstantName( String token ) { String name = super.toConstantName(token); if(!SourceVersion.isKeyword(name)) return name; else return '_'+name; } };
protected String toMixedCaseName(List<String> ss, boolean startUpper) { StringBuilder sb = new StringBuilder(); if(!ss.isEmpty()) { sb.append(startUpper ? ss.get(0) : toLowerCase(ss.get(0))); for (int i = 1; i < ss.size(); i++) sb.append(ss.get(i)); } return sb.toString(); }
/** * Formats a string into "THIS_KIND_OF_FORMAT_ABC_DEF". * * @return * Always return a string but there's no guarantee that * the generated code is a valid Java identifier. */ public String toConstantName(List<String> ss) { StringBuilder sb = new StringBuilder(); if( !ss.isEmpty() ) { sb.append(toUpperCase(ss.get(0))); for (int i = 1; i < ss.size(); i++) { sb.append('_'); sb.append(toUpperCase(ss.get(i))); } } return sb.toString(); }
/** * Escapes characters that are unusable as Java identifiers * by replacing unsafe characters with safe characters. */ private static String escape(String s) { int n = s.length(); for (int i = 0; i < n; i++) if (!Character.isJavaIdentifierPart(s.charAt(i))) { StringBuilder sb = new StringBuilder(s.substring(0, i)); escape(sb, s, i); return sb.toString(); } return s; } }
public String toClassName(String s) { return toMixedCaseName(toWordList(s), true); } public String toVariableName(String s) {
public String toPropertyName(String s) { String prop = toClassName(s); // property name "Class" with collide with Object.getClass, // so escape this. if(prop.equals("Class")) prop = "Clazz"; return prop; } public String toConstantName( String token ) {