• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

generic problem, Object to String

I am using an open source lib and can not get a method with generics to compile.

the declaration is
protected static Object chainASync(Future<?> future, GWT2Chain<Object> chain) {
     throw new GWT2ChainAsync(future, chain);
    }

The actual sample code is
      return (String) chain(new TestJob().now(), new GWT2Chain<String>() {

               public Object chain(String result) {

                 return chainASync(new TestJob().now(), new GWT2Chain<String>() {

                        public Object chain(String result) {

                           return chainASync(new TestJob().now(), new GWT2Chain<String>() {

                                 public Object chain(String result) {

                                             return result;
                                   }

                           });
                     }


               });  

The compiler gives
The method chainASync(Future<?>, GWT2Chain<Object>) in the type GWT2Service is not applicable for the arguments (F.Promise<String>, new GWT2Chain<String>(){})

you can see the only difference is GWT2Chain<String> is substituted for GWT2Chain<Object>

should java generics allow for such substitution? I am sure the author of the code was able to get the sample code to compile
0
bhomass
Asked:
bhomass
  • 7
  • 3
1 Solution
 
CEHJCommented:
Could you please post the code for TestJob?
0
 
for_yanCommented:
This
new TestJob().now()
should be of the type Furture
Is that the case?


 
0
 
bhomassAuthor Commented:
TestJob is not the problem. I have tried replacing the first param with null, and it still has the same compilation error. if I replace the 2nd and leave the 1st, then no error.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
for_yanCommented:
You have to put Object there:

chainASync(new TestJob().now(), new GWT2Chain<Object>() {..});
0
 
for_yanCommented:
This would also work (see below) but I guess you cannot modify
your  chainASync()  method:


import java.util.*;

public class Miscellaneous {
    public static void main(String[] args) {

        ArrayList <String>ar = new ArrayList<String>();

        doThis(ar);

       }

    public static void doThis (ArrayList<? extends Object> ar){

    }
}

Open in new window

0
 
for_yanCommented:
No, I don't think the author could have compiled it.

Thsi is a simple example:

This code will compile fine:
public class Miscellaneous {
    public static void main(String[] args) {


      GWT2Chain<Object> cc =  new GWT2Chain<Object>();

        chain(cc);

       }




 static Object   chain(GWT2Chain<Object> c){
            Object o = null;
     return o;
 }

}

class GWT2Chain<T>{



  

        }

Open in new window




Howevre this code:
import java.util.*;

public class Miscellaneous {
    public static void main(String[] args) {


      GWT2Chain<String> cc =  new GWT2Chain<String>();

        chain(cc);

       }




 static Object   chain(GWT2Chain<Object> c){
            Object o = null;
     return o;
 }

}

class GWT2Chain<T>{



  

        }

Open in new window


will complian that GWT2Chain<Object>
cannot be applied to GWT2Chain<String>

This is because Generics in Java are  contravariant
which means that although String inherits form Object
GWT2Chain<String> does not inherit from GWT2Chain<Object>
which measn that you cannot use instance of
CWT2Chain<String> where instance of GWT2Chain<Object> is requirred





0
 
for_yanCommented:


Read here about Covariance/contravariance and check also links in there which have many useful
explanations about generics
http://checkmate-tech.blogspot.com/2011/07/java-generics-and-covariancecontravaria.html
0
 
for_yanCommented:
>This is because Generics in Java are  contravariant
actually this is not strict statement,
it is more strict to say that they are invariant
(see here : http://en.wikipedia.org/wiki/Covariance_and_contravariance_%28computer_science%29) so that compiler cannot say
if instance of GWT2Chain<Object> class and instance of GWT2Chain<String> class are in
any super class- subclass relationship - therefore it  shows a compilation error;
the wildcard need to be used in order to define generic class which would
allow to use subtypes as ther parameters in methods


0
 
bhomassAuthor Commented:
thank you. for_yan. you were totally correct. The author acknowledged the error and made the fix.
0
 
bhomassAuthor Commented:
some one who truly understands generics
0
 
for_yanCommented:
Thanks. You are always welcome.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now