ahmaddarawshi / powermock

Automatically exported from code.google.com/p/powermock
0 stars 0 forks source link

WhiteboxImpl.getField(Class,String) doesn't check for null returned from Class.getSuperclass(), which will happen when thisType is an interface. #149

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Run the attached WhiteBoxGetFieldTest against the revision 916 or older of 
PowerMock trunk

WhiteboxImpl.getField(Class,String) doesn't check for null returned from 
Class.getSuperclass(), which 
will happen when thisType is an interface.

As the class hierarchy for the given Class is ascended, if the field is not 
found in the parent 
class, the interfaces are checked before the grandparent class. The problem is 
that the 
getSuperclass() method will always return null for the Class representing the 
interface. The current 
implementation of WhiteboxImpl blindly adds this null to the examine LinkedList.

What is the expected output? What do you see instead?

I expect a Field to be returned or a FieldNotFoundException to be thrown. 
Instead I get a 
NullPointerException:

java.lang.NullPointerException
    at org.powermock.reflect.internal.WhiteboxImpl.getField(WhiteboxImpl.java:203)
    at org.powermock.reflect.WhiteBoxGetFieldTest.testGetField(WhiteBoxGetFieldTest.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
    at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
    at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
    at 
org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMeth
odsRunner.java:66)
    at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
    at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
    at 
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReferen
ce.java:45)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

What version of the product are you using? On what operating system?

This issue was found in PowerMock 1.2.5 and has been verified to also be in 
trunk (revision 916). See 
attached WhiteBoxGetFieldTest.java for a demonstration. I have tested this with 
JDK 1.5.0_07 and 
1.6.0.013 running on Windows XP SP3.

Please provide any additional information below.

See the attached WhiteboxImpl_r916.patch file for an easy fix.

Original issue reported on code.google.com by benchate...@gmail.com on 4 Sep 2009 at 11:04

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks I will look into it asap (which means probably tomorrow).

Original comment by johan.ha...@gmail.com on 6 Sep 2009 at 3:42

GoogleCodeExporter commented 9 years ago
I've added your test to trunk but your test pass without your patch so I cannot
verify the issue. Could you see if you still get the error you got earlier when
running the test from trunk? The test is available here:
http://code.google.com/p/powermock/source/browse/trunk/reflect/src/test/java/org
/powermock/reflect/WhiteBoxGetFieldTest.java

Original comment by johan.ha...@gmail.com on 7 Sep 2009 at 6:14

GoogleCodeExporter commented 9 years ago
After I did a maven clean I could indeed verify the issue to ignore the previous
response. I've applied your patch and it works! Thanks a lot for the 
contribution.

/Johan

Original comment by johan.ha...@gmail.com on 7 Sep 2009 at 6:50