Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Types generation from strings. #30

Closed
cushon opened this issue Oct 31, 2014 · 2 comments
Closed

Types generation from strings. #30

cushon opened this issue Oct 31, 2014 · 2 comments

Comments

@cushon
Copy link
Collaborator

cushon commented Oct 31, 2014

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)".

@cushon
Copy link
Collaborator Author

cushon commented Oct 31, 2014

Original comment posted by eaftan@google.com on 2012-08-03 at 10:45 PM


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.


Status: Started
Owner: eaftan@google.com
Labels: Priority-Medium

@cushon
Copy link
Collaborator Author

cushon commented Oct 31, 2014

Original comment posted by eaftan@google.com on 2012-09-07 at 10:29 PM


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);


Status: Fixed
CC: sjnickerson@google.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant