Author: alexsmirnov
Date: 2010-06-24 01:18:38 -0400 (Thu, 24 Jun 2010)
New Revision: 17664
Modified:
root/cdk/branches/RF8755/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/RendererClassVisitor.java
root/cdk/branches/RF8755/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/builder/model/MethodBodyStatementsContainer.java
Log:
refactor template generator
Modified:
root/cdk/branches/RF8755/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/RendererClassVisitor.java
===================================================================
---
root/cdk/branches/RF8755/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/RendererClassVisitor.java 2010-06-23
17:11:45 UTC (rev 17663)
+++
root/cdk/branches/RF8755/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/RendererClassVisitor.java 2010-06-24
05:18:38 UTC (rev 17664)
@@ -23,17 +23,16 @@
package org.richfaces.cdk.templatecompiler;
-import static org.richfaces.cdk.templatecompiler.QNameComparator.*;
-import static org.richfaces.cdk.util.JavaUtils.*;
+import static org.richfaces.cdk.templatecompiler.QNameComparator.QNAME_COMPARATOR;
+import static org.richfaces.cdk.util.JavaUtils.getEscapedString;
+import static org.richfaces.cdk.util.JavaUtils.getEscapedStringsArray;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.EnumSet;
-import java.util.HashMap;
import java.util.HashSet;
-import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
@@ -57,6 +56,8 @@
import org.richfaces.cdk.templatecompiler.builder.model.JavaMethod;
import org.richfaces.cdk.templatecompiler.builder.model.JavaModifier;
import org.richfaces.cdk.templatecompiler.builder.model.MethodBody;
+import org.richfaces.cdk.templatecompiler.builder.model.MethodBodyStatement;
+import org.richfaces.cdk.templatecompiler.builder.model.MethodBodyStatementImpl;
import org.richfaces.cdk.templatecompiler.builder.model.MethodBodyStatementsContainer;
import org.richfaces.cdk.templatecompiler.el.ELParserUtils;
import org.richfaces.cdk.templatecompiler.el.ELVisitor;
@@ -82,12 +83,11 @@
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Lists;
/**
* <p class="changed_added_4_0">
* </p>
- *
+ *
* @author asmirnov(a)exadel.com
*/
public class RendererClassVisitor implements TemplateVisitor {
@@ -130,8 +130,8 @@
private static final Set<String> DEFAULT_NAMESPACES =
ImmutableSet.of(Template.XHTML_EL_NAMESPACE,
Template.XHTML_NAMESPACE);
- private static final EnumMap<HelperMethod, HelperMethodBodyStatement>
HELPER_METHOD_BODIES =
- new EnumMap<HelperMethod, HelperMethodBodyStatement>(HelperMethod.class);
+ private static final EnumMap<HelperMethod, HelperMethodBodyStatement>
HELPER_METHOD_BODIES = new EnumMap<HelperMethod, HelperMethodBodyStatement>(
+ HelperMethod.class);
static {
HELPER_METHOD_BODIES.put(HelperMethod.EMPTINESS_CHECK, new
EmptinessCheckingMethodBodyStatement());
@@ -144,13 +144,10 @@
private MethodBodyStatementsContainer currentStatement;
- private final Schema attributesSchema ;
+ private final Schema attributesSchema;
private JavaClass generatedClass;
private CompositeInterface compositeInterface;
- private final LinkedList<MethodBodyStatementsContainer> statements =
Lists.newLinkedList();
-
- private Map<String, Type> localsTypesMap;
private final ClassLoader classLoader;
private Set<HelperMethod> addedHelperMethods =
EnumSet.noneOf(HelperMethod.class);
@@ -158,11 +155,11 @@
private Type lastCompiledExpressionType;
private int passThroughCounter;
private final Collection<PropertyBase> attributes;
-
+
private final VisitorFactoryImpl visitorFactoryImpl;
public RendererClassVisitor(CompositeInterface compositeInterface,
Collection<PropertyBase> attributes,
- VisitorFactoryImpl factory) {
+ VisitorFactoryImpl factory) {
this.compositeInterface = compositeInterface;
this.attributes = attributes;
this.visitorFactoryImpl = factory;
@@ -202,8 +199,8 @@
arguments[i] = new Argument(argumentNames[i], argumentTypes[i]);
}
- JavaMethod helperJavaMethod = new JavaMethod(helperMethod.getName(),
- helperMethod.getReturnType(), arguments);
+ JavaMethod helperJavaMethod = new JavaMethod(helperMethod.getName(),
helperMethod.getReturnType(),
+ arguments);
helperJavaMethod.addModifier(JavaModifier.PRIVATE);
helperJavaMethod.addModifier(JavaModifier.STATIC);
@@ -334,8 +331,8 @@
generatedClass.addImport(Collections.class);
// TODO - get rid of FQNs for classes via imports
- passThroughField.setGenericArguments(new JavaClass[]{new
JavaClass(String.class),
- new JavaClass("org.richfaces.renderkit.ComponentAttribute")});
+ passThroughField.setGenericArguments(new JavaClass[] { new
JavaClass(String.class),
+ new JavaClass("org.richfaces.renderkit.ComponentAttribute") });
StringBuilder fieldValue = new
StringBuilder("Collections.unmodifiableMap(ComponentAttribute.createMap(");
boolean isFirstArgument = true;
@@ -367,19 +364,18 @@
private void createMethodContext() {
this.currentStatement = new MethodBody();
- this.localsTypesMap = new HashMap<String, Type>();
- localsTypesMap.put(FACES_CONTEXT_VARIABLE,
TypesFactory.getType(FacesContext.class));
- localsTypesMap.put(RESPONSE_WRITER_VARIABLE,
TypesFactory.getType(ResponseWriter.class));
- localsTypesMap.put(CLIENT_ID_VARIABLE, TypesFactory.getType(String.class));
+ currentStatement.setVariable(FACES_CONTEXT_VARIABLE,
TypesFactory.getType(FacesContext.class));
+ currentStatement.setVariable(RESPONSE_WRITER_VARIABLE,
TypesFactory.getType(ResponseWriter.class));
+ currentStatement.setVariable(CLIENT_ID_VARIABLE,
TypesFactory.getType(String.class));
// TODO: try load component class
- localsTypesMap.put(COMPONENT_VARIABLE, TypesFactory.getType(UIComponent.class));
+ currentStatement.setVariable(COMPONENT_VARIABLE,
TypesFactory.getType(UIComponent.class));
Type generatedClassType = createTypeOfKnownClass(generatedClass,
Renderer.class);
- localsTypesMap.put(THIS_VARIABLE, generatedClassType);
+ currentStatement.setVariable(THIS_VARIABLE, generatedClassType);
Type generatedClassSuperType =
createTypeOfKnownClass(generatedClass.getSuperClass(), Renderer.class);
- localsTypesMap.put(SUPER_VARIABLE, generatedClassSuperType);
+ currentStatement.setVariable(SUPER_VARIABLE, generatedClassSuperType);
}
private void flushToEncodeMethod(String encodeMethodName) {
@@ -397,15 +393,10 @@
methodBody.addStatement(0, new EncodeMethodPrefaceStatement());
generatedClass.addMethod(javaMethod);
-
- Collection<Type> variableTypes = localsTypesMap.values();
- for (Type variableType : variableTypes) {
-
- Collection<Class<?>> importsList =
variableType.getImportsList();
- if (importsList != null) {
- for (Class<?> importedClass : importsList) {
- generatedClass.addImport(importedClass);
- }
+ Collection<Class<?>> importsList =
currentStatement.getImports();
+ if (importsList != null) {
+ for (Class<?> importedClass : importsList) {
+ generatedClass.addImport(importedClass);
}
}
}
@@ -414,8 +405,8 @@
}
private void defineObject(Type type, String name, String initializationExpression) {
- currentStatement.addStatement(new DefineObjectStatement(type, name,
initializationExpression));
- localsTypesMap.put(name, type);
+ addStatement(new DefineObjectStatement(type, name, initializationExpression));
+ currentStatement.setVariable(name, type);
}
private void createRendersChildrenMethod() {
@@ -424,31 +415,35 @@
JavaMethod rendersChildrenMethod = new
JavaMethod("getRendersChildren", Boolean.TYPE);
rendersChildrenMethod.addModifier(JavaModifier.PUBLIC);
rendersChildrenMethod.addAnnotation(Override.class);
-
+
MethodBody methodBody = new MethodBody();
rendersChildrenMethod.setMethodBody(methodBody);
generatedClass.addMethod(rendersChildrenMethod);
-
- methodBody.addStatement(
- new
ConstantReturnMethodBodyStatement(Boolean.toString(compositeInterface.getRendersChildren())));
+
+ methodBody.addStatement(new
ConstantReturnMethodBodyStatement(Boolean.toString(compositeInterface
+ .getRendersChildren())));
}
}
-
+
protected void pushStatement(MethodBodyStatementsContainer container) {
- currentStatement.addStatement(container);
- statements.push(currentStatement);
+ addStatement(container);
currentStatement = container;
}
protected void popStatement() {
- currentStatement = statements.pop();
+ currentStatement = currentStatement.getParent();
}
+ protected void addStatement(MethodBodyStatement statement) {
+ // TODO - add imports, fields and helper methods required by statement.
+ addStatement(statement);
+ }
+
/**
* <p class="changed_added_4_0">
* </p>
- *
+ *
* @return the rendererClass
*/
public JavaClass getGeneratedClass() {
@@ -496,7 +491,7 @@
return;
}
- currentStatement.addStatement(new
StartElementStatement(elementName.getLocalPart()));
+ addStatement(new StartElementStatement(elementName.getLocalPart()));
if (elementAttributes != null) {
Set<String> writtenAttributes = new HashSet<String>();
@@ -524,8 +519,8 @@
String attributeLocalName = attributeName.getLocalPart();
if (writtenAttributes.add(attributeLocalName)) {
addRendererUtilsImport();
- currentStatement.addStatement(new
WriteAttributeStatement(attributeLocalName, compileEl(
- attributeValue.toString(), Object.class)));
+ addStatement(new WriteAttributeStatement(attributeLocalName,
compileEl(attributeValue
+ .toString(), Object.class)));
}
}
}
@@ -550,7 +545,7 @@
if (!actualAttributesMap.isEmpty()) {
String passThroughFieldName =
createPassThroughField(actualAttributesMap);
addRendererUtilsImport();
- currentStatement.addStatement(new
WriteAttributesSetStatement(passThroughFieldName));
+ addStatement(new
WriteAttributesSetStatement(passThroughFieldName));
}
}
}
@@ -567,7 +562,7 @@
@Override
public void endElement(AnyElement anyElement) throws CdkException {
QName elementName = anyElement.getName();
- currentStatement.addStatement(new
EndElementStatement(elementName.getLocalPart()));
+ addStatement(new EndElementStatement(elementName.getLocalPart()));
}
/*
@@ -581,7 +576,7 @@
if (text != null) {
String trimmedText = text.trim();
if (!Strings.isEmpty(trimmedText)) {
- currentStatement.addStatement(new
WriteTextStatement(compileEl(trimmedText, String.class)));
+ addStatement(new WriteTextStatement(compileEl(trimmedText,
String.class)));
}
}
}
@@ -600,7 +595,7 @@
expression = cdkCallElement.getBodyValue();
}
- currentStatement.addStatement(expression + ";");
+ addStatement(new MethodBodyStatementImpl(expression + ";"));
}
/*
@@ -774,7 +769,7 @@
pushStatement(new ForEachStatement(itemsExpression, cdkForEachElement.getVar(),
collectionElementClass
.getName()));
- localsTypesMap.put(cdkForEachElement.getVar(),
lastCompiledExpressionType.getContainerType());
+ currentStatement.setVariable(cdkForEachElement.getVar(),
lastCompiledExpressionType.getContainerType());
}
/*
@@ -786,7 +781,6 @@
@Override
public void endElement(CdkForEachElement cdkForEachElement) {
- localsTypesMap.remove(cdkForEachElement.getVar());
popStatement();
}
Modified:
root/cdk/branches/RF8755/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/builder/model/MethodBodyStatementsContainer.java
===================================================================
---
root/cdk/branches/RF8755/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/builder/model/MethodBodyStatementsContainer.java 2010-06-23
17:11:45 UTC (rev 17663)
+++
root/cdk/branches/RF8755/plugins/generator/src/main/java/org/richfaces/cdk/templatecompiler/builder/model/MethodBodyStatementsContainer.java 2010-06-24
05:18:38 UTC (rev 17664)
@@ -22,11 +22,11 @@
package org.richfaces.cdk.templatecompiler.builder.model;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.richfaces.cdk.templatecompiler.FreeMarkerRenderer;
-import org.richfaces.cdk.templatecompiler.el.ParsingException;
import org.richfaces.cdk.templatecompiler.el.Type;
import com.google.common.collect.Maps;
@@ -49,24 +49,22 @@
}
public void addStatement(MethodBodyStatement statement) {
- if (statement instanceof MethodBodyStatementsContainer) {
- MethodBodyStatementsContainer container = (MethodBodyStatementsContainer)
statement;
- container.setParent(this);
- }
+ setParent(statement);
statements.add(statement);
}
- public void addStatement(int index, MethodBodyStatement statement) {
- statements.add(index, statement);
+ public void addStatement(int index,MethodBodyStatement statement) {
+ setParent(statement);
+ statements.add(index,statement);
}
- public void addStatement(String statementCode) {
- addStatement(new MethodBodyStatementImpl(statementCode));
+ private void setParent(MethodBodyStatement statement) {
+ if (statement instanceof MethodBodyStatementsContainer) {
+ MethodBodyStatementsContainer container = (MethodBodyStatementsContainer)
statement;
+ container.setParent(this);
+ }
}
- public void addStatement(int index, String statementCode) {
- addStatement(index, new MethodBodyStatementImpl(statementCode));
- }
/**
* <p class="changed_added_4_0"></p>
@@ -103,7 +101,7 @@
}
@Override
- public Type getVariable(String name) throws ParsingException {
+ public Type getVariable(String name) {
Type type = localVariablesMap.get(name);
if(null == type && null != parent){
type = parent.getVariable(name);
@@ -112,7 +110,7 @@
}
@Override
- public boolean isDefined(String name) throws ParsingException {
+ public boolean isDefined(String name) {
boolean defined = localVariablesMap.containsKey(name);
if(!defined && null != parent){
defined = parent.isDefined(name);
@@ -121,9 +119,14 @@
}
@Override
- public Type setVariable(String name, Type type) throws ParsingException {
+ public Type setVariable(String name, Type type) {
Type variable = getVariable(name);
localVariablesMap.put(name, type);
return variable;
}
+
+ public Collection<Class<?>> getImports() {
+ // TODO Auto-generated method stub
+ return null;
+ }
}