Skip to content

Commit 0b8e8e6

Browse files
author
jossonsmith
committed
Merge with /trunks for exposing Visitor as extension points
1 parent b4d8ecd commit 0b8e8e6

File tree

8 files changed

+374
-7
lines changed

8 files changed

+374
-7
lines changed

plugin.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
<extension-point name="Extended Compiler"
99
id="extendedCompiler"
1010
schema="schema/extendedCompiler.exsd"/>
11+
12+
<!-- =================================================================================== -->
13+
<!-- Extension Point: Extended AST Script Visitor -->
14+
<!-- =================================================================================== -->
15+
<extension-point name="Extended AST Script Visitor"
16+
id="extendedASTScriptVisitor"
17+
schema="schema/extendedASTScriptVisitor.exsd"/>
1118

1219
<extension
1320
id="net.sf.j2s.core.j2scompiler"
@@ -54,4 +61,14 @@
5461
</builder>
5562
</extension>
5663

64+
<extension
65+
point="net.sf.j2s.core.extendedASTScriptVisitor">
66+
<extendedASTScriptVisitor
67+
class="net.sf.j2s.core.compiler.ASTExtendedVisitor"
68+
id="BasicASTScriptVisitor"/>
69+
<extendedASTScriptVisitor
70+
class="net.sf.j2s.core.compiler.SWTExtendedVisitor"
71+
id="SWTASTScriptVisitor"/>
72+
</extension>
73+
5774
</plugin>

schema/extendedASTScriptVisitor.exsd

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<!-- Schema file written by PDE -->
3+
<schema targetNamespace="net.sf.j2s.core">
4+
<annotation>
5+
<appInfo>
6+
<meta.schema plugin="net.sf.j2s.core" id="j2sVisitor" name="Java2Script AST Script Visitor"/>
7+
</appInfo>
8+
<documentation>
9+
Extended AST Script Visitor for Java2Script Compiler
10+
</documentation>
11+
</annotation>
12+
13+
<element name="extension">
14+
<complexType>
15+
<sequence>
16+
<element ref="extendedASTScriptVisitor" minOccurs="0" maxOccurs="unbounded"/>
17+
</sequence>
18+
<attribute name="point" type="string" use="required">
19+
<annotation>
20+
<documentation>
21+
22+
</documentation>
23+
</annotation>
24+
</attribute>
25+
<attribute name="id" type="string">
26+
<annotation>
27+
<documentation>
28+
29+
</documentation>
30+
</annotation>
31+
</attribute>
32+
<attribute name="name" type="string">
33+
<annotation>
34+
<documentation>
35+
36+
</documentation>
37+
<appInfo>
38+
<meta.attribute translatable="true"/>
39+
</appInfo>
40+
</annotation>
41+
</attribute>
42+
</complexType>
43+
</element>
44+
45+
<element name="extendedASTScriptVisitor">
46+
<complexType>
47+
<attribute name="id" type="string" use="required">
48+
<annotation>
49+
<documentation>
50+
51+
</documentation>
52+
</annotation>
53+
</attribute>
54+
<attribute name="class" type="string" use="required">
55+
<annotation>
56+
<documentation>
57+
58+
</documentation>
59+
<appInfo>
60+
<meta.attribute kind="java"/>
61+
</appInfo>
62+
</annotation>
63+
</attribute>
64+
</complexType>
65+
</element>
66+
67+
<annotation>
68+
<appInfo>
69+
<meta.section type="since"/>
70+
</appInfo>
71+
<documentation>
72+
Java2Script 1.0.0 M4
73+
</documentation>
74+
</annotation>
75+
76+
<annotation>
77+
<appInfo>
78+
<meta.section type="examples"/>
79+
</appInfo>
80+
<documentation>
81+
82+
</documentation>
83+
</annotation>
84+
85+
<annotation>
86+
<appInfo>
87+
<meta.section type="apiInfo"/>
88+
</appInfo>
89+
<documentation>
90+
The class must inherit ASTScriptVisitor.
91+
</documentation>
92+
</annotation>
93+
94+
<annotation>
95+
<appInfo>
96+
<meta.section type="implementation"/>
97+
</appInfo>
98+
<documentation>
99+
When visiting AST tree, user can choose one of ASTScriptVisitors defined by this extension point.
100+
</documentation>
101+
</annotation>
102+
103+
<annotation>
104+
<appInfo>
105+
<meta.section type="copyright"/>
106+
</appInfo>
107+
<documentation>
108+
j2s.sourceforge.net
109+
</documentation>
110+
</annotation>
111+
112+
</schema>

