Author: alexsmirnov
Date: 2010-07-28 19:37:31 -0400 (Wed, 28 Jul 2010)
New Revision: 18272
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/test/java/org/richfaces/cdk/apt/AptSourceUtilsAnnotationsTest.java
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/apt/SourceUtilsTestBase.java
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/apt/TestAnnotation.java
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/apt/AnnotationsTestClass.java
Log:
AptSourceUtils passed all annotation tests
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:11 UTC (rev 18271)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/AptSourceUtils.java 2010-07-28
23:37:31 UTC (rev 18272)
@@ -7,7 +7,9 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.NoSuchElementException;
import java.util.Set;
+import java.util.Map.Entry;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
@@ -22,11 +24,15 @@
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter;
+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.Iterables;
+import com.google.common.collect.Lists;
import com.google.inject.Inject;
public class AptSourceUtils implements SourceUtils {
@@ -235,39 +241,72 @@
return null != element.getAnnotation(annotationType);
}
+
@Override
+ 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) {
- // TODO Auto-generated method stub
- return false;
+ Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>
attributeEntry =
+ findAnnotationProperty(annotation, propertyName);
+ return !annotation.getElementValues().containsKey(attributeEntry.getKey());
}
+ @SuppressWarnings("unchecked")
@Override
public <T> T getAnnotationValue(AnnotationMirror annotation, String
propertyName, Class<T> expectedType) {
- Map<? extends ExecutableElement, ? extends AnnotationValue> elements =
processingEnv.getElementUtils().getElementValuesWithDefaults(annotation);
- for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>
attributeEntry : elements.entrySet()) {
- if(attributeEntry.getKey().getSimpleName().toString().equals(propertyName)){
- AnnotationValue annotationValue = attributeEntry.getValue();
- if(Enum.class.isAssignableFrom(expectedType)){
- VariableElement variable = (VariableElement)
annotationValue.getValue();
- Object constantValue = variable.getConstantValue();
- // TODO - properly convert into enum value.
- return (T) Enum.valueOf((Class<? extends Enum>) expectedType,
(String)constantValue);
- } else if(ClassName.class.equals(expectedType)){
- TypeMirror value = (TypeMirror) annotationValue.getValue();
- return (T) ClassName.get(value.toString());
- }
- return (T) annotationValue.getValue();
- }
+ Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>
attributeEntry =
+ findAnnotationProperty(annotation, propertyName);
+ AnnotationValue annotationValue = attributeEntry.getValue();
+ return convertAnnotationValue(expectedType, annotationValue);
+ }
+
+ private <T> T convertAnnotationValue(Class<T> expectedType,
AnnotationValue annotationValue) {
+ if (Enum.class.isAssignableFrom(expectedType)) {
+ VariableElement variable = (VariableElement) annotationValue.getValue();
+ return (T) Enum.valueOf((Class<? extends Enum>) expectedType,
variable.getSimpleName().toString());
+ } else if (ClassName.class.equals(expectedType)) {
+ TypeMirror value = (TypeMirror) annotationValue.getValue();
+ return (T) ClassName.get(value.toString());
+ } else if (AnnotationMirror.class.isAssignableFrom(expectedType)) {
+ AnnotationMirror value = (AnnotationMirror) annotationValue.getValue();
+ return (T) value;
+ } else {
+ // TODO - check value for expected type.
+ return (T) annotationValue.getValue();
}
- return null;
}
+ @SuppressWarnings("unchecked")
@Override
public <T> List<T> getAnnotationValues(AnnotationMirror annotation,
String propertyName, Class<T> expectedType) {
- // TODO Auto-generated method stub
- return null;
+ Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>
attributeEntry =
+ findAnnotationProperty(annotation, propertyName);
+ List<? extends AnnotationValue>annotationValues = (List<? extends
AnnotationValue>) attributeEntry.getValue().getValue();
+ List<T> values = Lists.newArrayList();
+ for (AnnotationValue annotationValue : annotationValues) {
+ values.add(convertAnnotationValue(expectedType, annotationValue));
+ }
+ return values;
}
+ private Entry<? extends ExecutableElement, ? extends AnnotationValue>
findAnnotationProperty(
+ AnnotationMirror annotation, final String propertyName) {
+ try {
+ 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) {
+ return processingEnv.getElementUtils().getElementValuesWithDefaults(annotation);
+ }
+
public Object getConstant(TypeElement componentElement, String name) {
List<VariableElement> fieldsIn =
ElementFilter.fieldsIn(this.processingEnv.getElementUtils().getAllMembers(componentElement));
@@ -310,6 +349,21 @@
}
}
+ private static final class AnnotationAttributePredicate implements
+ Predicate<Map.Entry<? extends ExecutableElement, ? extends
AnnotationValue>> {
+ private final String propertyName;
+
+ private AnnotationAttributePredicate(String propertyName) {
+ this.propertyName = propertyName;
+ }
+
+ @Override
+ public boolean apply(Entry<? extends ExecutableElement, ? extends
AnnotationValue> input) {
+
+ return this.propertyName.equals(input.getKey().getSimpleName().toString());
+ }
+ }
+
/**
* <p class="changed_added_4_0">
* </p>
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:11 UTC (rev 18271)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/ReflectionUtils.java 2010-07-28
23:37:31 UTC (rev 18272)
@@ -114,5 +114,9 @@
return false;
}
+ public boolean isAnnotationPropertyPresent(AnnotationMirror annotation, final String
propertyName) {
+ return false;
+ }
+
}
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:11 UTC (rev 18271)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/SourceUtils.java 2010-07-28
23:37:31 UTC (rev 18272)
@@ -216,4 +216,6 @@
*/
TypeElement asTypeElement(ClassName type);
+ public abstract boolean isAnnotationPropertyPresent(AnnotationMirror annotation,
final String propertyName);
+
}
Modified:
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/apt/AptSourceUtilsAnnotationsTest.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/apt/AptSourceUtilsAnnotationsTest.java 2010-07-28
23:37:11 UTC (rev 18271)
+++
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/apt/AptSourceUtilsAnnotationsTest.java 2010-07-28
23:37:31 UTC (rev 18272)
@@ -2,12 +2,16 @@
import static org.junit.Assert.*;
+import java.util.List;
import java.util.Set;
import javax.annotation.processing.RoundEnvironment;
+import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import org.junit.Test;
+import org.richfaces.cdk.apt.TestAnnotation.TestEnum;
+import org.richfaces.cdk.model.ClassName;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
@@ -23,36 +27,138 @@
@Test
public void testGetAnnotationMirror() {
- fail("Not yet implemented");
+ execute(new SourceUtilsCallback() {
+ @Override
+ public void process(SourceUtils utils, RoundEnvironment roundEnv) {
+ Element element = findElement(roundEnv, ANNOTATIONS_TEST_CLASS);
+ AnnotationMirror annotationMirror = utils.getAnnotationMirror(element,
TestAnnotation.class);
+ assertNotNull(annotationMirror);
+ assertEquals(TestAnnotation.class.getName(),
annotationMirror.getAnnotationType().toString());
+ }
+ });
}
@Test
public void testIsAnnotationPresent() {
- setCallback(new SourceUtilsCallback() {
-
+ execute(new SourceUtilsCallback() {
@Override
public void process(SourceUtils utils, RoundEnvironment roundEnv) {
assertTrue(utils.isAnnotationPresent(findElement(roundEnv,
ANNOTATIONS_TEST_CLASS), TestAnnotation.class));
}
});
- execute();
}
@Test
public void testIsDefaultValue() {
- fail("Not yet implemented");
+ execute(new SourceUtilsCallback() {
+ @Override
+ public void process(SourceUtils utils, RoundEnvironment roundEnv) {
+ Element element = findElement(roundEnv, ANNOTATIONS_TEST_CLASS);
+ AnnotationMirror annotationMirror = utils.getAnnotationMirror(element,
TestAnnotation.class);
+ assertTrue(utils.isDefaultValue(annotationMirror,
"withDefault"));
+ }
+ });
}
@Test
- public void testGetAnnotationValue() {
- fail("Not yet implemented");
+ public void testNotIsDefaultValue() {
+ execute(new SourceUtilsCallback() {
+ @Override
+ public void process(SourceUtils utils, RoundEnvironment roundEnv) {
+ Element element = findElement(roundEnv, ANNOTATIONS_TEST_CLASS);
+ AnnotationMirror annotationMirror = utils.getAnnotationMirror(element,
TestAnnotation.class);
+ assertFalse(utils.isDefaultValue(annotationMirror, "value"));
+ }
+ });
}
@Test
+ public void testIsAnnotationPropertyPresent() {
+ execute(new SourceUtilsCallback() {
+ @Override
+ public void process(SourceUtils utils, RoundEnvironment roundEnv) {
+ Element element = findElement(roundEnv, ANNOTATIONS_TEST_CLASS);
+ AnnotationMirror annotationMirror = utils.getAnnotationMirror(element,
TestAnnotation.class);
+ assertTrue(utils.isAnnotationPropertyPresent(annotationMirror,
"value"));
+ }
+ });
+ }
+
+ @Test
+ public void testNotIsAnnotationPropertyPresent() {
+ execute(new SourceUtilsCallback() {
+ @Override
+ public void process(SourceUtils utils, RoundEnvironment roundEnv) {
+ Element element = findElement(roundEnv, ANNOTATIONS_TEST_CLASS);
+ AnnotationMirror annotationMirror = utils.getAnnotationMirror(element,
TestAnnotation.class);
+ assertFalse(utils.isAnnotationPropertyPresent(annotationMirror,
"notExistedProperty"));
+ }
+ });
+ }
+
+ @Test
+ public void testGetStringAnnotationValue() {
+ getAndCompareAnnotationValue("value", String.class, "foo");
+ }
+
+ @Test
+ public void testGetBooleanAnnotationValue() {
+ getAndCompareAnnotationValue("booleanProperty", Boolean.class,
Boolean.TRUE);
+ }
+
+ @Test
+ public void testGetEnumAnnotationValue() {
+ getAndCompareAnnotationValue("enumProperty",
TestAnnotation.TestEnum.class, TestEnum.BAR);
+ }
+
+ @Test
+ public void testGetClassAnnotationValue() {
+ getAndCompareAnnotationValue("typeProperty", ClassName.class,
ClassName.get(PACKAGE_PATH.replace('/',
'.')+ANNOTATIONS_TEST_SUB_CLASS));
+ }
+
+ @Test
+ public void testGetDefaultStringAnnotationValue() {
+ getAndCompareAnnotationValue("withDefault", String.class,
"FOO");
+ }
+
+ public <T> void getAndCompareAnnotationValue(final String propertyName,final
Class<T> type, final Object expected) {
+ execute(new SourceUtilsCallback() {
+ @Override
+ public void process(SourceUtils utils, RoundEnvironment roundEnv) {
+ Element element = findElement(roundEnv, ANNOTATIONS_TEST_CLASS);
+ AnnotationMirror annotationMirror = utils.getAnnotationMirror(element,
TestAnnotation.class);
+ T annotationValue = utils.getAnnotationValue(annotationMirror,
propertyName, type);
+ assertEquals("Annotation value is different from
expected",expected, annotationValue);
+ }
+ });
+ }
+
+ public <T> void getAndCompareAnnotationValues(final String propertyName,final
Class<T> type, final Object ... expected) {
+ execute(new SourceUtilsCallback() {
+ @Override
+ public void process(SourceUtils utils, RoundEnvironment roundEnv) {
+ Element element = findElement(roundEnv, ANNOTATIONS_TEST_CLASS);
+ AnnotationMirror annotationMirror = utils.getAnnotationMirror(element,
TestAnnotation.class);
+ List<T> annotationValues =
utils.getAnnotationValues(annotationMirror, propertyName, type);
+ assertEquals("Annotation values size is different from
expected",expected.length, annotationValues.size());
+ for (int i = 0; i < expected.length; i++) {
+ Object expectedValue = expected[i];
+ assertEquals("Annotation value at position "+i+" is
different from expected",expectedValue, annotationValues.get(i));
+ }
+ }
+ });
+ }
+
+ @Test
public void testGetAnnotationValues() {
- fail("Not yet implemented");
+ getAndCompareAnnotationValues("emptyStrings", String.class);
}
+ @Test
+ public void testGetClassAnnotationValues() {
+ getAndCompareAnnotationValues("types",
ClassName.class,ClassName.get(String.class),ClassName.get(Object.class));
+ }
+
@Override
protected Iterable<String> sources() {
return ImmutableList.of(CLASS_JAVA, SUB_CLASS_JAVA);
Modified:
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/apt/SourceUtilsTestBase.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/apt/SourceUtilsTestBase.java 2010-07-28
23:37:11 UTC (rev 18271)
+++
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/apt/SourceUtilsTestBase.java 2010-07-28
23:37:31 UTC (rev 18272)
@@ -71,11 +71,9 @@
binder.bind(CdkProcessor.class).to(TestProcessor.class).in(Singleton.class);
}
- protected void setCallback(SourceUtilsCallback callback){
- ((TestProcessor)processor).callback = callback;
- }
- protected void execute(){
+ protected void execute(SourceUtilsCallback callback){
+ ((TestProcessor)processor).callback = callback;
assertTrue("Compilation error",factory.get().call());
}
Modified:
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/apt/TestAnnotation.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/apt/TestAnnotation.java 2010-07-28
23:37:11 UTC (rev 18271)
+++
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/apt/TestAnnotation.java 2010-07-28
23:37:31 UTC (rev 18272)
@@ -39,5 +39,23 @@
@Documented
@Target(ElementType.TYPE)
public @interface TestAnnotation {
- public String value();
+
+ public enum TestEnum {
+ FOO,
+ BAR
+ }
+
+ String value();
+
+ String withDefault() default "FOO";
+
+ boolean booleanProperty() default true;
+
+ TestEnum enumProperty() default TestEnum.BAR;
+
+ Class<?> typeProperty();
+
+ String[] emptyStrings() default {};
+
+ Class<?>[] types() default {String.class,Object.class};
}
Modified:
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/apt/AnnotationsTestClass.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/apt/AnnotationsTestClass.java 2010-07-28
23:37:11 UTC (rev 18271)
+++
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/apt/AnnotationsTestClass.java 2010-07-28
23:37:31 UTC (rev 18272)
@@ -30,5 +30,5 @@
* @author asmirnov(a)exadel.com
*
*/
-@TestAnnotation("foo")
+@TestAnnotation(value="foo",typeProperty=AnnotationsTestSubClass.class)
public class AnnotationsTestClass {}