Seema Kekre

Ranch Hand
+ Follow
since Dec 02, 2010
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
3
Received in last 30 days
0
Total given
2
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Seema Kekre

Was able to figure it out. For some reason, using mock for final classes in static causes a java.lang.ExceptionInInitializerError. When I changed this to a class method it still throws this Exception but my tests pass. Will post on Powermockito forum to understand this better.
12 years ago
I am new to Powermockito. I have a Junit4 setup with Ant1.7.1. I am mocking a final class using mockito 1.8.5


When I run this test using Eclipse, it runs fine. When I run the test using my ant build I get the following error. From this error it looks like it can find Powermockito and mockito in its classpath and there is just one mockito jar in the lib (claspath).

[junit] java.lang.ExceptionInInitializerError
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:169)
[junit] Caused by: java.lang.IllegalArgumentException: Cannot subclass final class class com.company.FinalDB
[junit] at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:447)
[junit] at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
[junit] at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217)
[junit] at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378)
[junit] at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318)
[junit] at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:93)
[junit] at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:50)
[junit] at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:100)
[junit] at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:58)
[junit] at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:138)
[junit] at com.hp.platform.store.archiver.SomeClassTest.getMockSomeClass(SomeClassTest.java:78)


My eclipse and Ant are pointing to the same junit. I dont think this is a Junit issue, but a mockito issue but I cannot figure out how to start figuring out the issue.


Why does it work with Eclipse and not with Ant? How can I figure out the order in which the Classloaders are loaded?

Thanks in advance.
Seema.
12 years ago
Hi All,

I am finally OCPJP 6 Certified. I got 81% and am a little disappointed with the score, I was aiming about 88-90% atleast. Nonetheless, studying for this exam cleared so many of my Java concepts and I feel so much richer now, that the score really does not matter. I would like to thank everyone who contributes to the forum, especially Bert Bates for writing the fabulous book and Devaka for his ExamLabs. Even though I may not have posted many questions, the existing threads solved a lot of my doubts.
On a side note, even though I gave the Java 6 exam, I did not face a single question about NavigableSet, NavigableMap or Console and there were a couple of questions which were not completely correct, one drag and drop question had a compilation error and another multiple choice question required 2 answers but had radio boxes, so I could select just one answer.

Seema.
13 years ago
So either you use ABS to define the type of the subclass if you dont want the subclass to use Generics, or use Generics wrt K's definition of the superclass. Right?
Now it all compiles fine.
I infer that ABS<K> is required because we have already defined what K is in the abstract class, then why do I need to redefine K for ABS2?
Code reference Generics Question


At Line 2, I tried
class SubABS<K> extends ABS<K>,
class SubABS<K extends Number> extends ABS<K extends Number>

and all variations of this, finally it compiles with class SubABS extends ABS, and the warning that raw types are used.

When I use Eclipse to implement the unimplemented methods, I get the method on 3. When I used the exact signature in 1, I get an error
Multiple markers at this line
- Name clash: The method useMe(ABS<? extends K>) of type SubABS has the same erasure as useMe(ABS) of type
ABS but does not override it
- The method useMe(ABS<? extends K>) of type SubABS must override a superclass method

How can I extend this abstract method correctly using Generics and why am I getting the above issues?
Ankur, I am having a difficult time understanding Generics as well. I will give my best shot at explaining this:

1 should never compile anyway because ABS<? super Number> means ABS<Object> is valid, which is untrue. So even if K is Number or its subtype, this method is not applicable.
Similarly Line 3 will never compile because K can be anything and it has to be K extends Number. Hence it is not allowed.

But, one conflict between 1,2 & 3 is, what happens when you say

Now method on Line 1 for SubClassABS can be interpreted as

Line 2 can be

Line 3 can be

However your a in a.useMe(a)(Line 4) matches ABS<? super Number>(Line 1) as well as ABS<? extends Number>(Line 2) as well as ABS<Number>(Line 3)
So all 3 methods are applicable. The compiler will not know which one to choose at runtime. Now if you comment out 1 and 3, 2 will compile without any issue. 2 compiles because there is a chance that <K> defined in the method is Number.

Experts please comment.
Seema.
Thanks Hauke. I think I get it now.
I thought I got what Hauke explained, and then I tried this code where I modified Line 1 as indicated:

No compile error. No runtime errors. Except for a warning saying "The type parameter E is hiding type E" at Line 1. Can someone explain this?
e1, e2, e3 are all different instances sharing the same StringBuffer object. When you use synchronized method it will lock "this" (Thread instance), which is separate for all 3 hence they dont interfere with each other. But when you lock StringBuffer, since it is the same for all 3 instances, if it is locked by one instance, the others have to wait till it is released.

To achieve what you want try implementing Runnable for Excercise and have its constructor take StringBuffer like before :
Runnable r= new Exercise(str);
Thread t1 = new Thread(r);
Thread t2= new Thread(r);
Thread t3 = new Thread(r);

Then try starting the threads.
Thanks Mike. I was aware of Lock, but unaware of the fair boolean in the constructor of ReentrantLock or what it did. Your second solution is great as well. I need to learn and practice multi-threading a lot more.
I have been learning about multi-threading in Java for some months and have practiced them with examples. From what I know, when using wait/notify, if n threads are waiting on an object, one random thread is notified when notify() is called. Recently, for an interview I was asked this question:

If there are 10 threads T1 to T10, and T2-T10 are dependent on a result from T1 and are waiting for that object. Now the first thread that enters wait for this object should be given higher priority than the next and so on. So eg: T4 calls wait() before T3, then when the notify is called T4 should be notified and should run before T3.

I think you can use a static volatile member which has some value say 10 and you can setPriority for every Thread just before going into wait() by using this static value and then reduce the value by 1. Then use notifyAll. Since it is guaranteed that the thread with the highest priority will always be selected to run before the thread with the lowest priority, this should work right?
Or is there another technique to solve this problem?

<Edit> Already see a problem with this since priority can be 1-10 or 1-5 depending on the OS, so this solution does not work and does not even scale to more threads. Another solution I thought of was to put the Threads in a queue in the order of them calling wait() but then how to select them.
That is precisely what I am saying...

s4+="bar" -> s4= s4+"bar" -> s4=s4.concat("bar")

As per the Java Specifications for String.concat:
If the length of the argument string is 0, then this String object is returned. Otherwise, a new String object is created, representing a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.

String s4="foo"; //Creates a string "foo" in the literal pool
s4+="bar";// Creates a new String "foobar" in the heap, s4 will now point to this new String.

Here is another good article Strings Literally

HTH.
Hi Malte,

I had the same doubt. Re:String Literals at compile time
Basically is something the compiler sees and checks if it is present in the String literal pool and equates to "foobar" but

In this case, at Line 2, the compiler does not know what the variable s4 holds and is equivalent to creating a new String with the value "foobar", which is different from the String literal value.
Hope this helps.
Thanks for your response Devaka. I gave the test again so that I could learn from my "new" mistakes . Thanks again for providing this freeware and helping so many people.