src/net/sf/j2s/core/astvisitors/ASTKeywordParser.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,19 @@ public void endVisit(IfStatement node) {
10641064

10651065
public boolean visit(IfStatement node) {
10661066
buffer.append("if (");
1067-
node.getExpression().accept(this);
1067+
/**
1068+
* Boolean x = Boolean.FALSE;
1069+
*
1070+
* if( x ){
1071+
*
1072+
* }
1073+
* should converted to
1074+
* if(x.booleanValue()){
1075+
*
1076+
* }
1077+
*/
1078+
boxingNode(node.getExpression());
1079+
// node.getExpression().accept(this);
10681080
buffer.append(") ");
10691081
node.getThenStatement().accept(this);
10701082
if (node.getElseStatement() != null) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.compiler;
15+
16+
import net.sf.j2s.core.astvisitors.ASTScriptVisitor;
17+
import net.sf.j2s.core.astvisitors.DependencyASTVisitor;
18+
19+
/**
20+
* @author zhou renjian
21+
*
22+
* 2006-10-26
23+
*/
24+
public class ASTExtendedVisitor implements IExtendedVisitor {
25+
/* (non-Javadoc)
26+
* @see net.sf.j2s.core.compiler.IExtendedVisitor#getScriptVisitor()
27+
*/
28+
public ASTScriptVisitor getScriptVisitor() {
29+
return new ASTScriptVisitor();
30+
}
31+
/* (non-Javadoc)
32+
* @see net.sf.j2s.core.compiler.IExtendedVisitor#getDependencyVisitor()
33+
*/
34+
public DependencyASTVisitor getDependencyVisitor() {
35+
return new DependencyASTVisitor();
36+
}
37+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
package net.sf.j2s.core.compiler;
14+
15+
import java.util.HashMap;
16+
import java.util.Map;
17+
import org.eclipse.core.runtime.CoreException;
18+
import org.eclipse.core.runtime.IConfigurationElement;
19+
import org.eclipse.core.runtime.IExtension;
20+
import org.eclipse.core.runtime.IExtensionPoint;
21+
import org.eclipse.core.runtime.IExtensionRegistry;
22+
import org.eclipse.core.runtime.Platform;
23+
24+
/**
25+
* @author zhou renjian
26+
*
27+
* 2006-10-26
28+
*/
29+
public class ExtendedVisitors {
30+
31+
public static Map visitors = new HashMap();
32+
33+
private static boolean isExtensionPointsChecked = false;
34+
35+
private static void checkExtensionPoints() {
36+
if (isExtensionPointsChecked) {
37+
return;
38+
}
39+
isExtensionPointsChecked = true;
40+
IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry();
41+
IExtensionPoint extensionPoint = extensionRegistry
42+
.getExtensionPoint("net.sf.j2s.core.extendedASTScriptVisitor"); //$NON-NLS-1$
43+
if (extensionPoint == null) {
44+
return;
45+
}
46+
IExtension[] extensions = extensionPoint.getExtensions();
47+
// For each extension ...
48+
for (int i = 0; i < extensions.length; i++) {
49+
IExtension extension = extensions[i];
50+
IConfigurationElement[] elements = extension
51+
.getConfigurationElements();
52+
// For each member of the extension ...
53+
for (int j = 0; j < elements.length; j++) {
54+
IConfigurationElement element = elements[j];
55+
if ("extendedASTScriptVisitor".equals(element.getName())) { //$NON-NLS-1$
56+
String className = element.getAttribute("class"); //$NON-NLS-1$
57+
String id = element.getAttribute("id"); //$NON-NLS-1$
58+
if (className != null && className.trim().length() != 0
59+
&& id != null && id.trim().length() != 0) {
60+
try {
61+
Object callback = element
62+
.createExecutableExtension("class");
63+
if (callback instanceof IExtendedVisitor) {
64+
visitors.put(id.trim(), callback);
65+
}
66+
} catch (CoreException e) {
67+
e.printStackTrace();
68+
}
69+
}
70+
}
71+
}
72+
}
73+
}
74+
75+
public static void register(String visitorID, IExtendedVisitor visitor) {
76+
if (visitorID != null && visitorID.trim().length() != 0
77+
&& visitor != null) {
78+
visitors.put(visitorID, visitor);
79+
}
80+
}
81+
82+
public static IExtendedVisitor deregister(String visitorID) {
83+
if (visitorID != null && visitorID.trim().length() != 0) {
84+
return (IExtendedVisitor) visitors.remove(visitorID);
85+
}
86+
return null;
87+
}
88+
89+
public static IExtendedVisitor[] getExistedVisitors() {
90+
checkExtensionPoints();
91+
return (IExtendedVisitor[]) visitors.values().toArray(new IExtendedVisitor[0]);
92+
}
93+
94+
public static IExtendedVisitor getExistedVisitor(String id) {
95+
checkExtensionPoints();
96+
return (IExtendedVisitor) visitors.get(id);
97+
}
98+
99+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.compiler;
15+
16+
import net.sf.j2s.core.astvisitors.ASTScriptVisitor;
17+
import net.sf.j2s.core.astvisitors.DependencyASTVisitor;
18+
19+
/**
20+
* @author zhou renjian
21+
*
22+
* 2006-10-26
23+
*/
24+
public interface IExtendedVisitor {
25+
/**
26+
* Return visitor that generate scripts.
27+
* @return
28+
*/
29+
public ASTScriptVisitor getScriptVisitor();
30+
31+
/**
32+
* Return visitor for class dependencies.
33+
* @return
34+
*/
35+
public DependencyASTVisitor getDependencyVisitor();
36+
}

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,22 @@ public void compile(ICompilationUnit[] sourceUnits, IContainer binaryFolder) {
132132
root = (CompilationUnit) astParser.createAST(null);
133133

134134
DependencyASTVisitor dvisitor = null;
135-
if ("ASTScriptVisitor".equals(props.getProperty("j2s.compiler.visitor"))) {
135+
String visitorID = props.getProperty("j2s.compiler.visitor");
136+
IExtendedVisitor extVisitor = null;
137+
if ("ASTScriptVisitor".equals(visitorID)) {
136138
dvisitor = new DependencyASTVisitor();
137-
} else if ("SWTScriptVisitor".equals(props.getProperty("j2s.compiler.visitor"))) {
139+
} else if ("SWTScriptVisitor".equals(visitorID)) {
138140
dvisitor = new SWTDependencyASTVisitor();
139141
} else {
140-
dvisitor = new SWTDependencyASTVisitor();
142+
if (visitorID != null && visitorID.length() != 0) {
143+
extVisitor = ExtendedVisitors.getExistedVisitor(visitorID);
144+
if (extVisitor != null) {
145+
dvisitor = extVisitor.getDependencyVisitor();
146+
}
147+
}
148+
if (dvisitor == null) {
149+
dvisitor = new SWTDependencyASTVisitor();
150+
}
141151
}
142152
boolean errorOccurs = false;
143153
try {
@@ -167,12 +177,17 @@ public void compile(ICompilationUnit[] sourceUnits, IContainer binaryFolder) {
167177
}
168178

169179
ASTScriptVisitor visitor = null;
170-
if ("ASTScriptVisitor".equals(props.getProperty("j2s.compiler.visitor"))) {
180+
if ("ASTScriptVisitor".equals(visitorID)) {
171181
visitor = new ASTScriptVisitor();
172-
} else if ("SWTScriptVisitor".equals(props.getProperty("j2s.compiler.visitor"))) {
182+
} else if ("SWTScriptVisitor".equals(visitorID)) {
173183
visitor = new SWTScriptVisitor();
174184
} else {
175-
visitor = new SWTScriptVisitor();
185+
if (extVisitor != null) {
186+
visitor = extVisitor.getScriptVisitor();
187+
}
188+
if (visitor == null) {
189+
visitor = new SWTScriptVisitor();
190+
}
176191
}
177192
boolean isDebugging = "debug".equals(props.getProperty("j2s.compiler.mode"));
178193
visitor.setDebugging(isDebugging);

0 commit comments

Comments
 (0)