1.5.0 Syntex Question

A

Aryeh M. Friedman

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 {
 
T

Tor Iver Wilhelmsen

Aryeh M. Friedman said:
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
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.
 
A

Alan Moore

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top