Added:
/src/org/ductilej/tests/NameCollideTest.java
Modified:
/src/org/ductilej/detyper/Detype.java
=======================================
--- /dev/null
+++ /src/org/ductilej/tests/NameCollideTest.java Fri Aug 13 11:48:40 2010
@@ -0,0 +1,31 @@
+//
+// $Id$
+
+package org.ductilej.tests;
+
+import java.awt.event.*;
+import javax.swing.JButton;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ * Tests some annoying name collision behavior caused by TreeMaker.Type().
+ */
+public class NameCollideTest
+{
+ public static class MouseListener extends MouseAdapter {
+ public void mouseClicked (MouseEvent e) { /* yay! */ }
+ }
+
+ @Test public void testShadowedName () {
+ JButton button = new JButton();
+ // ensure that this gets transformed into:
+ // RT.invoke("addMouseListener", new Class<?>{
java.awt.event.MouseListener.class }, ...)
+ // rather than
+ // RT.invoke("addMouseListener", new Class<?>{
MouseListener.class }, ...)
+ // which will cause badness due to the shadowing MouseListener
above.
+ button.addMouseListener(new MouseListener());
+ assertTrue(1 == 1);
+ }
+}
=======================================
--- /src/org/ductilej/detyper/Detype.java Fri Aug 13 11:16:25 2010
+++ /src/org/ductilej/detyper/Detype.java Fri Aug 13 11:48:40 2010
@@ -1413,7 +1413,13 @@
protected JCExpression typeToTree (Type type, final int pos)
{
- JCExpression expr = _tmaker.at(pos).Type(type);
+ // note: we use _rootmaker here instead of _tmaker because _tmaker
will try to generate
+ // unqualified names for classes that are imported, but this opens
us up to name collisions
+ // when we are resolving the name of a type that does not normally
appear in the source
+ // file but we happen to need, and said type happens to be
shadowed by a local type name;
+ // _tmaker will assume the imported name can be referenced
unqualified, but that will
+ // resolve to the incorrect shadowing type
+ JCExpression expr = _rootmaker.at(pos).Type(type);
// there's a pesky bug in TreeMaker.Type that puts
java.lang.Object as the "inner" field of
// a JCWildcard for unbound declarations (i.e. Class<?>) which
later causes havoc to be