JavaVariable { static public String parseToken(String s, int offset) throws TokenException { int begin = -1; int nameEnd = -1; boolean generic = false; int nestedGeneric = 0; boolean array = false; int nestedArray = 0; int i = offset; for (; i < s.length(); i++) { char c = s.charAt(i); if (nestedGeneric > 0) { if (c == '<') { nestedGeneric++; } else if (c == '>') { nestedGeneric--; if (nestedGeneric <= 0) { generic = true; } } else { } } else if (nestedArray > 0) { if (RockerUtil.isWhitespace(c)) { } else if (c == ']') { nestedArray--; if (nestedArray <= 0) { array = true; } } else { throw new TokenException("Unexpected token '" + c + "' inside array delimiters"); } } else { if (c == '<') { if (generic) { throw new TokenException("Unexpected token '<': generic already defined"); } if (begin < 0) { throw new TokenException("Unexpected token '<': generic before name"); } if (array) { throw new TokenException("Unexpected token '<': generic cannot be after array delimiters"); } if (nameEnd < 0) { nameEnd = i - 1; } nestedGeneric++; } else if (c == '[') { if (begin < 0) { throw new TokenException("Unexpected token '[': array before name"); } if (nameEnd < 0) { nameEnd = i - 1; } nestedArray++; } else if (RockerUtil.isWhitespace(c)) { if (begin >= 0 && nameEnd < 0) { nameEnd = i - 1; } } else if (c == ',') { if (nameEnd < 0) { nameEnd = i - 1; } break; } else { if (begin < 0) { begin = i; } else if (nameEnd >= 0) { break; } else { } } } } if (nestedGeneric > 0) { throw new TokenException("Invalid java variable: closing generic token '>' not found"); } if (nestedArray > 0) { throw new TokenException("Invalid java variable: closing array token ']' not found"); } if (begin < 0) { throw new TokenException("Invalid java variable: name part never found"); } if (nameEnd < 0) { nameEnd = i; } return s.substring(offset, i); } JavaVariable(String type, String name); String getType(); String getTypeAsNonPrimitiveType(); String getName(); boolean hasType(); static JavaVariable parse(String s); static List<JavaVariable> parseList(String s); static String parseToken(String s, int offset); @Override int hashCode(); @Override boolean equals(Object obj); @Override String toString(); static final public Map<String,Class> PRIMITIVES; }
@Test public void parseToken() throws Exception { Assert.assertEquals("User", JavaVariable.parseToken("User", 0)); Assert.assertEquals(" User ", JavaVariable.parseToken(" User ", 0)); Assert.assertEquals(" User", JavaVariable.parseToken(" User, ", 0)); Assert.assertEquals("User[]", JavaVariable.parseToken("User[]", 0)); Assert.assertEquals("User[ ] ", JavaVariable.parseToken("User[ ] ", 0)); Assert.assertEquals("List<User> ", JavaVariable.parseToken("List<User> ", 0)); Assert.assertEquals("List<User>[]", JavaVariable.parseToken("List<User>[]", 0)); Assert.assertEquals("Map< String, Object >", JavaVariable.parseToken("Map< String, Object >", 0)); Assert.assertEquals("Map< String,Map<String,String> >", JavaVariable.parseToken("Map< String,Map<String,String> >", 0)); Assert.assertEquals("Map<String,Object> ", JavaVariable.parseToken("Map<String,Object> u", 0)); Assert.assertEquals("Map<String,Object> ", JavaVariable.parseToken("Map<String,Object> u,", 0)); String test = " String\tu,\nString s"; Assert.assertEquals(" String\t", JavaVariable.parseToken(test, 0)); Assert.assertEquals("u", JavaVariable.parseToken(test, 8)); Assert.assertEquals("\nString ", JavaVariable.parseToken(test, 10)); Assert.assertEquals("s", JavaVariable.parseToken(test, 18)); Assert.assertEquals("Map <String,Object> ", JavaVariable.parseToken("Map <String,Object> u,", 0)); Assert.assertEquals("Map < String , Object > [ ] [ ] ", JavaVariable.parseToken("Map < String , Object > [ ] [ ] u", 0)); try { JavaVariable.parseToken("<User u", 0); Assert.fail("expected exception"); } catch (TokenException e) { } try { JavaVariable.parseToken("User[<] u", 0); Assert.fail("expected exception"); } catch (TokenException e) { } try { JavaVariable.parseToken("User < u", 0); Assert.fail("expected exception"); } catch (TokenException e) { } try { JavaVariable.parseToken("", 0); Assert.fail("expected exception"); } catch (TokenException e) { } try { JavaVariable.parseToken(" ", 0); Assert.fail("expected exception"); } catch (TokenException e) { } try { JavaVariable.parseToken(" <>", 0); Assert.fail("expected exception"); } catch (TokenException e) { } }