oh happy, happy day!
Please join me in celebrating the first Friday of the year 2005 with some free stuff!

We've got a really nice bag with the new Solaris logo on it. Hot, hot, hot. There's a Java Everywhere cap, a Duke mouse pad, a long-sleeved 2003 JavaOne Conference Urban Adventure t-shirt and a really nice (heavy) ink pen.
Puzzler is still on vacation.
Don't fret. We got a plan. But first you 've got to go down memory lane with me.
OK. Think back to Junior High School.... try to remember .... think of something that could happen that would totally make your day. Aside from that. What could happen at school that would make your heart leap?
Substitute teacher!
So that's what we've got going on today with Friday Free Stuff.
The Puzzler is on vacation.
We've got a Sub.
I'd like to sincerely think Bob "CrazyBob" Lee for being the Sub. He's supplied me with a brain teaser that Dr. Josh and Dr. Neal said was "too easy" for you guys.
(For the record, Bob Lee is a former Friday Free Stuff winner. He went on to score a highfalutin job at Google. That's right. And it happened in exactly that order; on my watch. Seriously.)
Since it's either Crazy Bob's brain teaser or formulas. We're going with Crazy Bob:
Create an object "o" such that
"o.getClass().getMethods()" returns a method that throws
"java.lang.IllegalAccessException" when invoked.
Want in on this game? Post your answer as a comment to this blog entry.
One last thing... you might consider thinking about what has been happening Friday Free Stuff prizes in recent weeks. Notice any trends?
bon weekend!
mary
p.s. Friday Free Stuff is not a contest. It's me giving away stuff that I personally own to people I choose. I pay for shipping with stamps that I buy at the post office. This week I'm going to ask Crazy Bob to help me choose the winner (as I don't really understand any of this code stuff). In the event we have multiple winners, I'll ask him to choose the one which is most clever, graceful, funny or otherwise entertaining. We only have one grand prize package. But, like I said, you just never know what's going to happen with the prizes around here, people. I'm in a really, really good mood.
import java.lang.reflect.Method; public class Ill { public void foo() throws IllegalAccessException { throw new IllegalAccessException("foo!"); } public static void main(String args[]) { try { Ill o = new Ill(); Method m[] = o.getClass().getMethods(); for (int i = 0; i < m.length; i++) { if (m[i].getName().equals("foo")) m[i].invoke(o, new Object[0]); } } catch (Exception e) { e.printStackTrace(); } } }Posted by Andrew Taylor on January 07, 2005 at 02:26 PM PST #
This is going to bug me until I know the answer. - Trevor
Posted by Trevor on January 07, 2005 at 02:44 PM PST #
Posted by Bob Lee on January 07, 2005 at 06:16 PM PST #
package reflectiontest; public interface IDumb { public String foo(); } package reflectiontest; class Bar implements IDumb { public Bar() {} public String foo() { return "boohoo!"; } } package reflectiontest; import java.lang.reflect.*; public class Foo extends Bar { public Foo() { } public static IDumb getInstance() { return new Bar(); } } import java.lang.reflect.*; import reflectiontest.*; public class Tester { public Tester() { } public static void main(String[] args) { IDumb foo = Foo.getInstance(); Object methodArgs[] = new Object[0] ; Method[] methods = foo.getClass().getMethods(); for (int i = 0; i < methods.length; i++) { try { if (!methods[i].getName().equals("foo")) continue; System.err.print( methods[i].getName() + ": "); System.err.println( methods[i].invoke(foo, methodArgs) ); } catch (IllegalAccessException iae) { iae.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } }Output:
foo: java.lang.IllegalAccessException: Class Tester can not access a member of class reflectiontest.Bar with modifiers "public" at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:57) at java.lang.reflect.Method.invoke(Method.java:317) at Tester.main(Tester.java:16)Posted by Michael Appleby on January 07, 2005 at 08:48 PM PST #
Posted by Michael Appleby on January 07, 2005 at 08:57 PM PST #
Posted by MooBob42 on January 07, 2005 at 08:57 PM PST #
<tt>class Foo { Foo() throws Exception { // bogus activity Class.forName("java.lang.Class").newInstance(); } public static void main(String args[]) { try { Foo foo = new Foo(); java.lang.reflect.Method m[] = foo.getClass().getMethods(); m[0].invoke(foo, null); // invoke constructor } catch(Exception e) { e.printStackTrace(); } } }</tt>Posted by John Catherino on January 07, 2005 at 09:23 PM PST #
package test; import java.lang.reflect.Method; import com.teramedica.everload.tool.node.AnotherPackage; public class IAETest { public static void main(String args[]) { try { AnotherPackage another = new AnotherPackage(); Method[] m = another.getClass().getMethods(); for (int i = 0; i < m.length; i++) { if (m[i].getName().equals("getDefaultClass")) { Object obj = m[i].invoke(another, new Object[0]); System.out.println("Method was invoked"); Method[] more = obj.getClass().getMethods(); for (int j = 0; j < more.length; j++) { if (more[j].getName().equals("tryAndAccess")) { Object o = more[j].invoke(obj, new Object[0]); System.out.println("Another method was invoked"); } } } } } catch (Exception e) { e.printStackTrace(); } } } package test.another; public class AnotherPackage { public AnotherPackage() { super(); } public AnotherPackageDefaultClass getDefaultClass() { return new AnotherPackageDefaultClass(); } } package test.another; class AnotherPackageDefaultClass { /** * */ AnotherPackageDefaultClass() { super(); } public void tryAndAccess() { System.out.println("Never get here from there!"); } }Posted by Damien Evans on January 07, 2005 at 09:51 PM PST #
<tt> class Foo { public void foo() throws Exception { // bogus activity... Class.forName("java.lang.Class").newInstance(); } public static void main(String args[]) { try { Foo foo = new Foo(); java.lang.reflect.Method m[] = foo.getClass().getMethods(); m[1].invoke(foo, null); // call foo method } catch(Exception x) { System.out.println(x.getCause()); } } } </tt>Posted by John Catherino on January 07, 2005 at 10:34 PM PST #
package a; public class A{ public static C getC(){ return new C(); } private static class C { public int getI(){ return 10; } } }--------------------------------------------------import java.lang.reflect.Method; import a.A; public class B{ public static void main(String[] args) throws Exception { Object c = A.getC(); Method[] methods = c.getClass().getMethods(); for(int i=0; i<methods.length; i++){ if(methods[i].getName().equals("getI")){ methods[i].invoke(c, new Object[]{}); } } } }Posted by Kishore on January 08, 2005 at 12:03 AM PST #
package a; public class A{ public static C getC(){ return new C(); } private static class C { public int getI(){ return 10; } } }--------------------------------------------------import java.lang.reflect.Method; import a.A; public class B{ public static void main(String[] args) throws Exception { Object c = A.getC(); Method[] methods = c.getClass().getMethods(); for(int i=0; i<methods.length; i++){ if(methods[i].getName().equals("getI")){ methods[i].invoke(c, new Object[]{}); } } } }Posted by Kishore on January 08, 2005 at 12:03 AM PST #
import java.lang.reflect.Method; public class O { class A { A() throws Exception { Class c = O.class; Class[] types = {}; Method m = c.getDeclaredMethod("me", types); m.invoke(O.this, null); } } private void me() { System.out.println("me"); } public void o() throws Exception { A a = new A(); } public static void main(String[] args) throws Exception { O o = new O(); Method m[] = o.getClass().getMethods(); m[1].invoke(o, null); } }The trick is the inner class and the dynamic invocation of private me method. Gtg need to go to the company party...Posted by nic laborera on January 08, 2005 at 01:29 PM PST #
import java.lang.reflect.*; import java.util.*; public class IllegalAccessTest { public static void main(String[] args) throws IllegalAccessException, InvocationTargetException { Map map = new HashMap(); map.put("foo", "bar"); Object o = map.entrySet().iterator().next(); Method[] methods = o.getClass().getMethods(); for (int i = 0, n = methods.length; i < n; i++) { Method m = methods[i]; if ("toString".equals(m.getName())) { m.invoke(o, null); break; } } } }The exception message is pretty funny. I mean, if you can't access a member with modifiers "public", it all seems pretty hopeless...Posted by Kris Schneider on January 08, 2005 at 08:33 PM PST #
Here's what I know from Java SE 5.0 docs:
IllegalAccessExceptiongetMethdods()
Here's what I did to create an Object that when invoked reflectively thru getMethods would throw an IllegalAccessException:
Scratched my head for a few seconds, pound my head on my desk a few times, and finally gave up... Then took a nap with my 11 month old and when I woke up, I watched Spider-man 2 with my twin 6 yr olds. Feeling better seeing Spider-man fail to make a pizza delivery under 30 minutes (I could have done that!?).
Posted by Will on January 09, 2005 at 08:11 PM PST #
Posted by James on January 10, 2005 at 02:02 AM PST #
Posted by James on January 11, 2005 at 02:02 AM PST #
Posted by Tindu Radhakrishnan on January 11, 2005 at 06:43 AM PST #
Posted by Kris Schneider on January 11, 2005 at 10:49 AM PST #
Posted by mary on January 14, 2005 at 03:46 PM PST #