Author: alexsmirnov
Date: 2010-07-28 19:37:47 -0400 (Wed, 28 Jul 2010)
New Revision: 18273
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/AptSourceUtils.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/ReflectionUtils.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/SourceUtils.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/processors/AttributesProcessorImpl.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/util/PropertyUtils.java
Log:
https://jira.jboss.org/browse/RF-8907
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/AptSourceUtils.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/AptSourceUtils.java 2010-07-28
23:37:31 UTC (rev 18272)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/AptSourceUtils.java 2010-07-28
23:37:47 UTC (rev 18273)
@@ -1,5 +1,6 @@
package org.richfaces.cdk.apt;
+import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
@@ -307,6 +308,34 @@
return processingEnv.getElementUtils().getElementValuesWithDefaults(annotation);
}
+ /**
+ * <p class="changed_added_4_0">Set model property to the
corresponding annotation attribute, if annotation attribute set to non-default
value.</p>
+ * @param model Model object.
+ * @param annotation annotation to copy property from.
+ * @param modelProperty bean attribute name in the model and annotation.
+ */
+ @Override
+ public void setModelProperty(Object model, AnnotationMirror annotation, String
modelProperty) {
+ setModelProperty(model, annotation, modelProperty, modelProperty);
+ }
+
+ /**
+ * <p class="changed_added_4_0">Set model property to the
corresponding annotation attribute, if annotation attribute set to non-default
value.</p>
+ * @param model Model object.
+ * @param annotation annotation to copy property from.
+ * @param modelProperty bean attribute name in model.
+ * @param annotationAttribute annotation attribute name.
+ */
+ @Override
+ public void setModelProperty(Object model, AnnotationMirror annotation,
+ String modelProperty, String annotationAttribute) {
+ if (!isDefaultValue(annotation, annotationAttribute)) {
+ PropertyDescriptor propertyDescriptor =
PropertyUtils.getPropertyDescriptor(model, modelProperty);
+ PropertyUtils.setPropertyValue(model, modelProperty,
getAnnotationValue(annotation,
+ annotationAttribute, propertyDescriptor.getPropertyType()));
+ }
+ }
+
public Object getConstant(TypeElement componentElement, String name) {
List<VariableElement> fieldsIn =
ElementFilter.fieldsIn(this.processingEnv.getElementUtils().getAllMembers(componentElement));
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/ReflectionUtils.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/ReflectionUtils.java 2010-07-28
23:37:31 UTC (rev 18272)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/ReflectionUtils.java 2010-07-28
23:37:47 UTC (rev 18273)
@@ -118,5 +118,11 @@
return false;
}
+ public void setModelProperty(Object model, AnnotationMirror annotation, String
modelProperty, String annotationAttribute) {
+ }
+ public void setModelProperty(Object model, AnnotationMirror annotation, String
modelProperty) {
+ }
+
+
}
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/SourceUtils.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/SourceUtils.java 2010-07-28
23:37:31 UTC (rev 18272)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/SourceUtils.java 2010-07-28
23:37:47 UTC (rev 18273)
@@ -218,4 +218,8 @@
public abstract boolean isAnnotationPropertyPresent(AnnotationMirror annotation,
final String propertyName);
+ public abstract void setModelProperty(Object model, AnnotationMirror annotation,
String modelProperty, String annotationAttribute);
+
+ public abstract void setModelProperty(Object model, AnnotationMirror annotation,
String modelProperty);
+
}
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/processors/AttributesProcessorImpl.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/processors/AttributesProcessorImpl.java 2010-07-28
23:37:31 UTC (rev 18272)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/processors/AttributesProcessorImpl.java 2010-07-28
23:37:47 UTC (rev 18273)
@@ -2,18 +2,15 @@
import java.io.FileNotFoundException;
import java.util.Collection;
-import java.util.List;
import java.util.Set;
+import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.TypeElement;
-import javax.lang.model.type.MirroredTypeException;
-import javax.lang.model.type.MirroredTypesException;
import javax.lang.model.type.TypeMirror;
import org.richfaces.cdk.CdkException;
import org.richfaces.cdk.Logger;
import org.richfaces.cdk.annotations.Attribute;
-import org.richfaces.cdk.annotations.EventName;
import org.richfaces.cdk.annotations.Signature;
import org.richfaces.cdk.apt.SourceUtils;
import org.richfaces.cdk.apt.SourceUtils.BeanProperty;
@@ -22,11 +19,10 @@
import org.richfaces.cdk.model.ClassName;
import org.richfaces.cdk.model.MethodSignature;
import org.richfaces.cdk.model.PropertyBase;
-import org.richfaces.cdk.util.Strings;
+import org.richfaces.cdk.util.JavaUtils;
import org.richfaces.cdk.xmlconfig.CdkEntityResolver;
import org.richfaces.cdk.xmlconfig.FragmentParser;
-import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
import com.google.inject.Provider;
@@ -42,7 +38,7 @@
private static final ClassName SIGNATURE_NONE_CLASS_NAME =
ClassName.get(Signature.NONE.class);
- private static final String STRING_NAME = String.class.getName();
+ private static final ClassName STRING_TYPE = ClassName.get(String.class);
@Inject
private Logger log;
@@ -73,7 +69,7 @@
attribute.setType(beanProperty.getType());
- Attribute attributeAnnotarion = beanProperty.getAnnotation(Attribute.class);
+ AnnotationMirror attributeAnnotarion =
beanProperty.getAnnotationMirror(Attribute.class);
if (attributeAnnotarion == null) {
attribute.setGenerate(!beanProperty.isExists());
attribute.setDescription(beanProperty.getDocComment());
@@ -86,44 +82,44 @@
}
}
} else {
- attribute.setHidden(attributeAnnotarion.hidden());
- attribute.setLiteral(attributeAnnotarion.literal());
- attribute.setPassThrough(attributeAnnotarion.passThrough());
- attribute.setRequired(attributeAnnotarion.required());
- attribute.setReadOnly(attributeAnnotarion.readOnly());
- attribute.setGenerate(attributeAnnotarion.generate() ||
!beanProperty.isExists());
+ SourceUtils utils = utilsProvider.get();
+ utils.setModelProperty(attribute, attributeAnnotarion, "hidden");
+ utils.setModelProperty(attribute, attributeAnnotarion,
"literal");
+ utils.setModelProperty(attribute, attributeAnnotarion,
"passThrough");
+ utils.setModelProperty(attribute, attributeAnnotarion,
"required");
+ utils.setModelProperty(attribute, attributeAnnotarion,
"readOnly");
+ utils.setModelProperty(attribute, attributeAnnotarion,
"generate");
- descriptionProcessor.processDescription(attribute,
attributeAnnotarion.description(), beanProperty
+ descriptionProcessor.processDescription(attribute,
beanProperty.getAnnotation(Attribute.class).description(), beanProperty
.getDocComment());
setDefaultValue(attribute, attributeAnnotarion);
- String suggestedValue = attributeAnnotarion.suggestedValue();
- if (!Strings.isEmpty(suggestedValue)) {
- attribute.setSuggestedValue(suggestedValue);
- }
+ utils.setModelProperty(attribute, attributeAnnotarion,
"suggestedValue");
// MethodExpression call signature.
- attribute.setSignature(getSignature(attributeAnnotarion.signature()));
+ attribute.setSignature(getSignature(attributeAnnotarion));
- for (EventName event : attributeAnnotarion.events()) {
+ for (AnnotationMirror event : utils.getAnnotationValues(attributeAnnotarion,
"events", AnnotationMirror.class)){
setBehaviorEvent(attribute, event);
}
}
}
- private void setDefaultValue(PropertyBase attribute, Attribute attributeAnnotarion)
{
- String defaultValue = attributeAnnotarion.defaultValue();
- if (Strings.isEmpty(defaultValue)) {
+
+ private void setDefaultValue(PropertyBase attribute, AnnotationMirror
attributeAnnotarion) {
+ SourceUtils utils = utilsProvider.get();
+ String defaultValue;
+ // TODO - move to model validator.
+ if (utils.isDefaultValue(attributeAnnotarion, "defaultValue")) {
if (attribute.getType().isPrimitive()) {
String pimitiveDefaultValue =
getPimitiveDefaultValue(attribute.getType().getName());
- if (pimitiveDefaultValue != null) {
- attribute.setDefaultValue(pimitiveDefaultValue);
- }
+ attribute.setDefaultValue(pimitiveDefaultValue);
}
} else {
- if (STRING_NAME.equals(attribute.getType().toString())) {
- defaultValue = "\"" + defaultValue + "\"";
+ defaultValue = utils.getAnnotationValue(attributeAnnotarion,
"defaultValue", String.class);
+ if (STRING_TYPE.equals(attribute.getType())) {
+ defaultValue = JavaUtils.getEscapedString(defaultValue);
}
attribute.setDefaultValue(defaultValue);
}
@@ -154,51 +150,33 @@
return byteClass.getSimpleName().equals(typeName);
}
- private MethodSignature getSignature(Signature signature) {
+ private MethodSignature getSignature(AnnotationMirror attributeAnnotarion) {
- ClassName returnType;
+ SourceUtils utils = utilsProvider.get();
- try {
- returnType = ClassName.get(signature.returnType());
- } catch (MirroredTypeException e) {
- TypeMirror returnTypeMirror = e.getTypeMirror();
- returnType = ClassName.get(returnTypeMirror.toString());
- }
+ if (!utils.isDefaultValue(attributeAnnotarion, "signature")) {
+ AnnotationMirror signatureAnnotation =
+ utils.getAnnotationValue(attributeAnnotarion, "signature",
AnnotationMirror.class);
+ ClassName returnType = utils.getAnnotationValue(signatureAnnotation,
"returnType", ClassName.class);
- if (!SIGNATURE_NONE_CLASS_NAME.equals(returnType)) {
- MethodSignature methodSignature = new MethodSignature();
- methodSignature.setParameters(getSignatureParams(signature));
- methodSignature.setReturnType(returnType);
- return methodSignature;
+ if (!SIGNATURE_NONE_CLASS_NAME.equals(returnType)) {
+ MethodSignature methodSignature = new MethodSignature();
+
methodSignature.setParameters(utils.getAnnotationValues(signatureAnnotation,
"parameters",
+ ClassName.class));
+ methodSignature.setReturnType(returnType);
+ return methodSignature;
+ }
}
-
return null;
}
- private List<ClassName> getSignatureParams(Signature signature) {
- List<ClassName> parameters = Lists.newArrayList();
- try {
- for (Class<?> parameterType : signature.parameters()) {
- parameters.add(ClassName.get(parameterType));
- }
- } catch (MirroredTypeException e) {
- parameters.add(ClassName.get(e.getTypeMirror().toString()));
- } catch (MirroredTypesException e) {
- for (TypeMirror parameterType : e.getTypeMirrors()) {
- parameters.add(new ClassName(parameterType.toString()));
- }
- }
-
- return parameters;
- }
-
- private void setBehaviorEvent(PropertyBase attribute, EventName eventName) {
- if (null != eventName) {
+ private void setBehaviorEvent(PropertyBase attribute, AnnotationMirror eventMirror)
{
+ if (null != eventMirror) {
+ SourceUtils utils = utilsProvider.get();
org.richfaces.cdk.model.EventName event = new
org.richfaces.cdk.model.EventName();
-
- event.setName(eventName.value());
- event.setDefaultEvent(eventName.defaultEvent());
+ utils.setModelProperty(event, eventMirror, "name",
"value");
+ utils.setModelProperty(event, eventMirror, "defaultEvent");
attribute.getEventNames().add(event);
}
}
@@ -223,7 +201,7 @@
} catch (CdkException e) {
log.error(e);
} catch (FileNotFoundException e) {
- log.debug("No properties description found at "+uri);
+ log.debug("No properties description found at " + uri);
}
}
@@ -255,7 +233,7 @@
} catch (CdkException e) {
log.error(e);
} catch (FileNotFoundException e) {
- log.debug("No properties description found at "+attributes);
+ log.debug("No properties description found at " + attributes);
}
}
}
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/util/PropertyUtils.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/util/PropertyUtils.java 2010-07-28
23:37:31 UTC (rev 18272)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/util/PropertyUtils.java 2010-07-28
23:37:47 UTC (rev 18273)
@@ -23,6 +23,12 @@
package org.richfaces.cdk.util;
+import java.beans.IntrospectionException;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import java.util.NoSuchElementException;
+
+import org.richfaces.cdk.CdkException;
import org.richfaces.cdk.model.InvalidNameException;
/**
@@ -47,4 +53,33 @@
throw new InvalidNameException("Method name " + methodName + " is
not valid bean property getter or setter");
}
+
+ public static void setPropertyValue(Object bean,String propertyName,Object
newValue){
+ PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean,
propertyName);
+ if(null != propertyDescriptor.getWriteMethod()){
+ try {
+ propertyDescriptor.getWriteMethod().invoke(bean, newValue);
+ } catch (Exception e) {
+ throw new CdkException("Cannot set new value for bean property
"+bean.getClass().getName()+"#"+propertyName,e);
+ }
+ } else {
+ throw new CdkException("Bean property not writable
"+bean.getClass().getName()+"#"+propertyName);
+
+ }
+ }
+
+ public static PropertyDescriptor getPropertyDescriptor(Object bean,String
propertyName){
+ PropertyDescriptor[] properties;
+ try {
+ properties =
Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
+ } catch (IntrospectionException e) {
+ throw new CdkException(e);
+ }
+ for (PropertyDescriptor propertyDescriptor : properties) {
+ if(propertyName.equals(propertyDescriptor.getName())){
+ return propertyDescriptor;
+ }
+ }
+ throw new NoSuchElementException("Bean property "+propertyName+"
not found in class "+bean.getClass().getName());
+ }
}