Skip to content

Commit 467221a

Browse files
hansonrhansonr
authored andcommitted
addresses issue of overloaded fields in a2s subclasses
The problem is that AWT class subclassing may introduce fields that have the same name as JComponent subclasses, and the transpiler will not recognize that. One solution would be to privatize all JComponent subclass fields as $p1 type fields or even just with var.
1 parent 5c7ef17 commit 467221a

File tree

11 files changed

+95
-72
lines changed

11 files changed

+95
-72
lines changed
0 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20190324055304
1+
20190325055157
0 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20190324055304
1+
20190325055157

sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptCompiler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class Java2ScriptCompiler {
6767

6868
private String siteFolder;
6969

70+
private boolean testing;
71+
7072
private List<String> lstExcludedPaths;
7173

7274
private boolean isCleanBuild;
@@ -187,6 +189,8 @@ boolean initializeProject(IJavaProject project, boolean isCompilationParticipant
187189
lstExcludedPaths = null;
188190
}
189191

192+
testing = "true".equals(getProperty("j2s.testing"));
193+
190194
String prop = getProperty("j2s.compiler.nonqualified.packages");
191195
// older version of the name
192196
String nonqualifiedPackages = getProperty("j2s.compiler.nonqualified.classes");
@@ -249,7 +253,7 @@ boolean compileToJavaScript(IFile javaSource) {
249253
CompilationUnit root = (CompilationUnit) astParser.createAST(null);
250254
// If the Java2ScriptVisitor is ever extended, it is important to set the project.
251255
// Java2ScriptVisitor#addClassOrInterface uses getClass().newInstance().setproject(project).
252-
Java2ScriptVisitor visitor = new Java2ScriptVisitor().setProject(project);
256+
Java2ScriptVisitor visitor = new Java2ScriptVisitor().setProject(project, testing);
253257

254258
try {
255259

sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptVisitor.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,21 @@ public class Java2ScriptVisitor extends ASTVisitor {
198198

199199
private IJavaProject global_project;
200200

201+
/**
202+
* multipurpose flag for testing development ideas.
203+
*
204+
*/
205+
private boolean global_testing;
206+
// make private fields properties of p1$ ?
207+
// the problem only shows up with a2s subclasses of Swing components
208+
// because they could declare the same private variable and not get
209+
// caught by the compiler, since they do not subclass that class.
210+
//
211+
// possible issues:
212+
// 1) We may have changed some awt, sun, and javax fields from private to public
213+
// 2) Is there an issue with inner classes referencing outer-class private fields?
214+
215+
201216
private String package_name;
202217
private int package_blockLevel = 0;
203218
private int package_currentBlockForVisit = -1;
@@ -305,6 +320,7 @@ private Java2ScriptVisitor setInnerGlobals(Java2ScriptVisitor parent, ASTNode no
305320
*/
306321
private boolean haveDefaultConstructor;
307322

323+
308324
private static IType appletType;
309325

310326
public Java2ScriptVisitor() {
@@ -318,7 +334,8 @@ public Java2ScriptVisitor() {
318334
// }
319335
}
320336

321-
public Java2ScriptVisitor setProject(IJavaProject project) {
337+
public Java2ScriptVisitor setProject(IJavaProject project, boolean testing) {
338+
this.global_testing = testing;
322339
this.global_project = project;
323340
return this;
324341
}
@@ -1626,7 +1643,7 @@ private void addClassOrInterface(ASTNode node, ITypeBinding binding, List<?> bod
16261643

16271644
Java2ScriptVisitor tempVisitor = null;
16281645
try {
1629-
tempVisitor = getClass().newInstance().setProject(global_project).setInnerGlobals(this, node);
1646+
tempVisitor = getClass().newInstance().setProject(global_project, global_testing).setInnerGlobals(this, node);
16301647
} catch (@SuppressWarnings("unused") Exception e) {
16311648
// impossible
16321649
}
334 Bytes
Binary file not shown.

sources/net.sf.j2s.java.core/src/java/awt/JSComponent.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public boolean selfOrChildIsPainted() {
180180
}
181181

182182

183-
private Insets tempInsets;
183+
private Insets _tempInsets;
184184
public JSGraphics2D _gtemp; // indicates that we are painting, so that g.setBackground() should also be set
185185

186186
public JSComponent() {
@@ -203,11 +203,11 @@ public Graphics getGraphics() {
203203
if (frameViewer != null) {
204204
g = frameViewer.getGraphics().create();
205205
if (isContentPane) {
206-
if (tempInsets == null)
207-
tempInsets = new Insets(0,0,0,0);
208-
((JComponent) this).getRootPane().getInsets(tempInsets);
209-
if (tempInsets.left != 0 || tempInsets.top != 0)
210-
g.translate(tempInsets.left, tempInsets.top);
206+
if (_tempInsets == null)
207+
_tempInsets = new Insets(0,0,0,0);
208+
((JComponent) this).getRootPane().getInsets(_tempInsets);
209+
if (_tempInsets.left != 0 || _tempInsets.top != 0)
210+
g.translate(_tempInsets.left, _tempInsets.top);
211211
// when user has inset the applet -- should clip?
212212
}
213213
return g;

sources/net.sf.j2s.java.core/src/javax/swing/JComponent.java

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,13 @@ public abstract class JComponent extends Container {
201201
/*
202202
* The following fields support set methods for the corresponding
203203
* java.awt.Component properties.
204+
*
205+
* SwingJS
204206
*/
205-
private boolean isAlignmentXSet;
206-
private float alignmentX;
207-
private boolean isAlignmentYSet;
208-
private float alignmentY;
207+
private boolean _isAlignmentXSet;
208+
private float _alignmentX;
209+
private boolean _isAlignmentYSet;
210+
private float _alignmentY;
209211

210212
/**
211213
* Backing store for JComponent properties and listeners
@@ -220,9 +222,9 @@ public abstract class JComponent extends Container {
220222
/**
221223
* Whether or not autoscroll has been enabled.
222224
*/
223-
private boolean autoscrolls;
224-
private Border border;
225-
private int flags;
225+
private boolean _autoscrolls;
226+
private Border _border;
227+
private int _flags;
226228

227229
/* Input verifier for this component */
228230
// private InputVerifier inputVerifier = null;
@@ -285,7 +287,7 @@ public abstract class JComponent extends Container {
285287
/**
286288
* <code>JPopupMenu</code> assigned to this component and all of its childrens
287289
*/
288-
private JPopupMenu popupMenu;
290+
private JPopupMenu _popupMenu;
289291

290292
/** Private flags **/
291293
private static final int IS_DOUBLE_BUFFERED = 0;
@@ -323,7 +325,7 @@ public abstract class JComponent extends Container {
323325
/**
324326
* Temporary rectangles.
325327
*/
326-
private static Lst<Rectangle> tempRectangles = new Lst<Rectangle>();
328+
private static Lst<Rectangle> _tempRect = new Lst<Rectangle>();
327329

328330
/** Used for <code>WHEN_FOCUSED</code> bindings. */
329331
private InputMap focusInputMap;
@@ -420,11 +422,11 @@ static Set<KeyStroke> getManagingFocusBackwardTraversalKeys() {
420422
}
421423

422424
private static Rectangle fetchRectangle() {
423-
synchronized (tempRectangles) {
425+
synchronized (_tempRect) {
424426
Rectangle rect;
425-
int size = tempRectangles.size();
427+
int size = _tempRect.size();
426428
if (size > 0) {
427-
rect = (Rectangle) tempRectangles.removeItemAt(size - 1);
429+
rect = (Rectangle) _tempRect.removeItemAt(size - 1);
428430
} else {
429431
rect = new Rectangle(0, 0, 0, 0);
430432
}
@@ -433,8 +435,8 @@ private static Rectangle fetchRectangle() {
433435
}
434436

435437
private static void recycleRectangle(Rectangle rect) {
436-
synchronized (tempRectangles) {
437-
tempRectangles.addLast(rect);
438+
synchronized (_tempRect) {
439+
_tempRect.addLast(rect);
438440
}
439441
}
440442

@@ -496,8 +498,8 @@ public void setComponentPopupMenu(JPopupMenu popup) {
496498
if (popup != null) {
497499
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
498500
}
499-
JPopupMenu oldPopup = this.popupMenu;
500-
this.popupMenu = popup;
501+
JPopupMenu oldPopup = this._popupMenu;
502+
this._popupMenu = popup;
501503
firePropertyChange("componentPopupMenu", oldPopup, popup);
502504
}
503505

@@ -516,10 +518,10 @@ public void setComponentPopupMenu(JPopupMenu popup) {
516518
public JPopupMenu getComponentPopupMenu() {
517519

518520
if (!getInheritsPopupMenu()) {
519-
return popupMenu;
521+
return _popupMenu;
520522
}
521523

522-
if (popupMenu == null) {
524+
if (_popupMenu == null) {
523525
// Search parents for its popup
524526
Container parent = getParent();
525527
while (parent != null) {
@@ -535,7 +537,7 @@ public JPopupMenu getComponentPopupMenu() {
535537
return null;
536538
}
537539

538-
return popupMenu;
540+
return _popupMenu;
539541
}
540542

541543
/**
@@ -1588,9 +1590,9 @@ public boolean contains(int x, int y) {
15881590
* description: The component's border.
15891591
*/
15901592
public void setBorder(Border border) {
1591-
Border oldBorder = this.border;
1593+
Border oldBorder = this._border;
15921594

1593-
this.border = border;
1595+
this._border = border;
15941596
firePropertyChange("border", oldBorder, border);
15951597
if (border != oldBorder) {
15961598
if (border == null
@@ -1611,7 +1613,7 @@ public void setBorder(Border border) {
16111613
* @see #setBorder
16121614
*/
16131615
public Border getBorder() {
1614-
return border;
1616+
return _border;
16151617
}
16161618

16171619
/**
@@ -1623,8 +1625,8 @@ public Border getBorder() {
16231625
*/
16241626
@Override
16251627
public Insets getInsets() {
1626-
if (border != null) {
1627-
return border.getBorderInsets(this);
1628+
if (_border != null) {
1629+
return _border.getBorderInsets(this);
16281630
}
16291631
return super.getInsets();
16301632
}
@@ -1651,17 +1653,17 @@ public Insets getInsets(Insets insets) {
16511653
// because AWT components do not have this method
16521654
in = getInsets();
16531655
} else {
1654-
if (border == null) {
1656+
if (_border == null) {
16551657
// super.getInsets() always returns an Insets object with
16561658
// all of its value zeroed. No need for a new object here.
16571659
insets.left = insets.top = insets.right = insets.bottom = 0;
16581660
} else {
1659-
if (border instanceof AbstractBorder) {
1660-
in = ((AbstractBorder) border).getBorderInsets(this, insets);
1661+
if (_border instanceof AbstractBorder) {
1662+
in = ((AbstractBorder) _border).getBorderInsets(this, insets);
16611663
}
16621664
// Can't reuse border insets because the Border interface
16631665
// can't be enhanced.
1664-
in = border.getBorderInsets(this);
1666+
in = _border.getBorderInsets(this);
16651667
}
16661668
}
16671669
if (in != null) {
@@ -1683,8 +1685,8 @@ public Insets getInsets(Insets insets) {
16831685
*/
16841686
@Override
16851687
public float getAlignmentY() {
1686-
if (isAlignmentYSet) {
1687-
return alignmentY;
1688+
if (_isAlignmentYSet) {
1689+
return _alignmentY;
16881690
}
16891691
return super.getAlignmentY();
16901692
}
@@ -1698,9 +1700,9 @@ public float getAlignmentY() {
16981700
* @beaninfo description: The preferred vertical alignment of the component.
16991701
*/
17001702
public void setAlignmentY(float alignmentY) {
1701-
this.alignmentY = alignmentY > 1.0f ? 1.0f : alignmentY < 0.0f ? 0.0f
1703+
this._alignmentY = alignmentY > 1.0f ? 1.0f : alignmentY < 0.0f ? 0.0f
17021704
: alignmentY;
1703-
isAlignmentYSet = true;
1705+
_isAlignmentYSet = true;
17041706
}
17051707

17061708
/**
@@ -1713,8 +1715,8 @@ public void setAlignmentY(float alignmentY) {
17131715
*/
17141716
@Override
17151717
public float getAlignmentX() {
1716-
if (isAlignmentXSet) {
1717-
return alignmentX;
1718+
if (_isAlignmentXSet) {
1719+
return _alignmentX;
17181720
}
17191721
return super.getAlignmentX();
17201722
}
@@ -1728,9 +1730,9 @@ public float getAlignmentX() {
17281730
* @beaninfo description: The preferred horizontal alignment of the component.
17291731
*/
17301732
public void setAlignmentX(float alignmentX) {
1731-
this.alignmentX = alignmentX > 1.0f ? 1.0f : alignmentX < 0.0f ? 0.0f
1733+
this._alignmentX = alignmentX > 1.0f ? 1.0f : alignmentX < 0.0f ? 0.0f
17321734
: alignmentX;
1733-
isAlignmentXSet = true;
1735+
_isAlignmentXSet = true;
17341736
}
17351737

17361738
/**
@@ -2958,8 +2960,8 @@ public void scrollRectToVisible(Rectangle aRect) {
29582960
*/
29592961
public void setAutoscrolls(boolean autoscrolls) {
29602962
setFlag(AUTOSCROLLS_SET, true);
2961-
if (this.autoscrolls != autoscrolls) {
2962-
this.autoscrolls = autoscrolls;
2963+
if (this._autoscrolls != autoscrolls) {
2964+
this._autoscrolls = autoscrolls;
29632965
if (autoscrolls) {
29642966
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
29652967
enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
@@ -2977,7 +2979,7 @@ public void setAutoscrolls(boolean autoscrolls) {
29772979
* @see #setAutoscrolls
29782980
*/
29792981
public boolean getAutoscrolls() {
2980-
return autoscrolls;
2982+
return _autoscrolls;
29812983
}
29822984

29832985
/**
@@ -2993,7 +2995,7 @@ public boolean getAutoscrolls() {
29932995
*/
29942996
@Override
29952997
protected void processMouseEvent(MouseEvent e) {
2996-
if (autoscrolls && e.getID() == MouseEvent.MOUSE_RELEASED) {
2998+
if (_autoscrolls && e.getID() == MouseEvent.MOUSE_RELEASED) {
29972999
Autoscroller.stop(this);
29983000
}
29993001
super.processMouseEvent(e);
@@ -3009,7 +3011,7 @@ protected void processMouseEvent(MouseEvent e) {
30093011
@Override
30103012
protected void processMouseMotionEvent(MouseEvent e) {
30113013
boolean dispatch = true;
3012-
if (autoscrolls && e.getID() == MouseEvent.MOUSE_DRAGGED) {
3014+
if (_autoscrolls && e.getID() == MouseEvent.MOUSE_DRAGGED) {
30133015
// We don't want to do the drags when the mouse moves if we're
30143016
// autoscrolling. It makes it feel spastic.
30153017
dispatch = !Autoscroller.isRunning(this);
@@ -4164,7 +4166,7 @@ public void removeNotify() {
41644166
RepaintManager.currentManager(this).resetDoubleBuffer();
41654167
setCreatedDoubleBuffer(false);
41664168
}
4167-
if (autoscrolls) {
4169+
if (_autoscrolls) {
41684170
Autoscroller.stop(this);
41694171
}
41704172
}
@@ -4680,15 +4682,15 @@ boolean checkIfChildObscuredBySibling() {
46804682

46814683
private void setFlag(int aFlag, boolean aValue) {
46824684
if (aValue) {
4683-
flags |= (1 << aFlag);
4685+
_flags |= (1 << aFlag);
46844686
} else {
4685-
flags &= ~(1 << aFlag);
4687+
_flags &= ~(1 << aFlag);
46864688
}
46874689
}
46884690

46894691
private boolean getFlag(int aFlag) {
46904692
int mask = (1 << aFlag);
4691-
return ((flags & mask) == mask);
4693+
return ((_flags & mask) == mask);
46924694
}
46934695

46944696
// // These functions must be static so that they can be called from
@@ -4754,11 +4756,11 @@ protected String paramString() {
47544756
.toString() : "");
47554757
String maximumSizeString = (isMaximumSizeSet() ? getMaximumSize()
47564758
.toString() : "");
4757-
String borderString = (border == null ? "" : (border == this ? "this"
4758-
: border.toString()));
4759+
String borderString = (_border == null ? "" : (_border == this ? "this"
4760+
: _border.toString()));
47594761

4760-
return super.paramString() + ",alignmentX=" + alignmentX + ",alignmentY="
4761-
+ alignmentY + ",border=" + borderString + ",flags=" + flags
4762+
return super.paramString() + ",alignmentX=" + _alignmentX + ",alignmentY="
4763+
+ _alignmentY + ",border=" + borderString + ",flags=" + _flags
47624764
+ // should beef this up a bit
47634765
",maximumSize=" + maximumSizeString + ",minimumSize="
47644766
+ minimumSizeString + ",preferredSize=" + preferredSizeString;

0 commit comments

Comments
 (0)