You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As far as I dug into the error-prone and javac there is no convenient way to get a Type from String. And sometimes you need to check if the result type is a subtype of/castable to java.lang.Object[] or java.util.List<Integer>, etc.
Of course for plain classes (e.g. java.lang.Object) you can make a look-up in symtab, but you can't do the same for array types and parametrized classes. (Am I wrong here?)
I think it would be great to have such a method in error-prone library.
Something like "Type getType(String typeName)".
The text was updated successfully, but these errors were encountered:
I looked into this a bit. You're right: for plain classes this is easy enough, but when you get into more complex stuff you start rebuilding the parse and attribution phases of the compiler.
What is we provided a TypeBuilder class that lets you build up a type programmatically? So you'd specify something like:
Type t = new TypeBuilder(basetype).setIsArray(true).build();
or
Type t = new TypeBuilder(basetype).setTypeParams(tparam1, tparam2, ...).build();
I would also provide a convenience method for getting simple types like java.lang.Object.
I committed a fix in revisions 82ee5e6, f56063d, 0fc2db4, and eaeae92. I provided two new instance methods in VisitorState:
getTypeFromString(typeStr) and getType(baseType, isArray, typeParams)
getTypeFromString returns a Type object from a simple (not an array, not generic) type string (e.g., "java.lang.Object"). It returns null if the compiler has not seen that type yet.
getType lets you construct complex types if you already have the baseType and parameter Types. This lets me avoid parsing a type string. For example, to construct the Type ArrayList<String>, you would do this:
Type baseType = getTypeFromString("java.util.ArrayList");
List<Type> typeParams = new ArrayList<Type>();
typeParams.add(getTypeFromString("java.lang.String");
getType(baseType, false, typeParams);
Original issue created by stepanp@google.com on 2012-07-27 at 11:14 AM
As far as I dug into the error-prone and javac there is no convenient way to get a Type from String. And sometimes you need to check if the result type is a subtype of/castable to java.lang.Object[] or java.util.List<Integer>, etc.
Of course for plain classes (e.g. java.lang.Object) you can make a look-up in symtab, but you can't do the same for array types and parametrized classes. (Am I wrong here?)
I think it would be great to have such a method in error-prone library.
Something like "Type getType(String typeName)".
The text was updated successfully, but these errors were encountered: