JavaVariable { static public List<JavaVariable> parseList(String s) throws TokenException { s = s.trim(); List<JavaVariable> vars = new ArrayList<>(); int offset = 0; while (offset < s.length()) { String typeOrName = parseToken(s, offset); offset += typeOrName.length(); if (offset >= s.length() || s.charAt(offset) == ',') { vars.add(new JavaVariable(null, typeOrName.trim())); } else { String name = parseToken(s, offset); offset += name.length(); vars.add(new JavaVariable(typeOrName.trim(), name.trim())); } if (offset < s.length()) { offset++; } } return vars; } 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 parseList() throws Exception { List<JavaVariable> vars; vars = JavaVariable.parseList(" "); Assert.assertEquals(0, vars.size()); vars = JavaVariable.parseList("User u"); Assert.assertEquals(1, vars.size()); Assert.assertEquals(new JavaVariable("User", "u"), vars.get(0)); vars = JavaVariable.parseList("u"); Assert.assertEquals(1, vars.size()); Assert.assertEquals(new JavaVariable(null, "u"), vars.get(0)); vars = JavaVariable.parseList("User u, String s"); Assert.assertEquals(2, vars.size()); Assert.assertEquals(new JavaVariable("User", "u"), vars.get(0)); Assert.assertEquals(new JavaVariable("String", "s"), vars.get(1)); vars = JavaVariable.parseList("Map<String, Object> m , List<Map<String,Object>> l "); Assert.assertEquals(2, vars.size()); Assert.assertEquals(new JavaVariable("Map<String, Object>", "m"), vars.get(0)); Assert.assertEquals(new JavaVariable("List<Map<String,Object>>", "l"), vars.get(1)); vars = JavaVariable.parseList("k,v"); Assert.assertEquals(2, vars.size()); Assert.assertEquals(new JavaVariable(null, "k"), vars.get(0)); Assert.assertEquals(new JavaVariable(null, "v"), vars.get(1)); vars = JavaVariable.parseList("java.lang.String s,v"); Assert.assertEquals(2, vars.size()); Assert.assertEquals(new JavaVariable("java.lang.String", "s"), vars.get(0)); Assert.assertEquals(new JavaVariable(null, "v"), vars.get(1)); vars = JavaVariable.parseList("java.lang.String s,v"); Assert.assertEquals(2, vars.size()); Assert.assertEquals(new JavaVariable("java.lang.String", "s"), vars.get(0)); Assert.assertEquals(new JavaVariable(null, "v"), vars.get(1)); vars = JavaVariable.parseList("java.lang.String[] s,v"); Assert.assertEquals(2, vars.size()); Assert.assertEquals(new JavaVariable("java.lang.String[]", "s"), vars.get(0)); Assert.assertEquals(new JavaVariable(null, "v"), vars.get(1)); vars = JavaVariable.parseList("java.lang.String[][] s,v"); Assert.assertEquals(2, vars.size()); Assert.assertEquals(new JavaVariable("java.lang.String[][]", "s"), vars.get(0)); Assert.assertEquals(new JavaVariable(null, "v"), vars.get(1)); vars = JavaVariable.parseList("java.lang.String [ ] [ ] s"); Assert.assertEquals(1, vars.size()); Assert.assertEquals(new JavaVariable("java.lang.String [ ] [ ]", "s"), vars.get(0)); Assert.assertEquals("java.lang.String[][]", vars.get(0).getType()); }