Author: alexsmirnov
Date: 2010-12-28 19:53:56 -0500 (Tue, 28 Dec 2010)
New Revision: 20835
Added:
branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/apt/PropertyImpl.java
Modified:
branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/apt/AptSourceUtils.java
branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/apt/ReflectionUtils.java
branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/apt/SourceUtils.java
branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/model/validator/ValidatorImpl.java
branches/RF-9323/cdk/generator/src/test/java/org/richfaces/cdk/apt/AptSourceUtilsPropertiesTest.java
Log:
CODING IN PROGRESS - issue RF-9323: CDK annotation @RendererSpecificComponent.attributes
doesn't work
https://issues.jboss.org/browse/RF-9323
Modified:
branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/apt/AptSourceUtils.java
===================================================================
---
branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/apt/AptSourceUtils.java 2010-12-28
22:22:16 UTC (rev 20834)
+++
branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/apt/AptSourceUtils.java 2010-12-29
00:53:56 UTC (rev 20835)
@@ -2,15 +2,12 @@
import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Set;
-import java.util.Map.Entry;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
@@ -24,25 +21,43 @@
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter;
-import javax.lang.model.util.ElementKindVisitor6;
import org.richfaces.cdk.CdkException;
import org.richfaces.cdk.Logger;
import org.richfaces.cdk.model.ClassName;
-import org.richfaces.cdk.model.InvalidNameException;
import org.richfaces.cdk.util.PropertyUtils;
import com.google.common.base.Predicate;
+import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.inject.Inject;
+/**
+ * <p class="changed_added_4_0">
+ * Implementation to use in annotation processor.
+ * </p>
+ *
+ * @author asmirnov(a)exadel.com
+ *
+ */
public class AptSourceUtils implements SourceUtils {
- private static final Set<String> PROPERTIES =
- new HashSet<String>(Arrays.asList("getEventNames",
"getDefaultEventName", "getClientBehaviors", "getFamily"));
+ private static final String IS = "is";
- private static final Predicate<Element> BEAN_PROPERTY_PREDICATE = new
BeanPropertyMethodPredicate();
+ private static final int IS_LENGTH = IS.length();
+
+ private static final String GET = "get";
+
+ private static final String SET = "set";
+
+ private static final int SET_LENGTH = SET.length();
+
+ private static final int GET_LENGTH = GET.length();
+
+ private static final ImmutableSet<String> HIDDEN_PROPERTIES =
ImmutableSet.of("eventNames", "defaultEventName",
+ "clientBehaviors", "family", "class");
+
private final ProcessingEnvironment processingEnv;
@Inject
@@ -69,210 +84,140 @@
*/
public Set<BeanProperty> getBeanPropertiesAnnotatedWith(Class<? extends
Annotation> annotation, TypeElement type) {
Set<BeanProperty> properties = new HashSet<BeanProperty>();
- List<? extends Element> members =
this.processingEnv.getElementUtils().getAllMembers(type);
-
- // Get all methods and fields annotated by annotation.
- for (Element childElement : members) {
- boolean annotated = null != childElement.getAnnotation(annotation);
- if (!annotated) {
- continue;
+ Map<String, AptBeanProperty> beanProperties = getBeanProperties(type);
+ for (BeanProperty beanProperty : beanProperties.values()) {
+ if (beanProperty.isAnnotationPresent(annotation)) {
+ properties.add(beanProperty);
}
-
- // Have an annotation, infer property name.
- if (ElementKind.METHOD.equals(childElement.getKind())) {
- processMethod(properties, childElement, annotated);
- } else if (ElementKind.FIELD.equals(childElement.getKind())) {
-// processFiled(properties, childElement);
- }
-
- // TODO - merge properties with same name ?
}
-
return properties;
}
public Set<BeanProperty> getAbstractBeanProperties(TypeElement type) {
- log.debug("AptSourceUtils.getAbstractBeanProperties");
- log.debug(" - type = " + type);
-
Set<BeanProperty> properties = new HashSet<BeanProperty>();
- List<? extends Element> members =
this.processingEnv.getElementUtils().getAllMembers(type);
-
- Map<String, List<ExecutableElement>> props =
groupMethodsBySignature(members);
- removeNotAbstractGroups(props);
-
- for (List<ExecutableElement> methods : props.values()) {
- ExecutableElement method = methods.get(0);
-
- if (ElementKind.METHOD.equals(method.getKind()) &&
!PROPERTIES.contains(method.getSimpleName().toString())) {
- processMethod(properties, method, false);
+ Map<String, AptBeanProperty> beanProperties = getBeanProperties(type);
+ for (BeanProperty beanProperty : beanProperties.values()) {
+ if (!beanProperty.isExists()) {
+ properties.add(beanProperty);
}
-
- // TODO - merge properties with same name ?
}
-
return properties;
}
@Override
- public BeanProperty getBeanProperty(TypeElement type, String name) {
- List<? extends Element> members =
this.processingEnv.getElementUtils().getAllMembers(type);
- return null;
+ public BeanProperty getBeanProperty(ClassName type, final String name) {
+ return getBeanProperty(asTypeElement(type), name);
}
+ @Override
+ public BeanProperty getBeanProperty(TypeElement type, final String name) {
+ Map<String, AptBeanProperty> beanProperties = getBeanProperties(type);
+ if (beanProperties.containsKey(name)) {
+ return beanProperties.get(name);
+ } else {
+ return new PropertyImpl(name);
+ }
+ }
+
/**
- * <p class="changed_added_4_0">Utility method to get all bean
properties, similar to Introspector</p>
+ * <p class="changed_added_4_0">
+ * Utility method to get all bean properties, similar to Introspector
+ * </p>
+ *
* @param type
* @return
*/
- Map<String,BeanProperty> getBeanProperties(TypeElement type){
+ Map<String, AptBeanProperty> getBeanProperties(TypeElement type) {
List<? extends Element> members =
this.processingEnv.getElementUtils().getAllMembers(type);
// extract all getters/setters.
- Map<String,BeanProperty> result = Maps.newHashMap();
+ Map<String, AptBeanProperty> result = Maps.newHashMap();
for (Element element : members) {
- if (ElementKind.METHOD.equals(element.getKind())){
+ if (ElementKind.METHOD.equals(element.getKind())) {
ExecutableElement method = (ExecutableElement) element;
- if(isPublicNonStatic(method)){
- if(isGetter(method)){
- String propertyName =
PropertyUtils.methodToName(method.getSimpleName().toString());
- if(result.containsKey(propertyName)){
- // Merge property with existed one.
- BeanProperty beanProperty = result.get(propertyName);
-// processingEnv.getElementUtils().
- } else {
-
- }
- } else if (isSetter(method)) {
-
- }
- }
+ processMethod(type, result, method);
}
}
return result;
}
-
- private void removeNotAbstractGroups(Map<String, List<ExecutableElement>>
props) {
- List<String> removeKeys = new ArrayList<String>();
- for (Map.Entry<String, List<ExecutableElement>> entry :
props.entrySet()) {
- List<ExecutableElement> value = entry.getValue();
- for (ExecutableElement element : value) {
- if (!isAbstract(element)) {
- removeKeys.add(entry.getKey());
- }
+
+ private void processMethod(TypeElement type, Map<String, AptBeanProperty>
result, ExecutableElement method) {
+ if (isPublicNonStatic(method)) {
+ if (isGetter(method)) {
+ processBeanPropertyAccessor(type, result, method, false);
+ } else if (isSetter(method)) {
+ processBeanPropertyAccessor(type, result, method, true);
}
}
-
- for (String removeKey : removeKeys) {
- props.remove(removeKey);
- }
}
- private Map<String, List<ExecutableElement>>
groupMethodsBySignature(List<? extends Element> members) {
- Map<String, List<ExecutableElement>> props = new HashMap<String,
List<ExecutableElement>>();
- for (Element element : members) {
- if (ElementKind.METHOD.equals(element.getKind())
- && !PROPERTIES.contains(element.getSimpleName().toString())) {
-
- ExecutableElement method = (ExecutableElement) element;
-
- String signature = getSignature(method);
-
- List<ExecutableElement> methods = props.get(signature);
- if (methods == null) {
- methods = new ArrayList<ExecutableElement>(5);
- props.put(signature, methods);
+ private void processBeanPropertyAccessor(TypeElement type, Map<String,
AptBeanProperty> result,
+ ExecutableElement method, boolean setter) {
+ String propertyName = getPropertyName(method);
+ if (!HIDDEN_PROPERTIES.contains(propertyName)) {
+ ClassName propertyType = asClassDescription(method.getReturnType());
+ if (result.containsKey(propertyName)) {
+ // Merge property with existed one.
+ AptBeanProperty beanProperty = result.get(propertyName);
+ if (null != beanProperty.getter) {
+ log.warn("Two " + (setter ? "setter" :
"getter") + " methods for the same bean property "
+ + propertyName + " in the class " +
type.getQualifiedName());
}
-
- methods.add(method);
+ checkPropertyType(type, propertyName, propertyType, beanProperty);
+ beanProperty.setAccessMethod(method, setter);
+ } else {
+ AptBeanProperty beanProperty = new AptBeanProperty(propertyName);
+ beanProperty.setAccessMethod(method, setter);
+ beanProperty.type = propertyType;
+ result.put(propertyName, beanProperty);
}
+
}
- return props;
}
- private String getSignature(ExecutableElement method) {
- String name = method.getSimpleName().toString();
- List<? extends VariableElement> methodParams = method.getParameters();
- StringBuilder builder = new StringBuilder(name);
- for (VariableElement methodParam : methodParams) {
- builder.append(":").append(methodParam.getKind().name());
- }
- return builder.toString();
+ private String getPropertyName(ExecutableElement method) {
+ return PropertyUtils.methodToName(method.getSimpleName().toString());
}
- private void processMethod(Set<BeanProperty> properties, Element childElement,
boolean annotated) {
- ExecutableElement method = (ExecutableElement) childElement;
- boolean exists = !isAbstract(method);
- if (!annotated && exists) {
- log.debug(" - " + childElement.getSimpleName() + " :
didn't annotated and didn't abstract.");
- return;
+ private void checkPropertyType(TypeElement type, String propertyName, ClassName
propertyType,
+ AptBeanProperty beanProperty) {
+ if (!propertyType.equals(beanProperty.type)) {
+ log.warn("Unambiguious type for bean property " + propertyName +
" in the class " + type.getQualifiedName());
}
-
- TypeMirror propertyType = method.getReturnType();
- List<? extends VariableElement> parameters = method.getParameters();
- if (TypeKind.VOID.equals(propertyType.getKind()) && 1 ==
parameters.size()) {
-
- // That is setter method, get type from parameter.
- propertyType = parameters.get(0).asType();
- } else if (!parameters.isEmpty()) {
- // TODO Invalid method signature for a bean property,
- // throw exception ?
- log.debug(" - " + childElement.getSimpleName() + " :
Invalid method signature for a bean property.");
- return;
- }
-
- try {
- String name =
PropertyUtils.methodToName(childElement.getSimpleName().toString());
- AptBeanProperty property = new AptBeanProperty(name);
-
- property.type = asClassDescription(propertyType);
- property.getter = (ExecutableElement) childElement;
- property.exists = exists;
-
- properties.add(property);
- log.debug(" - " + childElement.getSimpleName() + " : was
added.");
-
- } catch (InvalidNameException e) {
- log.debug(" - " + childElement.getSimpleName() + " :
Invalid method name for a bean property, throw.");
-
- // TODO Invalid method name for a bean property, throw
- // exception ?
- }
}
private boolean isAbstract(ExecutableElement method) {
return method.getModifiers().contains(Modifier.ABSTRACT);
}
- private boolean isPublicNonStatic(ExecutableElement method){
+ private boolean isPublicNonStatic(ExecutableElement method) {
Set<Modifier> modifiers = method.getModifiers();
return modifiers.contains(Modifier.PUBLIC) &&
!modifiers.contains(Modifier.STATIC);
}
-
+
private boolean isGetter(ExecutableElement e) {
String methodName = e.getSimpleName().toString();
- return (isGetterName(methodName)||isBooleanGetterName(methodName)) && 0
== e.getParameters().size();
+ return (isGetterName(methodName) || isBooleanGetterName(methodName)) && 0
== e.getParameters().size();
}
private boolean isGetterName(String methodName) {
- return methodName.startsWith("get") && methodName.length()>3
&& Character.isUpperCase(methodName.charAt(3));
+ return methodName.startsWith(GET) && methodName.length() > GET_LENGTH
+ && Character.isUpperCase(methodName.charAt(GET_LENGTH));
}
-
- private boolean isBooleanGetter(ExecutableElement e) {
- String methodName = e.getSimpleName().toString();
- return isBooleanGetterName(methodName) && 0 == e.getParameters().size()
&& TypeKind.BOOLEAN.equals(e.getReturnType().getKind());
- }
private boolean isBooleanGetterName(String methodName) {
- return methodName.startsWith("is") && methodName.length()>2
&& Character.isUpperCase(methodName.charAt(2));
+ return methodName.startsWith(IS) && methodName.length() > IS_LENGTH
+ && Character.isUpperCase(methodName.charAt(IS_LENGTH));
}
-
+
private boolean isSetter(ExecutableElement e) {
String methodName = e.getSimpleName().toString();
- return isSetterName(methodName) && 1 == e.getParameters().size()
&& !e.isVarArgs() && TypeKind.VOID.equals(e.getReturnType().getKind());
+ return isSetterName(methodName) && 1 == e.getParameters().size()
&& !e.isVarArgs()
+ && TypeKind.VOID.equals(e.getReturnType().getKind());
}
private boolean isSetterName(String methodName) {
- return methodName.startsWith("set") && methodName.length()>3
&& Character.isUpperCase(methodName.charAt(3));
+ return methodName.startsWith(SET) && methodName.length() > SET_LENGTH
+ && Character.isUpperCase(methodName.charAt(SET_LENGTH));
}
private ClassName asClassDescription(TypeMirror type) {
@@ -302,12 +247,12 @@
return null != element.getAnnotation(annotationType);
}
-
@Override
- public boolean isAnnotationPropertyPresent(AnnotationMirror annotation, final String
propertyName){
- return Iterables.any(getAnnotationValuesMap(annotation).entrySet(), new
AnnotationAttributePredicate(propertyName));
+ public boolean isAnnotationPropertyPresent(AnnotationMirror annotation, final String
propertyName) {
+ return Iterables.any(getAnnotationValuesMap(annotation).entrySet(), new
AnnotationAttributePredicate(
+ propertyName));
}
-
+
@Override
public boolean isDefaultValue(AnnotationMirror annotation, String propertyName) {
Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>
attributeEntry =
@@ -315,7 +260,6 @@
return !annotation.getElementValues().containsKey(attributeEntry.getKey());
}
- @SuppressWarnings("unchecked")
@Override
public <T> T getAnnotationValue(AnnotationMirror annotation, String
propertyName, Class<T> expectedType) {
Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>
attributeEntry =
@@ -324,6 +268,7 @@
return convertAnnotationValue(expectedType, annotationValue);
}
+ @SuppressWarnings("unchecked")
private <T> T convertAnnotationValue(Class<T> expectedType,
AnnotationValue annotationValue) {
if (Enum.class.isAssignableFrom(expectedType)) {
VariableElement variable = (VariableElement) annotationValue.getValue();
@@ -335,7 +280,6 @@
AnnotationMirror value = (AnnotationMirror) annotationValue.getValue();
return (T) value;
} else {
- // TODO - check value for expected type.
return (T) annotationValue.getValue();
}
}
@@ -345,7 +289,8 @@
public <T> List<T> getAnnotationValues(AnnotationMirror annotation,
String propertyName, Class<T> expectedType) {
Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>
attributeEntry =
findAnnotationProperty(annotation, propertyName);
- List<? extends AnnotationValue>annotationValues = (List<? extends
AnnotationValue>) attributeEntry.getValue().getValue();
+ List<? extends AnnotationValue> annotationValues =
+ (List<? extends AnnotationValue>)
attributeEntry.getValue().getValue();
List<T> values = Lists.newArrayList();
for (AnnotationValue annotationValue : annotationValues) {
values.add(convertAnnotationValue(expectedType, annotationValue));
@@ -356,43 +301,57 @@
private Entry<? extends ExecutableElement, ? extends AnnotationValue>
findAnnotationProperty(
AnnotationMirror annotation, final String propertyName) {
try {
- return Iterables.find(getAnnotationValuesMap(annotation).entrySet(),
- new AnnotationAttributePredicate(propertyName));
+ return Iterables.find(getAnnotationValuesMap(annotation).entrySet(), new
AnnotationAttributePredicate(
+ propertyName));
} catch (NoSuchElementException e) {
throw new CdkException("Attribute " + propertyName + " not
found for annotation "
+ annotation.getAnnotationType().toString());
}
}
- private Map<? extends ExecutableElement, ? extends AnnotationValue>
getAnnotationValuesMap(AnnotationMirror annotation) {
+ private Map<? extends ExecutableElement, ? extends AnnotationValue>
getAnnotationValuesMap(
+ AnnotationMirror annotation) {
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.
+ * <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);
+ 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.
+ * <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) {
+ 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()));
+ PropertyUtils.setPropertyValue(model, modelProperty,
+ getAnnotationValue(annotation, annotationAttribute,
propertyDescriptor.getPropertyType()));
}
}
@@ -424,11 +383,14 @@
visitor.visit(type);
}
- @Override
- public TypeElement asTypeElement(ClassName type) {
+ private TypeElement asTypeElement(ClassName type) {
return processingEnv.getElementUtils().getTypeElement(type.toString());
}
+ public boolean isClassExists(ClassName type){
+ return null != asTypeElement(type);
+ }
+
@Override
public TypeElement asTypeElement(TypeMirror mirror) {
if (TypeKind.DECLARED.equals(mirror.getKind())) {
@@ -438,45 +400,13 @@
}
}
- private static final class BeanPropertyMethodPredicate extends
ElementKindVisitor6<Boolean, Boolean> implements Predicate<Element> {
-
- public BeanPropertyMethodPredicate() {
- super(Boolean.FALSE);
- }
-
- @Override
- public Boolean visitExecutableAsMethod(ExecutableElement e, Boolean p) {
- Set<Modifier> modifiers = e.getModifiers();
- // bean accessors have to be publis, non-static.
- if(modifiers.contains(Modifier.PUBLIC) &&
!modifiers.contains(Modifier.STATIC)){
- if(isGetter(e)||isBooleanGetter(e)||isSetter(e)){
- return Boolean.TRUE;
- }
- }
- return DEFAULT_VALUE;
- }
-
- private boolean isGetter(ExecutableElement e) {
- String methodName = e.getSimpleName().toString();
- return methodName.startsWith("get") &&
methodName.length()>3 && Character.isUpperCase(methodName.charAt(3)) &&
0 == e.getParameters().size();
- }
-
- private boolean isBooleanGetter(ExecutableElement e) {
- String methodName = e.getSimpleName().toString();
- return methodName.startsWith("is") &&
methodName.length()>2 && Character.isUpperCase(methodName.charAt(2)) &&
0 == e.getParameters().size() &&
TypeKind.BOOLEAN.equals(e.getReturnType().getKind());
- }
-
- private boolean isSetter(ExecutableElement e) {
- String methodName = e.getSimpleName().toString();
- return methodName.startsWith("set") &&
methodName.length()>3 && Character.isUpperCase(methodName.charAt(3)) &&
1 == e.getParameters().size() && !e.isVarArgs() &&
TypeKind.VOID.equals(e.getReturnType().getKind());
- }
-
- @Override
- public boolean apply(Element input) {
- return visit(input);
- }
- }
-
+ /**
+ * <p class="changed_added_4_0">
+ * </p>
+ *
+ * @author asmirnov(a)exadel.com
+ *
+ */
private static final class AnnotationAttributePredicate implements
Predicate<Map.Entry<? extends ExecutableElement, ? extends
AnnotationValue>> {
private final String propertyName;
@@ -502,9 +432,6 @@
protected final class AptBeanProperty implements BeanProperty {
private ExecutableElement getter;
private ExecutableElement setter;
- private String docComment;
- private Map<Class<? extends Annotation>,AnnotationMirror> annotations
= Maps.newHashMap();
- private boolean exists;
private final String name;
private ClassName type;
@@ -518,6 +445,103 @@
this.name = name;
}
+ void setAccessMethod(ExecutableElement method, boolean setter) {
+ if (setter) {
+ this.setter = method;
+ } else {
+ this.getter = method;
+ }
+ }
+
+ /**
+ * <p class="changed_added_4_0">
+ * </p>
+ *
+ * @return the name
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * <p class="changed_added_4_0">
+ * Get JavaDoc comment of appropriate bean property element.
+ * </p>
+ *
+ * @return
+ */
+ public String getDocComment() {
+ String comment = getMethodComment(getter);
+ if (null == comment) {
+ comment = getMethodComment(setter);
+ }
+ return comment;
+ }
+
+ private String getMethodComment(ExecutableElement method) {
+ if (null != method) {
+ return processingEnv.getElementUtils().getDocComment(method);
+ } else {
+ return null;
+ }
+ }
+
+ public ClassName getType() {
+ return type;
+ }
+
+ /**
+ * <p class="changed_added_4_0">
+ * </p>
+ *
+ * @return the exists
+ */
+ public boolean isExists() {
+ return !(isAbstract(getter) || isAbstract(setter));
+ }
+
+ private boolean isAbstract(ExecutableElement method) {
+ return null != method &&
method.getModifiers().contains(Modifier.ABSTRACT);
+ }
+
+ public AnnotationMirror getAnnotationMirror(Class<? extends Annotation>
annotationType) {
+ if (isAnnotationPresent(getter, annotationType)) {
+ return AptSourceUtils.this.getAnnotationMirror(getter, annotationType);
+ } else if (isAnnotationPresent(setter, annotationType)) {
+ return AptSourceUtils.this.getAnnotationMirror(setter, annotationType);
+ }
+ return null;
+ }
+
+ @Override
+ public boolean isAnnotationPresent(Class<? extends Annotation>
annotationType) {
+ return isAnnotationPresent(getter, annotationType) ||
isAnnotationPresent(setter, annotationType);
+ }
+
+ @Override
+ public <T extends Annotation> T getAnnotation(Class<T>
annotationType) {
+ if (isAnnotationPresent(getter, annotationType)) {
+ return getter.getAnnotation(annotationType);
+ } else if (isAnnotationPresent(setter, annotationType)) {
+ return setter.getAnnotation(annotationType);
+ }
+ return null;
+ }
+
+ private <T extends Annotation> boolean
isAnnotationPresent(ExecutableElement method, Class<T> annotationType) {
+ return null != method && null !=
method.getAnnotation(annotationType);
+ }
+
+ @Override
+ public ACCESS_TYPE getAccessType() {
+ if (null != getter && null != setter) {
+ return ACCESS_TYPE.readWrite;
+ } else if (null == setter) {
+ return ACCESS_TYPE.readOnly;
+ }
+ return ACCESS_TYPE.writeOnly;
+ }
+
/*
* (non-Javadoc)
*
@@ -565,45 +589,10 @@
return true;
}
- /**
- * <p class="changed_added_4_0">
- * </p>
- *
- * @return the name
- */
- public String getName() {
- return name;
+ @Override
+ public String toString() {
+ return name + "[" + getType() + "]";
}
-
- /**
- * <p class="changed_added_4_0">
- * Get JavaDoc comment of appropriate bean property element.
- * </p>
- *
- * @return
- */
- public String getDocComment() {
- return processingEnv.getElementUtils().getDocComment(getter);
- }
-
- public ClassName getType() {
- return type;
- }
-
- /**
- * <p class="changed_added_4_0">
- * </p>
- *
- * @return the exists
- */
- public boolean isExists() {
- return exists;
- }
-
- public AnnotationMirror getAnnotationMirror(Class<? extends Annotation>
annotationType) {
- return AptSourceUtils.this.getAnnotationMirror(getter, annotationType);
- }
-
}
}
Added:
branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/apt/PropertyImpl.java
===================================================================
--- branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/apt/PropertyImpl.java
(rev 0)
+++
branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/apt/PropertyImpl.java 2010-12-29
00:53:56 UTC (rev 20835)
@@ -0,0 +1,57 @@
+package org.richfaces.cdk.apt;
+
+import java.lang.annotation.Annotation;
+
+import javax.lang.model.element.AnnotationMirror;
+
+import org.richfaces.cdk.apt.SourceUtils.ACCESS_TYPE;
+import org.richfaces.cdk.apt.SourceUtils.BeanProperty;
+import org.richfaces.cdk.model.ClassName;
+
+public final class PropertyImpl implements BeanProperty {
+ private final String name;
+
+ public PropertyImpl(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public boolean isExists() {
+ return false;
+ }
+
+ @Override
+ public boolean isAnnotationPresent(Class<? extends Annotation> annotationType)
{
+ return false;
+ }
+
+ @Override
+ public ClassName getType() {
+ return ClassName.get(Object.class);
+ }
+
+ @Override
+ public String getName() {
+ return this.name;
+ }
+
+ @Override
+ public String getDocComment() {
+ return null;
+ }
+
+ @Override
+ public AnnotationMirror getAnnotationMirror(Class<? extends Annotation>
annotationType) {
+ return null;
+ }
+
+ @Override
+ public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
+ return null;
+ }
+
+ @Override
+ public ACCESS_TYPE getAccessType() {
+ return ACCESS_TYPE.readWrite;
+ }
+}
\ No newline at end of file
Property changes on:
branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/apt/PropertyImpl.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified:
branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/apt/ReflectionUtils.java
===================================================================
---
branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/apt/ReflectionUtils.java 2010-12-28
22:22:16 UTC (rev 20834)
+++
branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/apt/ReflectionUtils.java 2010-12-29
00:53:56 UTC (rev 20835)
@@ -49,9 +49,8 @@
}
@Override
- public TypeElement asTypeElement(ClassName type) {
- // TODO Auto-generated method stub
- return null;
+ public boolean isClassExists(ClassName type) {
+ return true;
}
@Override
@@ -68,6 +67,11 @@
@Override
public BeanProperty getBeanProperty(TypeElement type, String name) {
+ return getBeanProperty(type, name);
+ }
+
+ @Override
+ public BeanProperty getBeanProperty(ClassName type, String name) {
// TODO Auto-generated method stub
return null;
}
Modified:
branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/apt/SourceUtils.java
===================================================================
---
branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/apt/SourceUtils.java 2010-12-28
22:22:16 UTC (rev 20834)
+++
branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/apt/SourceUtils.java 2010-12-29
00:53:56 UTC (rev 20835)
@@ -59,6 +59,11 @@
void visit(TypeMirror type);
}
+ enum ACCESS_TYPE {
+ readOnly,
+ writeOnly,
+ readWrite
+ }
/**
* <p class="changed_added_4_0">
* </p>
@@ -85,18 +90,28 @@
*/
String getDocComment();
+ /**
+ * <p class="changed_added_4_0">Bean property type</p>
+ * @return
+ */
ClassName getType();
/**
- * <p class="changed_added_4_0">
+ * <p class="changed_added_4_0">Is this property implementted by
component
* </p>
*
* @return the exists
*/
boolean isExists();
+
+ boolean isAnnotationPresent(Class<? extends Annotation> annotationType);
AnnotationMirror getAnnotationMirror(Class<? extends Annotation>
annotationType);
+ <T extends Annotation> T getAnnotation(Class<T> annotationType);
+
+ ACCESS_TYPE getAccessType();
+
}
/**
@@ -129,6 +144,14 @@
BeanProperty getBeanProperty(TypeElement type, String name);
/**
+ * <p class="changed_added_4_0">Get bean property descriptor for
particular type.</p>
+ * @param type
+ * @param name
+ * @return
+ */
+ BeanProperty getBeanProperty(ClassName type, String name);
+
+ /**
* <p class="changed_added_4_0">
* Get JavaDoc comments associated with given element.
* </p>
@@ -245,8 +268,8 @@
* </p>
*
* @param type
- * @return TypeElement for given type, or null if corresponding name does not exist.
+ * @return true if class already exist in project source or dependent libraries.
*/
- TypeElement asTypeElement(ClassName type);
+ boolean isClassExists(ClassName type);
}
Modified:
branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/model/validator/ValidatorImpl.java
===================================================================
---
branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/model/validator/ValidatorImpl.java 2010-12-28
22:22:16 UTC (rev 20834)
+++
branches/RF-9323/cdk/generator/src/main/java/org/richfaces/cdk/model/validator/ValidatorImpl.java 2010-12-29
00:53:56 UTC (rev 20835)
@@ -51,6 +51,7 @@
import org.richfaces.cdk.model.FacesId;
import org.richfaces.cdk.model.FacetModel;
import org.richfaces.cdk.model.InvalidNameException;
+import org.richfaces.cdk.model.ModelElement;
import org.richfaces.cdk.model.PropertyBase;
import org.richfaces.cdk.model.RenderKitModel;
import org.richfaces.cdk.model.RendererModel;
@@ -232,7 +233,7 @@
// TODO - infer listener interface name.
}
SourceUtils sourceUtils = sourceUtilsProvider.get();
- event.setGenerateListener(null ==
sourceUtils.asTypeElement(listenerInterface));
+ event.setGenerateListener(!sourceUtils.isClassExists(listenerInterface));
String methodName = event.getListenerMethod();
if (null == methodName) {
// TODO infer listener method name.
@@ -243,7 +244,7 @@
if (null == sourceInterface) {
// TODO - infer source interface.
}
- event.setGenerateSource(null == sourceUtils.asTypeElement(sourceInterface));
+ event.setGenerateSource(!sourceUtils.isClassExists(listenerInterface));
// Propagate event to corresponding components.
for (ComponentModel component : library.getComponents()) {
for (EventModel componentEvent : component.getEvents()) {
@@ -376,7 +377,7 @@
} // Check attributes.
for (PropertyBase attribute : component.getAttributes()) {
- verifyAttribute(attribute, component.getGenerate());
+ verifyAttribute(attribute, component);
}
// compact(component.getAttributes());
// Check renderers.
@@ -461,7 +462,7 @@
}
}
// Check classes.
- if (component.getGenerate()) {
+ if (Boolean.TRUE.equals(component.getGenerate())) {
if (null == component.getBaseClass()) {
component.setBaseClass(callback.getDefaultBaseClass());
// return;
@@ -482,7 +483,7 @@
return true;
}
- protected void verifyAttribute(PropertyBase attribute, boolean generatedComponent) {
+ protected void verifyAttribute(PropertyBase attribute, FacesComponent component) {
// Check name.
if (Strings.isEmpty(attribute.getName())) {
log.error("No name for attribute " + attribute);
@@ -509,7 +510,7 @@
// log.error("Signature for method expression attribute
"+attribute.getName()+" has not been set");
// }
// Check "generate" flag.
- if (generatedComponent) {
+ if (Boolean.TRUE.equals(component.getGenerate())) {
// TODO Attribute should be only generated if it does not exist or abstract
in the base class.
// Step one - check base class
} else {
Modified:
branches/RF-9323/cdk/generator/src/test/java/org/richfaces/cdk/apt/AptSourceUtilsPropertiesTest.java
===================================================================
---
branches/RF-9323/cdk/generator/src/test/java/org/richfaces/cdk/apt/AptSourceUtilsPropertiesTest.java 2010-12-28
22:22:16 UTC (rev 20834)
+++
branches/RF-9323/cdk/generator/src/test/java/org/richfaces/cdk/apt/AptSourceUtilsPropertiesTest.java 2010-12-29
00:53:56 UTC (rev 20835)
@@ -160,7 +160,7 @@
public void process(SourceUtils utils, RoundEnvironment roundEnv) {
AptSourceUtils aptUtils = (AptSourceUtils) utils;
TypeElement subClassType = (TypeElement) findElement(roundEnv,
TEST_SUB_CLASS);
- assertEquals(5, aptUtils.getBeanProperties(subClassType).size());
+ assertEquals(6, aptUtils.getBeanProperties(subClassType).size());
}
});
}