From: <iro...@us...> - 2009-08-15 16:48:30
|
Revision: 126 http://pojomatic.svn.sourceforge.net/pojomatic/?rev=126&view=rev Author: iroberts Date: 2009-08-15 16:48:17 +0000 (Sat, 15 Aug 2009) Log Message: ----------- Don't allow static properties. Modified Paths: -------------- trunk/Pojomatic/src/main/java/org/pojomatic/internal/ClassProperties.java trunk/Pojomatic/src/test/java/org/pojomatic/internal/ClassPropertiesTest.java Modified: trunk/Pojomatic/src/main/java/org/pojomatic/internal/ClassProperties.java =================================================================== --- trunk/Pojomatic/src/main/java/org/pojomatic/internal/ClassProperties.java 2009-08-15 16:04:29 UTC (rev 125) +++ trunk/Pojomatic/src/main/java/org/pojomatic/internal/ClassProperties.java 2009-08-15 16:48:17 UTC (rev 126) @@ -64,11 +64,21 @@ for (Field field : clazz.getDeclaredFields()) { Property property = field.getAnnotation(Property.class); + if (isStatic(field)) { + if (property != null) { + throw new IllegalArgumentException( + "Static field " + clazz.getName() + "." + field.getName() + + " is annotated with @Property"); + } + else { + continue; + } + } + final PojomaticPolicy propertyPolicy = (property != null) ? property.policy() : null; /* add all fields that are explicitly annotated or auto-detected */ - if (propertyPolicy != null || - (AutoDetectPolicy.FIELD == autoDetectPolicy && !isStatic(field))) { + if (propertyPolicy != null || AutoDetectPolicy.FIELD == autoDetectPolicy) { addPropertyToRoles( new PropertyField(field, getPropertyName(property)), classPolicy, propertyPolicy); } @@ -76,6 +86,17 @@ for (Method method : clazz.getDeclaredMethods()) { Property property = method.getAnnotation(Property.class); + if (isStatic(method)) { + if (property != null) { + throw new IllegalArgumentException( + "Static method " + clazz.getName() + "." + method.getName() + + "() is annotated with @Property"); + } + else { + continue; + } + } + PojomaticPolicy propertyPolicy = null; if (property != null) { if (!methodSignatureIsAccessor(method)) { Modified: trunk/Pojomatic/src/test/java/org/pojomatic/internal/ClassPropertiesTest.java =================================================================== --- trunk/Pojomatic/src/test/java/org/pojomatic/internal/ClassPropertiesTest.java 2009-08-15 16:04:29 UTC (rev 125) +++ trunk/Pojomatic/src/test/java/org/pojomatic/internal/ClassPropertiesTest.java 2009-08-15 16:48:17 UTC (rev 126) @@ -169,7 +169,33 @@ assertEquals(expectedChild, asSet(childClassProperties.getHashCodeProperties())); assertEquals(expectedChild, asSet(childClassProperties.getToStringProperties())); } + + @Test + public void testAnnotatedStaticField() { + try { + ClassProperties.createInstance(StaticField.class); + fail("Exception expected"); + } + catch (IllegalArgumentException e) { + assertEquals( + "Static field " + StaticField.class.getName() + ".a is annotated with @Property", + e.getMessage()); + } + } + @Test + public void testAnnotatedStaticMethod() { + try { + ClassProperties.createInstance(StaticMethod.class); + fail("Exception expected"); + } + catch (IllegalArgumentException e) { + assertEquals( + "Static method " + StaticMethod.class.getName() + ".a() is annotated with @Property", + e.getMessage()); + } + } + public static class FieldPojo { @SuppressWarnings("unused") @Property @@ -297,7 +323,15 @@ public String getBar() { return ""; } } + + public static class StaticField { + @Property public static int a; + } + public static class StaticMethod { + @Property public static int a() { return 1; } + } + private static Set<PropertyElement> asSet(PropertyElement... elements) { HashSet<PropertyElement> result = new HashSet<PropertyElement>(); for (PropertyElement element : elements) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |