Skip to content

Commit cb6c441

Browse files
author
jossonsmith
committed
Remove static class dependencies for those constant values. For example
SWT.DEFAULT does not require dependency of class SWT.
1 parent 075adcf commit cb6c441

3 files changed

Lines changed: 173 additions & 2 deletions

File tree

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/DependencyASTVisitor.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.eclipse.jdt.core.dom.Modifier;
4545
import org.eclipse.jdt.core.dom.Name;
4646
import org.eclipse.jdt.core.dom.PackageDeclaration;
47+
import org.eclipse.jdt.core.dom.QualifiedName;
4748
import org.eclipse.jdt.core.dom.SimpleName;
4849
import org.eclipse.jdt.core.dom.Statement;
4950
import org.eclipse.jdt.core.dom.TagElement;
@@ -695,11 +696,39 @@ protected void visitForRequires(AbstractTypeDeclaration node) {
695696
protected void visitForOptionals(AbstractTypeDeclaration node) {
696697

697698
}
699+
700+
protected boolean isSimpleQualified(QualifiedName node) {
701+
Name qualifier = node.getQualifier();
702+
if (qualifier instanceof SimpleName) {
703+
return true;
704+
} else if (qualifier instanceof QualifiedName) {
705+
return isSimpleQualified((QualifiedName) qualifier);
706+
}
707+
return false;
708+
}
698709

710+
/* (non-Javadoc)
711+
* @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.QualifiedName)
712+
*/
713+
public boolean visit(QualifiedName node) {
714+
Object constValue = node.resolveConstantExpressionValue();
715+
if (constValue != null && (constValue instanceof Number
716+
|| constValue instanceof Boolean)
717+
&& isSimpleQualified(node)) {
718+
//buffer.append(constValue);
719+
return false;
720+
}
721+
return super.visit(node);
722+
}
699723
/* (non-Javadoc)
700724
* @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.SimpleName)
701725
*/
702726
public boolean visit(SimpleName node) {
727+
Object constValue = node.resolveConstantExpressionValue();
728+
if (constValue != null && (constValue instanceof Number
729+
|| constValue instanceof Boolean)) {
730+
return false;
731+
}
703732
/*
704733
ITypeBinding typeBinding = node.resolveTypeBinding();
705734
if (typeBinding != null) {
@@ -916,8 +945,9 @@ public boolean visit(MethodDeclaration node) {
916945
public boolean visit(FieldAccess node) {
917946
Object constValue = node.resolveConstantExpressionValue();
918947
IVariableBinding resolveFieldBinding = node.resolveFieldBinding();
948+
Expression exp = node.getExpression();
919949
if (constValue == null && Modifier.isStatic(resolveFieldBinding.getModifiers())) {
920-
Expression expression = node.getExpression();
950+
Expression expression = exp;
921951
if (expression instanceof Name) {
922952
Name name = (Name) expression;
923953
ITypeBinding resolveTypeBinding = name.resolveTypeBinding();
@@ -939,7 +969,14 @@ public boolean visit(FieldAccess node) {
939969
optionals.add(qn);
940970
}
941971
}
972+
} else if (constValue != null && (constValue instanceof Number
973+
|| constValue instanceof Boolean)) {
974+
if ((exp instanceof QualifiedName)
975+
|| (exp instanceof QualifiedName && isSimpleQualified((QualifiedName) exp))) {
976+
return false;
977+
}
942978
}
979+
943980
return super.visit(node);
944981
}
945982

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*******************************************************************************
2+
* Java2Script Pacemaker (http://j2s.sourceforge.net)
3+
*
4+
* Copyright (c) 2006 ognize.com and others.
5+
* All rights reserved. This program and the accompanying materials
6+
* are made available under the terms of the Eclipse Public License v1.0
7+
* which accompanies this distribution, and is available at
8+
* http://www.eclipse.org/legal/epl-v10.html
9+
*
10+
* Contributors:
11+
* ognize.com - initial API and implementation
12+
*******************************************************************************/
13+
14+
package net.sf.j2s.core.astvisitors;
15+
16+
import java.util.List;
17+
import org.eclipse.jdt.core.dom.Block;
18+
import org.eclipse.jdt.core.dom.Expression;
19+
import org.eclipse.jdt.core.dom.ExpressionStatement;
20+
import org.eclipse.jdt.core.dom.IMethodBinding;
21+
import org.eclipse.jdt.core.dom.ITypeBinding;
22+
import org.eclipse.jdt.core.dom.IfStatement;
23+
import org.eclipse.jdt.core.dom.MethodDeclaration;
24+
import org.eclipse.jdt.core.dom.MethodInvocation;
25+
import org.eclipse.jdt.core.dom.Statement;
26+
27+
/**
28+
* @author josson smith
29+
*
30+
* 2006-8-5
31+
*/
32+
public class SWTDependencyASTVisitor extends DependencyASTVisitor {
33+
34+
protected String[] getFilterMethods() {
35+
return new String[] {
36+
"org.eclipse.swt.widgets.Widget", "checkSubclass",
37+
"org.eclipse.swt.widgets.Dialog", "checkSubclass",
38+
"org.eclipse.swt.widgets.Widget", "checkWidget",
39+
"org.eclipse.swt.widgets.Display", "checkDevice",
40+
"org.eclipse.swt.graphics.Device", "checkDevice"
41+
};
42+
}
43+
44+
/* (non-Javadoc)
45+
* @see net.sf.j2s.core.astvisitors.ASTScriptVisitor#endVisit(org.eclipse.jdt.core.dom.MethodDeclaration)
46+
*/
47+
public void endVisit(MethodDeclaration node) {
48+
IMethodBinding methodBinding = node.resolveBinding();
49+
String[] filterMethods = getFilterMethods();
50+
for (int i = 0; i < filterMethods.length; i += 2) {
51+
if (isMethodInvoking(methodBinding, filterMethods[i], filterMethods[i + 1])) {
52+
return ;
53+
}
54+
}
55+
super.endVisit(node);
56+
}
57+
58+
private boolean isMethodInvoking(IMethodBinding methodBinding, String className, String methodName) {
59+
if (methodName.equals(methodBinding.getName())) {
60+
IMethodBinding findMethodInHierarchy = Bindings.findMethodInHierarchy(methodBinding.getDeclaringClass(), methodName, null);
61+
IMethodBinding last = findMethodInHierarchy;
62+
int count = 0;
63+
while (findMethodInHierarchy != null && (count++) < 10) {
64+
last = findMethodInHierarchy;
65+
ITypeBinding superclass = last.getDeclaringClass().getSuperclass();
66+
if (superclass == null) {
67+
break;
68+
}
69+
findMethodInHierarchy =
70+
Bindings.findMethodInHierarchy(superclass, methodName, null);
71+
}
72+
if (last == null) {
73+
last = methodBinding;
74+
}
75+
if (className.equals(last.getDeclaringClass().getQualifiedName())) {
76+
return true;
77+
}
78+
}
79+
return false;
80+
}
81+
82+
private boolean isMethodInvoking(Expression exp, String className, String methodName) {
83+
if (exp instanceof MethodInvocation) {
84+
MethodInvocation method = (MethodInvocation) exp;
85+
IMethodBinding methodBinding = method.resolveMethodBinding();
86+
if (isMethodInvoking(methodBinding, className, methodName)) {
87+
return true;
88+
}
89+
/*
90+
IMethodBinding methodBinding = method.resolveMethodBinding();
91+
if (methodName.equals(methodBinding.getName()) &&className.equals(
92+
methodBinding.getDeclaringClass().getQualifiedName())) {
93+
return true;
94+
}
95+
*/
96+
}
97+
return false;
98+
}
99+
100+
public boolean visit(IfStatement node) {
101+
if (node.getElseStatement() == null) {
102+
Statement thenStatement = node.getThenStatement();
103+
if (thenStatement instanceof Block) {
104+
Block block = (Block) thenStatement;
105+
List statements = block.statements();
106+
if (statements.size() == 1) {
107+
thenStatement = (Statement) statements.get(0);
108+
}
109+
}
110+
if (thenStatement instanceof ExpressionStatement) {
111+
ExpressionStatement expStmt = (ExpressionStatement) thenStatement;
112+
Expression exp = expStmt.getExpression();
113+
if (isMethodInvoking(exp, "org.eclipse.swt.widgets.Widget", "error")) {
114+
return false;
115+
}
116+
if (isMethodInvoking(exp, "org.eclipse.swt.SWT", "error")) {
117+
return false;
118+
}
119+
if (isMethodInvoking(exp, "org.eclipse.swt.widgets.Display", "error")) {
120+
return false;
121+
}
122+
}
123+
}
124+
return super.visit(node);
125+
}
126+
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import net.sf.j2s.core.astvisitors.DependencyASTVisitor;
1919
import net.sf.j2s.core.astvisitors.NameConvertItem;
2020
import net.sf.j2s.core.astvisitors.NameConverterUtil;
21+
import net.sf.j2s.core.astvisitors.SWTDependencyASTVisitor;
2122
import net.sf.j2s.core.astvisitors.SWTScriptVisitor;
2223
import net.sf.j2s.core.builder.SourceFile;
2324
import net.sf.j2s.core.builder.SourceFileProxy;
@@ -131,7 +132,14 @@ public void compile(ICompilationUnit[] sourceUnits, IContainer binaryFolder) {
131132
astParser.setSource(createdUnit);
132133
root = (CompilationUnit) astParser.createAST(null);
133134

134-
DependencyASTVisitor dvisitor = new DependencyASTVisitor();
135+
DependencyASTVisitor dvisitor = null;
136+
if ("ASTScriptVisitor".equals(props.getProperty("j2s.compiler.visitor"))) {
137+
dvisitor = new DependencyASTVisitor();
138+
} else if ("SWTScriptVisitor".equals(props.getProperty("j2s.compiler.visitor"))) {
139+
dvisitor = new SWTDependencyASTVisitor();
140+
} else {
141+
dvisitor = new SWTDependencyASTVisitor();
142+
}
135143
boolean errorOccurs = false;
136144
try {
137145
root.accept(dvisitor);

0 commit comments

Comments
 (0)