1.5.0 Syntex Question

Discussion in 'Java' started by Aryeh M. Friedman, Mar 17, 2005.

  1. I am new to 1.5.0 and was wondering if anyone knows how to fix the following
    syntex/semantic errors. The following code produces the following error
    (javac Debug.java Assert.java -xlint):

    public class Assert {
     
    Aryeh M. Friedman, Mar 17, 2005
    #1
    1. Advertising

  2. "Aryeh M. Friedman" <> writes:

    > Assert.java:81: warning: [unchecked] unchecked cast
    > found : java.lang.Object
    > required: java.lang.Comparable<java.lang.Object>
    > if(((Comparable<Object>) val).compareTo(min)<=0)


    You can turn off the cast check warning using a variant on the -lint
    command line option to javac.

    > TestFailException.java:11: warning: [serial] serializable class
    > unittest.TestFailException has no definition of serialVersionUID
    > public class TestFailException extends RuntimeException
    > ^


    The same with that warning.

    > TestResult.java:49: unittest.TestErrorException is abstract; cannot be
    > instantiated
    > throw new TestErrorException();
    > ^


    You need to make a non-abstract subclass.

    > TestResultTest.java:57: incompatible types
    > found : unittest.TestErrorException
    > required: java.lang.Throwable
    > } catch(TestErrorException e) {
    > ^


    Your TestErrorException needs to extend Throwable or one of its
    subclasses. Unlike C++, you cannot throw any object you want.

    > Assert.assertRange(5,0,9);
    > ^


    When autoboxing the 5, the compiler uses Integer: That class does not
    implement Comparable<Object> but Comparable<Integer> which is not the
    same thing.

    > AssertTest.java:89:
    > assertRange(java.lang.Comparable<java.lang.Object>,java.lang.Object,java.lang.Object)
    > in unittest.Assert cannot be applied to (int,int,int)
    > Assert.assertRange(-1,0,9);
    > ^


    Same thing.

    > AssertTest.java:90:
    > assertRange(java.lang.Comparable<java.lang.Object>,java.lang.Object,java.lang.Object)
    > in unittest.Assert cannot be applied to (int,int,int)
    > Assert.assertRange(10,0,9);
    > ^


    And again.

    The rest are the same you got before.
     
    Tor Iver Wilhelmsen, Mar 17, 2005
    #2
    1. Advertising

  3. Aryeh M. Friedman

    Alan Moore Guest

    On Thu, 17 Mar 2005 01:57:23 -0500, "Aryeh M. Friedman"
    <> wrote:

    >public class Assert {
    >
    > .
    > .
    > .
    >
    > /**
    >
    > if val is between (inclusive) min and max pass else fail
    >
    > @param val the "real" value
    > @param min the minimal value allowed
    > @param max the maximum value allowed
    > */
    >
    > public static void assertRange(Object val,Object min,Object max)
    > {
    > try {
    > if(((Comparable<Object>) val).compareTo(min)<0)
    > _assert("assertRange: value to low minimum value is "
    > + min + " got " + val);
    >
    > // repeat test to see if more then max
    > } catch(ClassCastException e) {
    > TestResult.error(e);
    > }
    > }
    >}


    Change the method signature to:

    public static <T extends Comparable<T>> void
    assertRange(T val, T min, T max)


    Then you won't need to do any casts, and the "unchecked" warnings
    should go away.
     
    Alan Moore, Mar 17, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sean
    Replies:
    1
    Views:
    628
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. Nikhil Patel

    client script syntex error

    Nikhil Patel, Oct 7, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,166
    Nikhil Patel
    Oct 7, 2004
  3. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    697
    Iain Norman
    Feb 16, 2005
  4. froil
    Replies:
    4
    Views:
    141
    Jürgen Exner
    Mar 8, 2006
  5. Nagrik

    Strange syntex: beginner

    Nagrik, Mar 20, 2008, in forum: Perl Misc
    Replies:
    5
    Views:
    137
    Jürgen Exner
    Mar 21, 2008
Loading...

Share This Page