Java - anonymous instance of abstract class [duplicate] - java

This question already has answers here:
Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic?
(19 answers)
Closed 8 years ago.
Type mismatch: cannot convert from BytecodeCodeProcessor<new AbstractBytecodeCodeVisitor(){}> to BytecodeCodeProcessor<AbstractBytecodeCodeVisitor>
public abstract class AbstractBytecodeCodeVisitor {
}
public class BytecodeCodeProcessor
<T extends AbstractBytecodeCodeVisitor> {
public BytecodeCodeProcessor(ClassSourceResult classSourceResult,
T visitor) {
}
}
BytecodeCodeProcessor<AbstractBytecodeCodeVisitor> processor =
new BytecodeCodeProcessor<>(classSourceResult,
new AbstractBytecodeCodeVisitor() {
});

The anonymous class instantiated via new AbstractBytecodeCodeVisitor() {} is a subclass of AbstractBytecodeCodeVisitor. This anonymous subclass is not equal to the generic type parameter specified by BytecodeCodeProcessor<AbstractBytecodeCodeVisitor>. Anonymous AbstractBytecodeCodeVisitor != AbstractBytecodeCodeVisitor, thus the compilation error. The code can be fixed in numerous ways, some of which are listed in the following link.
Generics and anonymous classes (bug or feature?)
One solution:
BytecodeCodeProcessor<AbstractBytecodeCodeVisitor> processor =
new BytecodeCodeProcessor<AbstractBytecodeCodeVisitor>(
classSourceResult, new AbstractBytecodeCodeVisitor() {}
);

Related

Why I can not overload this method? [duplicate]

This question already has answers here:
Method has the same erasure as another method in type
(8 answers)
Closed 1 year ago.
Here is the code:
public class Test {
private static void print(List<Plant> plants) {
}
private static void print(List<Animal> animals) {
}
}
class Animal {
}
class Plant {
}
When I want to overload print method in Test class for Animal and Plant lists, the compiler says that "both methods have same erasure". When I research "method erasure", I could not relate it to my situation.
Because both List<Plant> and List<Animal> are List (raw type) using type erasure, so compiler sees 2 print methods are identical.

Java generics - passing arguments to generic method signatures [duplicate]

This question already has answers here:
Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic?
(19 answers)
What is PECS (Producer Extends Consumer Super)?
(16 answers)
Closed 4 years ago.
I have the method below in my class
public void doNothing(Map<String, Collection<? extends Number>> value) {
}
and I'm calling this method with the parameter below.
private void loader() {
Map<String, ArrayList<Integer>> map = null;
doNothing(map);
}
I get the error below.
The method doNothing(Map<String,Collection<? extends Number>>) in the
type Lesson1<E> is not applicable for the arguments
(Map<String,ArrayList<Integer>>)
However if I changed the method to,
public void doNothing(Collection<? extends Number> something) {
}
and call it like below then there is no error.
private void loader() {
doNothing(new ArrayList<Integer>());
}
Can someone help me the problem with using the first method which takes in a map as an argument?
Thanks!!!

Up-casting template argument types [duplicate]

This question already has answers here:
Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic?
(19 answers)
Closed 7 years ago.
Why does Java not support automatic up-casting for template argument types?
For example, the following class will not compile unless the newly created Derived instance will be manually casted to a Base instance:
public class Example implements Iterable<Base> {
#Override
public Iterator<Base> iterator() {
return Arrays.asList(new Derived()).iterator();
}
private class Base {
}
private class Derived extends Base {
}
}
No need to cast.
The problem here is that Arrays.asList(new Derived()) naturally tries to create a List<Derived>,
and then calling .iterator() on a List<Derived> naturally gives an Iterator<Derived>,
which is not a sub-type of Iterator<Base>, so you get a compilation error.
You can specify that you want a List<Derived>, using Arrays.<Base>asList.
This works,
because you can certainly put a Derived instance into a List<Base>,
and then calling .iterator() on a List<Base> naturally gives an Iterator<Base>.
class Example implements Iterable<Base> {
#Override
public Iterator<Base> iterator() {
return Arrays.<Base>asList(new Derived()).iterator();
}
}

How to find a class of type variable in Java? [duplicate]

This question already has answers here:
Get generic type of class at runtime
(30 answers)
How do I get a class instance of generic type T?
(23 answers)
Closed 7 years ago.
How can I find the class of XOperation via Reflection API in Java?
public interface Operation<P extends Parameters> {
}
public class XParameters implements Parameters<XOperation> {
}
I'm trying to implement this method.
public <O extends Operation<P>, P extends Parameters> O getOperationByParametersClass(Class<P> parametersClass) {
// TODO
}
You can introduce a member in Parameters that will hold the Class of the type-parameter.
For example:
abstract class Parameters<T> {
protected Class<T> type;
}
Then, in the getOperationByParametersClass() method, you should pass an instance of Parameters (including subclasses):
public <O extends Operation, P extends Parameters<O>> Class<O>
getOperationByParametersClass(P parametersInstance) {
return parametersInstance.type;
}

Get class name for the generic class argument [duplicate]

This question already has answers here:
Name of Class <T> of a generic Class
(2 answers)
Closed 9 years ago.
I want to find the class name of the argument sent to the generic class as follows.
public abstract class RootClass<T extends Iface> {
protected ApplicationContext applicationContext;
public T getIfaceBean() {
return applicationContext.getBean(T.class);
}
}
But it looks like I can't do T.class (due to Type Erasure?).
So, Is such an action possible with Java Generics ?
How can I achieve this type of functionality with Java Generics?
Because of Type Erasure, you can't say T.class because T doesn't exist at runtime.
The best you can do is to take a parameter of type Class<T> to get the Class object:
public T getIfaceBean(Class<T> clazz) {

Categories

Resources