You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by tv...@apache.org on 2009/05/15 02:34:43 UTC
svn commit: r774972 - in /incubator/pivot/trunk/wtk/src/pivot/wtkx:
BindProcessor.java WTKXSerializer.java
Author: tvolkert
Date: Fri May 15 00:34:42 2009
New Revision: 774972
URL: http://svn.apache.org/viewvc?rev=774972&view=rev
Log:
Updated WTKX compilation code
Modified:
incubator/pivot/trunk/wtk/src/pivot/wtkx/BindProcessor.java
incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java
Modified: incubator/pivot/trunk/wtk/src/pivot/wtkx/BindProcessor.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtkx/BindProcessor.java?rev=774972&r1=774971&r2=774972&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtkx/BindProcessor.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtkx/BindProcessor.java Fri May 15 00:34:42 2009
@@ -288,19 +288,12 @@
if (loadGroups != null || strandedBindFields != null) {
// There is some bind work to be done in this class
StringBuilder buf = new StringBuilder("class _TMP {");
- buf.append("@Override @SuppressWarnings(\"unchecked\") ");
+ buf.append("@Override @SuppressWarnings({\"unchecked\",\"cast\"}) ");
buf.append("protected void bind(pivot.collections.Dictionary<String,");
buf.append("pivot.collections.Dictionary<String, Object>> namedObjectDictionaries) {");
buf.append("super.bind(namedObjectDictionaries);");
- // Local variable declarations
- buf.append("Object object;");
- buf.append("pivot.wtkx.WTKXSerializer wtkxSerializer;");
- buf.append("java.net.URL location;");
- buf.append("java.util.Locale locale;");
- buf.append("pivot.util.Resources resources;");
-
- // Process @Load fields
+ // Process @Load fields (and their associated @Bind fields)
if (loadGroups != null) {
for (String loadFieldName : loadGroups) {
AnnotationDossier.LoadGroup loadGroup = loadGroups.get(loadFieldName);
@@ -395,28 +388,30 @@
private void processCompiledLoad(StringBuilder buf, JCClassDecl classDeclaration,
JCVariableDecl loadField, ArrayList<JCVariableDecl> bindFields) {
FileObject sourceFile = null;
+
if (classDeclaration.sym != null) {
sourceFile = classDeclaration.sym.sourcefile;
}
if (sourceFile != null) {
- JavaFileManager fileManager = context.get(JavaFileManager.class);
-
String loadFieldName = loadField.name.toString();
- // Get annotation properties
- JCAnnotation loadAnnotation = getLoadAnnotation(loadField);
- String resourceName = getAnnotationProperty(loadAnnotation, "resourceName");
-
- InputStream inputStream;
try {
+ JavaFileManager fileManager = context.get(JavaFileManager.class);
+
+ // Get annotation properties
+ JCAnnotation loadAnnotation = getLoadAnnotation(loadField);
+ String resourceName = getAnnotationProperty(loadAnnotation, "resourceName");
+
+ InputStream inputStream;
if (resourceName.startsWith("/")) {
- resourceName = resourceName.substring(1);
+ // Absolute URL uses ClassLoader
ClassLoader classLoader = fileManager.getClassLoader
(StandardLocation.SOURCE_PATH);
- URL resourceLocation = classLoader.getResource(resourceName);
+ URL resourceLocation = classLoader.getResource(resourceName.substring(1));
inputStream = new BufferedInputStream(resourceLocation.openStream());
} else {
+ // Relative URL uses JavaFileManager
String packageName = classDeclaration.sym.packge().toString();
FileObject resourceFile = fileManager.getFileForInput
(StandardLocation.SOURCE_PATH, packageName, resourceName);
@@ -437,33 +432,33 @@
// Public and protected fields get kept for subclasses
if ((loadField.mods.flags & (Flags.PUBLIC | Flags.PROTECTED)) != 0) {
buf.append(String.format
- ("namedObjectDictionaries.put(\"%s\", __namedObjects);",
+ ("namedObjectDictionaries.put(\"%s\", _namedObjects);",
loadFieldName));
}
// Bind @Load member
buf.append(String.format
- ("%s = (%s)__result;", loadFieldName, loadField.vartype.toString()));
+ ("%s = (%s)_result;", loadFieldName, loadField.vartype.toString()));
// Bind @Bind members
for (JCVariableDecl bindField : bindFields) {
String bindFieldName = bindField.name.toString();
JCAnnotation bindAnnotation = getBindAnnotation(bindField);
- String wtkxID = getAnnotationProperty(bindAnnotation, "id");
- if (wtkxID == null) {
+ String id = getAnnotationProperty(bindAnnotation, "id");
+ if (id == null) {
// The bind name defaults to the field name
- wtkxID = bindFieldName;
+ id = bindFieldName;
}
buf.append(String.format
- ("object = __namedObjects.get(\"%s\");", wtkxID));
+ ("_result = _namedObjects.get(\"%s\");", id));
buf.append
- ("if (object == null) ");
+ ("if (_result == null) ");
buf.append(String.format
- ("throw new pivot.wtkx.BindException(\"Element not found: %s.\");", wtkxID));
+ ("throw new pivot.wtkx.BindException(\"Element not found: %s.\");", id));
buf.append(String.format
- ("%s = (%s)object;", bindFieldName, bindField.vartype.toString()));
+ ("%s = (%s)_result;", bindFieldName, bindField.vartype.toString()));
}
// Close local scope
@@ -472,14 +467,24 @@
inputStream.close();
}
} catch (Exception ex) {
+ ex.printStackTrace();
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
"Error trying to load field " + classDeclaration.name.toString() + "." +
loadFieldName + ": " + ex.toString());
}
} else {
- processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
- "Unable to determine source file location for " +
- classDeclaration.name.toString());
+ if (classDeclaration.name.isEmpty()) {
+ // NOTE: Anonymous inner classes do not have an associated
+ // symbol, so their source file cannot be determined, and
+ // we cannot compile their annotations. This bug has been
+ // filed to Sun
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
+ "WTKX compilation of anonymous classes is not supported.");
+ } else {
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
+ "Unable to determine source file location for class: " +
+ classDeclaration.name.toString());
+ }
}
}
@@ -504,6 +509,9 @@
JCVariableDecl loadField, ArrayList<JCVariableDecl> bindFields) {
String loadFieldName = loadField.name.toString();
+ // Open local scope for variable name protection
+ buf.append("{");
+
// Get annotation properties
JCAnnotation loadAnnotation = getLoadAnnotation(loadField);
String resourceName = getAnnotationProperty(loadAnnotation, "resourceName");
@@ -520,13 +528,13 @@
}
// Attempt to load the resources
- buf.append("resources = null;");
+ buf.append("pivot.util.Resources resources = null;");
if (baseName != null) {
if (language == null) {
- buf.append("locale = java.util.Locale.getDefault();");
+ buf.append("java.util.Locale locale = java.util.Locale.getDefault();");
} else {
buf.append(String.format
- ("locale = new java.util.Locale(\"%s\");", language));
+ ("java.util.Locale locale = new java.util.Locale(\"%s\");", language));
}
buf.append("try {");
buf.append(String.format
@@ -544,8 +552,9 @@
}
// Load the WTKX resource
- buf.append("wtkxSerializer = new pivot.wtkx.WTKXSerializer(resources);");
- buf.append(String.format("location = getClass().getResource(\"%s\");", resourceName));
+ buf.append("Object object = null;");
+ buf.append("pivot.wtkx.WTKXSerializer wtkxSerializer = new pivot.wtkx.WTKXSerializer(resources);");
+ buf.append(String.format("java.net.URL location = getClass().getResource(\"%s\");", resourceName));
buf.append("try {");
buf.append("object = wtkxSerializer.readObject(location);");
buf.append("} catch (Exception ex) {");
@@ -568,21 +577,24 @@
String bindFieldName = bindField.name.toString();
JCAnnotation bindAnnotation = getBindAnnotation(bindField);
- String wtkxID = getAnnotationProperty(bindAnnotation, "id");
- if (wtkxID == null) {
+ String id = getAnnotationProperty(bindAnnotation, "id");
+ if (id == null) {
// The bind name defaults to the field name
- wtkxID = bindFieldName;
+ id = bindFieldName;
}
buf.append(String.format
- ("object = wtkxSerializer.getObjectByID(\"%s\");", wtkxID));
+ ("object = wtkxSerializer.getObjectByID(\"%s\");", id));
buf.append
("if (object == null) ");
buf.append(String.format
- ("throw new pivot.wtkx.BindException(\"Element not found: %s.\");", wtkxID));
+ ("throw new pivot.wtkx.BindException(\"Element not found: %s.\");", id));
buf.append(String.format
("%s = (%s)object;", bindFieldName, bindField.vartype.toString()));
}
+
+ // Close local scope
+ buf.append("}");
}
/**
@@ -605,10 +617,10 @@
JCAnnotation bindAnnotation = getBindAnnotation(bindField);
String loadFieldName = getAnnotationProperty(bindAnnotation, "fieldName");
- String wtkxID = getAnnotationProperty(bindAnnotation, "id");
- if (wtkxID == null) {
+ String id = getAnnotationProperty(bindAnnotation, "id");
+ if (id == null) {
// The bind name defaults to the field name
- wtkxID = bindFieldName;
+ id = bindFieldName;
}
buf.append(String.format
@@ -622,11 +634,11 @@
("}");
buf.append(String.format
- ("object = namedObjects.get(\"%s\");", wtkxID));
+ ("object = namedObjects.get(\"%s\");", id));
buf.append
("if (object == null) ");
buf.append(String.format
- ("throw new pivot.wtkx.BindException(\"Element not found: %s.\");", wtkxID));
+ ("throw new pivot.wtkx.BindException(\"Element not found: %s.\");", id));
buf.append(String.format
("%s = (%s)object;", bindFieldName, bindField.vartype.toString()));
}
Modified: incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java?rev=774972&r1=774971&r2=774972&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java Fri May 15 00:34:42 2009
@@ -729,8 +729,6 @@
/**
* Interprets an object from a WTKX input stream.
- * <p>
- * TODO This method should take a ClassLoader.
*
* @param inputStream
* The data stream from which the WTKX will be read
@@ -745,8 +743,8 @@
}
StringBuilder buf = new StringBuilder();
- buf.append("Object __result = null;");
- buf.append("pivot.collections.Dictionary<String, Object> __namedObjects = " +
+ buf.append("Object _result = null;");
+ buf.append("pivot.collections.Dictionary<String, Object> _namedObjects = " +
"new pivot.collections.HashMap<String, Object>();");
// Parse the XML stream
@@ -819,13 +817,12 @@
+ localName + " tag.");
}
- String className = namespaceURI + "." + localName.replace('.', '$');
-
+ String className = namespaceURI + "." + localName;
buf.append(String.format
- ("%s __%d = new %s();", className, ++Element.counter, className));
+ ("%s _%d = new %s();", className, ++Element.counter, className));
try {
- // TODO Pass ClassLoader here
+ className = namespaceURI + "." + localName.replace('.', '$');
Class<?> type = Class.forName(className, false, getClass().getClassLoader());
element = new Element(element, Element.Type.INSTANCE,
attributes, type, Element.counter);
@@ -850,10 +847,10 @@
// Instantiate the property so we have a reference to it
buf.append(String.format
- ("%s __%d = __%d.%s();", valueType.getName(), ++Element.counter,
+ ("%s _%d = _%d.%s();", valueType.getName().replace('$', '.'), ++Element.counter,
element.ref, getterMethod.getName()));
buf.append(String.format
- ("assert (__%d != null) : \"Read-only properties cannot be null.\";",
+ ("assert (_%d != null) : \"Read-only properties cannot be null.\";",
Element.counter));
element = new Element(element, Element.Type.READ_ONLY_PROPERTY,
@@ -881,7 +878,7 @@
if (parentType != null
&& (Sequence.class.isAssignableFrom(parentType)
|| ListenerList.class.isAssignableFrom(parentType))) {
- buf.append(String.format("__%d.add(__%d);",
+ buf.append(String.format("_%d.add(_%d);",
element.parent.ref, element.ref));
}
}
@@ -893,7 +890,7 @@
+ " must not be null.");
}
- buf.append(String.format("__namedObjects.put(\"%s\", __%d);", id, element.ref));
+ buf.append(String.format("_namedObjects.put(\"%s\", _%d);", id, element.ref));
}
break;
@@ -912,7 +909,7 @@
Class<?> parentType = (Class<?>)element.parent.value;
Method setterMethod = BeanDictionary.getSetterMethod(parentType, localName, type);
- buf.append(String.format("__%d.%s(__%d);",
+ buf.append(String.format("_%d.%s(_%d);",
element.parent.ref, setterMethod.getName(), element.ref));
break;
@@ -934,22 +931,29 @@
}
// Resolve and apply the attribute
- // TODO Resolve
- // TODO attribute.value shouldn't always be quoted
- buf.append(String.format
- ("__%d.put(\"%s\", \"%s\")",
- element.ref, attribute.localName, attribute.value));
+ buf.append(String.format("_%d.put(\"%s\", %s);", element.ref,
+ attribute.localName, resolveSource(attribute.value, null)));
}
} else {
// The element represents a typed object; apply the attributes
for (Attribute attribute : element.attributes) {
+ Class<?> attributeType = BeanDictionary.getType(type, attribute.localName);
+
if (Character.isUpperCase(attribute.localName.charAt(0))) {
// The property represents an attached value
- // TODO
- //setStaticProperty(attribute, element.value);
- } else {
- Class<?> attributeType = BeanDictionary.getType(type, attribute.localName);
+ String propertyName = attribute.localName.substring
+ (attribute.localName.lastIndexOf(".") + 1);
+ propertyName = Character.toUpperCase(propertyName.charAt(0)) +
+ propertyName.substring(1);
+ String setterMethodName = BeanDictionary.SET_PREFIX + propertyName;
+
+ String propertyClassName = attribute.namespaceURI + "."
+ + attribute.localName.substring(0, attribute.localName.length()
+ - (propertyName.length() + 1));
+ buf.append(String.format("%s.%s(_%d, %s);", propertyClassName,
+ setterMethodName, element.ref, resolveSource(attribute.value, attributeType)));
+ } else {
if (attributeType != null
&& ListenerList.class.isAssignableFrom(attributeType)) {
// The property represents a listener list
@@ -976,10 +980,8 @@
attribute.localName.substring(1);
String setterMethodName = BeanDictionary.SET_PREFIX + key;
- buf.append(resolveSource(attribute.value, attributeType,
- ++Element.counter));
- buf.append(String.format("__%d.%s(__%d);", element.ref,
- setterMethodName, Element.counter));
+ buf.append(String.format("_%d.%s(%s);", element.ref,
+ setterMethodName, resolveSource(attribute.value, attributeType)));
}
}
}
@@ -998,7 +1000,7 @@
// If this is the top of the stack, return this element's value;
// otherwise, move up the stack
if (element.parent == null) {
- buf.append(String.format("__result = __%d;", element.ref));
+ buf.append(String.format("_result = _%d;", element.ref));
} else {
element = element.parent;
}
@@ -1201,117 +1203,104 @@
return resolvedValue;
}
- private String resolveSource(String attributeValue, Class<?> propertyType, int ref)
+ private String resolveSource(String attributeValue, Class<?> propertyType)
throws MalformedURLException {
- StringBuilder buf = new StringBuilder();
+ String result = null;
if (propertyType == Boolean.class
|| propertyType == Boolean.TYPE) {
- buf.append(String.format("boolean __%d = %b;",
- ref, Boolean.parseBoolean(attributeValue)));
+ result = String.valueOf(Boolean.parseBoolean(attributeValue));
} else if (propertyType == Character.class
|| propertyType == Character.TYPE) {
if (attributeValue.length() > 0) {
- buf.append(String.format("char __%d = '%c';",
- ref, attributeValue.charAt(0)));
+ result = String.format("'%c'", attributeValue.charAt(0));
}
} else if (propertyType == Byte.class
|| propertyType == Byte.TYPE) {
try {
- buf.append(String.format("byte __%d = %d;",
- ref, Byte.parseByte(attributeValue)));
+ result = String.format("(byte)%d", Byte.parseByte(attributeValue));
} catch(NumberFormatException exception) {
- buf.append(String.format("String __%d = \"%s\";", ref, attributeValue));
+ result = String.format("\"%s\"", attributeValue);
}
} else if (propertyType == Short.class
|| propertyType == Short.TYPE) {
try {
- buf.append(String.format("short __%d = %d;",
- ref, Short.parseShort(attributeValue)));
+ result = String.format("(short)%d", Short.parseShort(attributeValue));
} catch(NumberFormatException exception) {
- buf.append(String.format("String __%d = \"%s\";", ref, attributeValue));
+ result = String.format("\"%s\"", attributeValue);
}
} else if (propertyType == Integer.class
|| propertyType == Integer.TYPE) {
try {
- buf.append(String.format("int __%d = %d;",
- ref, Integer.parseInt(attributeValue)));
+ result = String.format("(int)%d", Integer.parseInt(attributeValue));
} catch(NumberFormatException exception) {
- buf.append(String.format("String __%d = \"%s\";", ref, attributeValue));
+ result = String.format("\"%s\"", attributeValue);
}
} else if (propertyType == Long.class
|| propertyType == Long.TYPE) {
try {
- buf.append(String.format("long __%d = %d;",
- ref, Long.parseLong(attributeValue)));
+ result = String.format("(long)%d", Long.parseLong(attributeValue));
} catch(NumberFormatException exception) {
- buf.append(String.format("String __%d = \"%s\";", ref, attributeValue));
+ result = String.format("\"%s\"", attributeValue);
}
} else if (propertyType == Float.class
|| propertyType == Float.TYPE) {
try {
- buf.append(String.format("float __%d = %f;",
- ref, Float.parseFloat(attributeValue)));
+ result = String.format("(float)%f", Float.parseFloat(attributeValue));
} catch(NumberFormatException exception) {
- buf.append(String.format("String __%d = \"%s\";", ref, attributeValue));
+ result = String.format("\"%s\"", attributeValue);
}
} else if (propertyType == Double.class
|| propertyType == Double.TYPE) {
try {
- buf.append(String.format("double __%d = %f;",
- ref, Double.parseDouble(attributeValue)));
+ result = String.format("(double)%f", Double.parseDouble(attributeValue));
} catch(NumberFormatException exception) {
- buf.append(String.format("String __%d = \"%s\";", ref, attributeValue));
+ result = String.format("\"%s\"", attributeValue);
}
} else {
if (attributeValue.length() > 0) {
if (attributeValue.charAt(0) == URL_PREFIX) {
if (attributeValue.length() > 1) {
if (attributeValue.charAt(1) == URL_PREFIX) {
- buf.append(String.format("String __%d = \"%s\";",
- ref, attributeValue.substring(1)));
+ result = String.format("\"%s\"", attributeValue.substring(1));
} else {
- buf.append(String.format
- ("java.net.URL __%d = getClass().getResource(\"%s\");",
- ref, attributeValue.substring(1)));
+ result = String.format("getClass().getResource(\"%s\")", attributeValue.substring(1));
}
}
} else if (attributeValue.charAt(0) == RESOURCE_KEY_PREFIX) {
if (attributeValue.length() > 1) {
if (attributeValue.charAt(1) == RESOURCE_KEY_PREFIX) {
- buf.append(String.format("String __%d = \"%s\";",
- ref, attributeValue.substring(1)));
+ result = String.format("\"%s\"", attributeValue.substring(1));
} else {
// TODO
- buf.append(String.format("String __%d = \"%s\";", ref, attributeValue));
+ result = String.format("\"%s\"", attributeValue);
}
}
} else if (attributeValue.charAt(0) == OBJECT_REFERENCE_PREFIX) {
if (attributeValue.length() > 1) {
if (attributeValue.charAt(1) == OBJECT_REFERENCE_PREFIX) {
- buf.append(String.format("String __%d = \"%s\";",
- ref, attributeValue.substring(1)));
+ result = String.format("\"%s\"", attributeValue.substring(1));
} else {
- buf.append(String.format("%s __%d = (%s)__namedObjects.get(\"%s\");",
- propertyType.getName(), ref, propertyType.getName(),
- attributeValue.substring(1)));
+ String className = propertyType == null ?
+ "Object" : propertyType.getName();
+ result = String.format("(%s)_namedObjects.get(\"%s\");",
+ className, attributeValue.substring(1));
}
}
} else {
- buf.append(String.format("String __%d = \"%s\";",
- ref, attributeValue));
+ result = String.format("\"%s\"", attributeValue);
}
} else {
- buf.append(String.format("String __%d = \"\";", ref));
+ result = "\"\"";
}
}
- if (buf.length() == 0) {
+ if (result == null) {
// Fall-through case
- buf.append(String.format("String __%d = null;", ref));
+ result = "null";
}
- return buf.toString();
+ return result;
}
/**