[ductilej] push by samskivert - Work around TreeMaker.QualIdent's noble intention to generate unqualif... on 2010-08-13 18:48 GMT

1 view
Skip to first unread message

duct...@googlecode.com

unread,
Aug 13, 2010, 2:49:42 PM8/13/10
to ductil...@googlegroups.com
Revision: 82bb1af997
Author: Michael Bayne <m...@samskivert.com>
Date: Fri Aug 13 11:48:40 2010
Log: Work around TreeMaker.QualIdent's noble intention to generate
unqualified type
names in situations where they are in scope. The problem is that
TreeMaker.QualIdent only checks the necessary but not sufficient condition
that
the type name has been imported. It fails to check that the type name has
not
*also* been shadowed by a local type declaration. That would of course be
much
harder, so I can see why they drop the ball. However, they should perhaps
have
avoided picking up the ball in the first place.
http://code.google.com/p/ductilej/source/detail?r=82bb1af997

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

Reply all
Reply to author
Forward
0 new messages