I would like to use a custom serializer for Maps, but only if they are <String, String> maps. I attempted to register a StdSerializer<Map,String, String>> following the advice in this post like so: My Serializer looks as follows: This works fine on Map<String, String> objects, but unfortunately it also gets called for other maps as well. How does one restrict
Tag: generics
Avoid cast in generic factories
Suppose I have a class structure looking like this: Now I have a class that uses those intervals and factories: Is this the right way of approaching the problem if a class needs to create instances from a type variable? Is there a way around the cast in the SomeClass constructor? The obvious things don’t work – having a wildcard
Factory pattern using generics
I would like to build a class that caches classes of type CachedObject using Map. Below is the factory class. I have a class that extends CacheableObject as below: When I try to create an instance of class X that extends CachedObject using the get method in the factory as below: (please note that cachedObjectFactory is autowired using Spring) I
Parameter to pass in for type Class
I am trying to make a generic Stack class as shown but am unsure of what to put in for type when instantiating since I thought of doing however I get the following error of so I thought of using but I am unsure of what to put inside of cast() since it won’t take now I am stuck. Answer
Cannot use base class without passing its type in Java
I want to create a common service and common request as shown below: Then implement this service as shown below: However, although CompanyARequest is inherited from CommonRequest, createOrUpdate method throws “Method does not override method from its superclass” error. On the other hand, if I use generic for the request, the error is gone, but if I use generic for
void with Generics in Java
I have a function that returns void Generic method Implementation of generic interface How can I use void in IRequestHandler<DeleteProductCommand, Void> so that I can map void from iProductService.delete(deleteProductCommand.id); Answer Option 1: Just return null: Option 2: Update the IProductService::delete method to return something meaningful, e.g. a boolean value like Collection::remove does:
Java Generics – Type must be from Class T “or” extend S
I am developing a ComboBox (more for context than actual significance), and I would like to know if the “or” operator exists in Java generics. For now, the declaration looks something like that: Where ComboBoxItem is an interface I created for items that can be used in the ComboBox. However, I’d also like to accept String as a type, which
x cannot be resolved or is not a field y cannot be resolved or is not a field
Main: Location: Foo class: When I try to run the program I get the “question title” error, I don’t know what that happens, and how to fix that. Answer One way is to use instanceof operator if you are expecting a type that you know. This is a demo. UPDATE As Stephen C mentioned that Foo class potentially needs hard-coded
Assigning datatype to a generic type of variable based on a condition in Java
I have a usecase of supporting multiple datatypes for same variable. So tried to use Generics. For example The above code does not work. Can anyone please suggest a good way of doing it? Answer I changed the class names. You declared the type with a <?> parameter, and that is what is causing you problems. I resolved this by
Is it possible to return the Class of a generic type without any warning?
The question is pretty simple, take a look at the following code: In eclipse and maybe other ide, a warning is generated: So i did change the return type of the getApplicableClass function to: But the question now is, how can i return a Class<ChoiceBox<?>> without warning Answer This case is explicitly treated in the book Effective Java by Joshua