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

I want to register a TypeParser that operates on all subclasses of the provided class. #7

Closed
Elisedlund opened this issue Apr 15, 2014 · 4 comments

Comments

@Elisedlund
Copy link

*I want to register a TypeParser that operates on all subclasses of the provided class.
More concretely I want to do this:
builder.registerTypeParser(Number.class, new TypeParser() {...})
return parser.parseType(string, invocation.getMethod().getGenericReturnType()); //returntype is Integer

My problem that there exist a lot of subtypes of my superclass and I don't want to register the same TypeParser for all(I might not know all of them) of them .

I also though and tried this:
builder.registerTypeParser(new GenericType<? extends Number>() {},new TypeParserImplementation());
which unfortunately isn't allowed.

If a TypeParser for a specific type is not found could it look for a TypeParser where TypeParserForClass.isAssignableFrom(SpecificClassWithoutTypeParser.class)

*If such subclass typeParsing as above is added the expected type would be nice to access from within a TypeParser.
new TypeParser() {
public ConfigurationData parse(String input,Class expectedType, TypeParserHelper helper) {
if(expectedType instanceof Integer){}
if(expectedType instanceof Float){}
}

@drapostolos
Copy link
Owner

Great suggestion! Internally there is such feature, for List, Map, Set etc. But it is hardcoded :/
I'll try to figure out a way to make it generic and public. Any suggestions/ideas would be great :)

Regarding the "expectedType", It should be possible to add a method:
TypeParserHelper.getTargetType() : Type
(TypeParserHelper is there to provide new functionality in a backward compatible way)

@Elisedlund
Copy link
Author

yes TypeParserHelper.getTargetType() is even better.

@drapostolos
Copy link
Owner

The above enhancement-request has been pushed to the master branch.

Usage example:

TypeParser parser = TypeParser.newBuilder()  
 .setsplitStrategy(SplitStrategy)  
 .registerTypeParserForTypesAssignableTo(Class<?>, StringToTypeParser)  
 .setInputPreprocessor(InputPreprocessor)  
 .build()

will be available in v0.2.0

@Elisedlund
Copy link
Author

Works precisely as I intended. Thank you.

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

2 participants