package com.openwaf.test.basic;
public class MethodArgumentTest {
static interface Inf{}
static class One<E extends Inf > implements Inf{
public <T extends One> T get(T k){
return k;
}
}
static class Two<E extends Inf> extends One<E>{ }
public static void test(){
One o=new One<Inf>();
Two t=new Two<One>();
o.<Two>get(t);
}
}
Above code is just for testing purpose. IMHO it should compile without any problem ,but java compiler says
MethodArgumentTest.java:15:
get(com.openwaf.test.basic.MethodArgumentTest.One) in
com.openwaf.test.basic.MethodArgumentTest.One cannot be applied to
(com.openwaf.test.basic.MethodArgumentTest.Two)
o.get(t);
1 error
Can someone please help me out here ?
Ok, as you said this is for testing only I will not ask what this code is good for. The following one compiles but still produces a warning. You were not consequent enough defining the generics:
public class MethodArgumentTest {
static interface Inf {
}
static class One<E extends Inf> implements Inf {
public <T extends One<E>> T get(T k) {
return k;
}
}
static class Two<E extends Inf> extends One<E> {
}
public static void test() {
One<Inf> o = new One<Inf>();
Two<One<Inf>> t = new Two<One<Inf>>();
o.<Two> get(t); /* unchecked warning */
}
}
I think what you need to do is
Two tPrime= o.get(t);
Related
I'm stuck with this problem for several hours. I'm trying to find an equivalent method for C#.
Java, works:
public class Main
{
public static void main(String[] args)
{
ArrayList<BaseList<? extends Base>> list = new ArrayList<>();
list.add(new DerivedList());
}
}
public class BaseList<T extends Base>
{
}
public class Base
{
}
public class DerivedList extends BaseList<Derived>
{
}
public class Derived extends Base
{
}
I need an equivalent method for ArrayList<BaseList<? extends Base>> in C#. I hope someone help me.
And is it posible in C# to wildcard your variables??
You cannot do that exactly as you describe, but there are workarounds. One is mentioned in another answer, another is to use interface instead:
public class Main
{
public static void main(String[] args)
{
var list = new List<IBaseList<Base>>();
list.Add(new DerivedList());
}
}
// note "out" here
public interface IBaseList<out T> where T : Base {
}
public class BaseList<T> : IBaseList<T> where T : Base {
}
public class Base {
}
public class DerivedList : IBaseList<Derived> {
}
public class Derived : Base {
}
C# uses runtime type reification, whereas Java uses type erasure. Which means that in Java, ArrayList<Foo> is the same class as ArrayList<Bar> at runtime. This is not the case in C#, so you can't just throw away the type parameter like that.
You can try to work around that like this:
public abstract class BaseList
{
}
public class BaseList<T> : BaseList
where T : Base
{
}
Then use a List<BaseList>
I have generic class :
public class Test<T> {
private Test<? extends T> myInstance;
public Test<? extends T> getInstance () {
return myInstance;
}
public void setInstance (Test<? extends T> argType) {
this.myInstance = argType;
}
}
And I have two classes in my class hierarchy relations:
public abstract class Alphabet {
//code here
}
and
public class A extends Alphabet{
public A() {
super();
System.out.print("This is A call");
}
}
Finally I have a class where I want to store make generic class Test with particular type and set new Instance of Object -> A through setInstance() method:
public static void main(String[] args) {
List<Alphabet> list = new ArrayList<Alphabet>();
Test<Alphabet> tAlphabet = new Test<Alphabet>();
tAlphabet.setInstance(new A()); //Here is compilation ERROR
}
But I have got the compilation error in line tAlphabet.setInstance(new A());
What is the issue with my generic class?
Your instance is a Test object as it's currently written, and you are supplying it with an Alphabet object instead. You probably want your instance to be of type Alphabet:
public class Test<T> {
private T myInstance;
public T getInstance() {
return myInstance;
}
public void setInstance(T argType) {
myInstance = argType;
}
}
This way, your Test stores an Alphabet instead of another Test.
It seems you have made things more complicated than needed. You probably want this in your Test class instead of what you actually have:
private T myInstance;
public T getInstance () {
return myInstance;
}
public void setInstance (T argType) {
this.myInstance = argType;
}
With this arrangement you would be free to setInstance(new A()) on a Test<Alphabet> instance.
I have some code as follows (excerpt):
public interface Event<S> {
S getSource();
}
public interface Subscriber<E> {
void update(E event);
}
public interface EventPublisher<S, E extends Event<S>> {
void addSubscription(S source, Subscriber<E> subscriber);
void removeSubscription(S source, Subscriber<E> subscriber);
}
public class SubscriptionManager<S, E extends Event<S>> implements Subscriber<E>, EventPublisher<S, E> {
...
}
public class MyEvent implements Event<MyEventSource> {
...
}
This all works fine, however, my problem is when I try something like this:
public class MyEventHandler {
private final SubscriptionManager<Class<? extends Event<?>>, ? extends Event<?>> subscriptionManager = new SubscriptionManager<>();
Subscriber<? extends Event<?>> subscriber = ...;
subscriptionManager.addSubscription(MyEvent.class, subscriber); **// Compile error**
}
I get the following error:
The method addSubscription(Class<? extends Event<?>>, Subscriber<capture#3-of ? extends Event<?>>) in the type SubscriptionManager<Class<? extends Event<?>>,capture#3-of ? extends Event<?>> is not applicable for the arguments (Class<MyEvent>, Subscriber<capture#5-of ? extends Event<?>>)
Can anyone tell me what's wrong?
Thanks
To be honest with you I think there's some design error in your code. It almost looks like a perfectly designed pattern, but something doesn't add up. You can probably omit half of generic parameters and make it more straightforward.
Please consider the code below. Entire framework is parametrized by a single parameter. Everything compiles and there are no raw types used.
Also, note that MyEvent is never used in the framework definition. It's a convenience class.
You can safely invoke subscriptionManager.update(new MyEvent()); somewhere in your code.
More complicated arrangements are possible too, but I believe that's the one you need.
Please let me know if that works for you.
static interface Event<S> {
S getSource();
}
static interface Subscriber<S> {
void update(Event<S> event);
}
static interface EventPublisher<S> {
void addSubscription(Class<S> sourceClass, Subscriber<S> subscriber);
void removeSubscription(Class<S> sourceClass, Subscriber<S> subscriber);
}
static class SubscriptionManager<S> implements Subscriber<S>, EventPublisher<S> {
public void addSubscription(Class<S> sourceClass, Subscriber<S> subscriber) {
}
public void removeSubscription(Class<S> sourceClass, Subscriber<S> subscriber) {
}
public void update(Event<S> event) {
}
}
static class MyEvent implements Event<String> {
public String getSource() {
return null;
}
}
static class MyEventHandler {
private final SubscriptionManager<String> subscriptionManager = new SubscriptionManager<String>();
public MyEventHandler() {
Subscriber<String> subscriber = null;
subscriptionManager.addSubscription(String.class, subscriber);
}
}
EDIT: This turned out not be a problem with the code at all, but with a bug in the Groovy Eclipse plugin (http://jira.codehaus.org/browse/GRECLIPSE-373)
Eclipse is giving me a weird error message about ambiguous types in a Java program and I really don't understand why. I have an interface that takes a generic parameter indicating what type of data it returns.
public interface InterfaceA<T> {
T getData();
}
One of the implementations of it looks like this:
public class Impl<T extends AnotherClass> implements InterfaceA<Collection<T>> {
public Collection<T> getData() {
// get the data
}
}
There is also a container for an InterfaceA
public class Container<T extends InterfaceA>
{
private T a;
public Container(T a) {
this.a = a;
}
public T getA() {
return a;
}
}
Doing this causes the "getData is ambiguous" error.
Container<Impl<AnotherClass>> c = new Container(new Impl<AnotherClass>());
Collection<AnotherClass> coll = c.getA().getData();
I'm stumped on this one.
There appears to be a bug causing this from the groovy plugin. http://jira.codehaus.org/browse/GRECLIPSE-373. It is not a java problem at all. Thanks for the help and my apologies.
Collection<T> getData() defined in Impl needs to be made public. If I do this the code compiles cleanly for me.
As other posters have said, I am not seeing this problem on Eclipse 3.5.0 running on JDK 1.6.0.14 (when fixing the reduced visibility of the getData() method).
I suggest doing a clean build (Project/Clean in Eclipse). Also, the Eclipse and Java version you are running might help.
-- Flaviu Cipcigan
Your edited example works fine for me (JDK 1.5) with the exception, that you have to define the generic type on the constructor. Here is my complete working code:
public interface InterfaceA<T> {
T getData();
}
public static class Impl<T extends Date> implements InterfaceA<Collection<T>> {
public Collection<T> getData() {
return null;
}
}
public static class Container<T extends InterfaceA> {
private T a;
public Container(T a) {
this.a = a;
}
public T getA() {
return a;
}
}
public static void main(String[] args) {
Container<Impl<Date>> c = new Container<Impl<Date>>(new Impl<Date>());
Collection<Date> coll = c.getA().getData();
}
[Edit to reflect updated question]
This shouldn't even compile as you are reducing the visibility of the method from public to package scope:
public class Impl<T extends AnotherClass> implements InterfaceA<Collection<T>> {
Collection<T> getData() {
// get the data
}
}
And this still compiles for me (Eclispe 3.4, OS X, 1.5), so don't know what exactly is the issue:
package temp.tests;
import java.util.Collection;
public interface InterfaceA <T> {
T getData();
public static final class AnotherClass {}
public static final class Impl<T extends AnotherClass>
implements InterfaceA<Collection<T>>
{
public Collection<T> getData () {
return null;
}
}
public static class Container<T extends InterfaceA>
{
private T a;
public Container(T a) { this.a = a; }
public T getA() { return a; }
}
public static final class Test {
public static void main (String[] args) {
Container<Impl<AnotherClass>> c = new Container(new Impl<AnotherClass>());
Collection<AnotherClass> coll = c.getA().getData();
}
}
}
What you have here seems legitimate. Perhaps Eclipse is showing an error it otherwise should not.
Go to Windows > Preferences > Java > Compiler > Errors/Warnings. In the "Generic Types" section, ensure that Eclipse isn't reporting an error for any of the operations listed (unless you want it too). I have all mine in that section set to "Warning". I would then try refreshing the project and restarting Eclipse.
Edit:
After the updated post was made, I got a warning (not an error still) on the lines of usage, saying "Container is a raw type. References to generic type Container should be parameterized:". This can be fixed by:
Container<Impl<Date>> c = new Container<Impl<Date>>(new Impl<Date>());
(In my example, I'm using java.util.Date as in place of 'AnotherClass').
I wanted to create an interface for copying an object to a destination object of the same class. The simple way is to use casting:
import org.junit.Test;
import org.junit.internal.runners.JUnit4ClassRunner;
import org.junit.runner.RunWith;
#RunWith(JUnit4ClassRunner.class)
public class TestGenerics {
public static interface Copyable {
public void copy(Copyable c);
}
public static class A implements Copyable {
private String aField = "--A--";
protected void innerCopy(Copyable c) {
A a = (A)c;
System.out.println(a.aField);
}
public void copy(Copyable c) {
innerCopy(c);
}
}
public static class B extends A {
private String bField = "--B--";
protected void innerCopy(Copyable c) {
B b = (B)c;
super.innerCopy(b);
System.out.println(b.bField);
}
}
#Test
public void testCopy() {
Copyable b1 = new B();
Copyable b2 = new B();
b1.copy(b2);
}
}
But also i've found a way it can be done using generics:
import org.junit.Test;
import org.junit.internal.runners.JUnit4ClassRunner;
import org.junit.runner.RunWith;
#RunWith(JUnit4ClassRunner.class)
public class TestGenerics {
public static interface Copyable<T> {
public void copy(T t);
}
public static class A<T extends A<?>> implements Copyable<T> {
private String a = "--A--";
public void copy(T t) {
System.out.println(t.a);
}
}
public static class B<T extends B<?>> extends A<T> {
private String b = "--B--";
public void copy(T t) {
super.copy(t);
System.out.println(t.b);
}
}
#Test
#SuppressWarnings("unchecked")
public void testCopy() {
Copyable b1 = new B();
Copyable b2 = new B();
b1.copy(b2);
}
}
Though the only way i've found to get rid of warnings is the annotation. And it feels like something is wrong.
So what's wrong? I can accept that something is wrong in the root of the problem. So any sort of clarification is welcome.
Your interface definition:
public interface Copyable<T extends Copyable<T>> {
void copy(T copyFrom);
}
Your implementation:
public class Example implements Copyable<Example> {
private Object data;
void copy(Example copyFrom) {
data = copyFrom.data;
}
//nontrivial stuff
}
That should take care of your warnings.
Assuming you don't want to subclass further you just need:
public static /*final*/ class AClass implements Copyable<AClass> {
For an abstract class, you do the "enum" thing:
public static abstract class AClass<T extends AClass<T>> implements Copyable<T> {
In testCopy, one of the warnings is because you're instantiating a "raw type" of Copyable rather than some concrete Copyable<T>. Once you instantiate a Copyable, it can only be applied to Ts (which include subtypes of T). In order to instantiate with a formal type, the class definitions will need to be changed slightly:
public static class A<T extends A> implements Copyable<T>
public static class B<T extends B> extends A<T>
The next issue is that a Copyable<B> can only be passed a compile-time type of B (based on the definition of Copyable). And testCopy() above is passing it a compile-time type of Copyable. Below are some examples of what will work, with brief descriptions:
public void testExamples()
{
// implementation of A that applies to A and subtypes
Copyable<A> aCopier = new A<A>();
// implementation of B that applies to B and subtypes
Copyable<B> bCopier = new B<B>();
// implementation of A that applies to B and subtypes
Copyable<B> bCopier2 = new A<B>();
}
I keep trying to figure out a way to get rid of the warnings in your first approach and I can't come up with anything that works. Even so, I think the first approach is the lesser of two evils. An unsafe cast is better than needing to give your classes such a complicated api.
A completely separate approach would be to override Object.clone() and implement Cloneable.
This is the best possible code of second approach. It compiles without any warnings.
import static org.junit.Assert.fail;
import org.junit.Test;
import org.junit.internal.runners.JUnit4ClassRunner;
import org.junit.runner.RunWith;
#RunWith(JUnit4ClassRunner.class)
public class TestGenerics {
public static interface Copyable<T> {
public void copy(T t);
}
public static class A<T extends A<T>> implements Copyable<T> {
private String a = "--A--";
public void copy(T t) {
System.out.println(t.a);
}
#SuppressWarnings("unchecked")
public static Copyable<Object> getInstance() {
return new A();
}
}
public static class B<T extends B<T>> extends A<T> {
private String b = "--B--";
public void copy(T t) {
super.copy(t);
System.out.println(t.b);
}
#SuppressWarnings("unchecked")
public static Copyable<Object> getInstance() {
return new B();
}
}
#Test
public void testCopy() {
Copyable<Object> b1 = B.getInstance();
Copyable<Object> b2 = B.getInstance();
Copyable<Object> a = A.getInstance();
b1.copy(b2); // this works as intended
try {
b1.copy(a); // this throws ClassCastException
fail();
} catch (ClassCastException cce) {
}
}
}
And also i figured out all that happens in this program with help of reflection:
for (Method method : A.class.getMethods()) {
if (method.getName().equals("copy")) {
System.out.println(method.toString());
}
}
for (Method method : B.class.getMethods()) {
if (method.getName().equals("copy")) {
System.out.println(method.toString());
}
}
Here is the output:
public void com.sbp.core.TestGenerics$A.copy(com.sbp.core.TestGenerics$A)
public void com.sbp.core.TestGenerics$A.copy(java.lang.Object)
public void com.sbp.core.TestGenerics$B.copy(com.sbp.core.TestGenerics$B)
public void com.sbp.core.TestGenerics$B.copy(com.sbp.core.TestGenerics$A)
public void com.sbp.core.TestGenerics$A.copy(java.lang.Object)
It means that:
The copy(...) methods in A and B make compiler generate "bridges" -
2 different methods for each, one with reifed argument type from
ancestor (reified T from Copyable becomes Object, reified "T extends
A" from A becomes A) and that is why it's override and not overload,
and the other one with reified argument type for defining class. First
method (with autogenerated body) downcasts its argument to call the
second (they call it a bridge). Because of this downcasting we get
ClassCastException in runtime if we call b1.copy(a).
It looks like direct type casting is cleaner and better tool for my
problem and generics are better used in their direct purpose - to
enforce compile time type checking.
I've learned Scala and now i know that the thing i wanted 2 years ago could have been achieved with contravariant type parameter and Scala's type system:
trait CopyableTo[-T] {
def copyTo(t: T)
}
class A(private var a: Int) extends CopyableTo[A] {
override def copyTo(t: A) {
println("A:copy")
t.a = this.a
}
}
class B(private var a: Int, private var b: Int) extends A(a) with CopyableTo[B] {
def copyTo(t: B) {
println("B:copy")
super.copyTo(t)
t.b = this.b
}
}
#Test
def zzz {
val b1 = new B(1, 2)
val a1 = new A(3)
val b2 = new B(4, 5)
b1.copyTo(a1)
a1.copyTo(b1)
b1.copyTo(b2)
}
Java type system is too weak for this.