Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

FIELD level plug-in parameter injection

  • Loading branch information...
commit b778171a319e45771c9baa85698fb905aaa82323 1 parent c0c2a3c
Brian Cowdery authored
View
2  billing-ng-core/src/main/java/com/billing/ng/dao/PersistenceProvider.java
@@ -17,7 +17,7 @@
package com.billing.ng.dao;
-import org.jboss.seam.solder.core.ExtensionManaged;
+import org.jboss.solder.core.ExtensionManaged;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.inject.Produces;
View
50 billing-ng-core/src/main/java/com/billing/ng/plugin/Parameters.java
@@ -18,12 +18,14 @@
package com.billing.ng.plugin;
import com.billing.ng.plugin.annotation.Parameter;
+import com.billing.ng.util.ClassUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import javax.xml.registry.infomodel.ClassificationScheme;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
@@ -59,10 +61,12 @@ public void populate(T plugin, Map<String, String> parameters) {
String fieldName = entry.getKey();
Parameter parameter = entry.getValue();
+ // get parameter value to populate the plug-in with
String value = parameters.get(parameter.name());
- if (isBlank(value)) value = parameter.defaultValue();
+ if (StringUtils.isBlank(value)) value = parameter.defaultValue();
- if (!isBlank(value)) {
+ // populate the target property with the parameter value
+ if (StringUtils.isNotBlank(value)) {
try {
BeanUtils.setProperty(plugin, fieldName, value);
@@ -107,7 +111,7 @@ public void populate(T plugin, Map<String, String> parameters) {
}
/**
- * Returns the names of javabeans property methods annotated with the
+ * Returns the field name of javabeans properties annotated with the
* {@link Parameter} annotation.
*
* @param type plugin class with parameter annotations
@@ -116,32 +120,32 @@ public void populate(T plugin, Map<String, String> parameters) {
public Map<String, Parameter> getAnnotatedFields(Class<T> type) {
Map<String, Parameter> fields = new HashMap<String, Parameter>();
- // collect method annotations
+ // collect public member methods of the class, including those defined on the interface
+ // or those inherited from a super class or super interface.
for (Method method : type.getMethods()) {
- for (Annotation annotation : method.getDeclaredAnnotations()) {
- if (annotation instanceof Parameter) {
- if (method.getName().startsWith("get") || method.getName().startsWith("set")) {
- fields.put(getFieldName(method.getName()), (Parameter) annotation);
- }
+ Parameter annotation = method.getAnnotation(Parameter.class);
+ if (annotation != null) {
+ if (method.getName().startsWith("get") || method.getName().startsWith("set")) {
+ fields.put(ClassUtils.getFieldName(method.getName()), annotation);
}
}
}
- return fields;
- }
+ // collection all field annotations, including private fields that
+ // we can to access via a public accessor method
+ Class klass = type;
+ while (klass != null) {
+ for (Field field : klass.getDeclaredFields()) {
+ Parameter annotation = field.getAnnotation(Parameter.class);
+ if (annotation != null) {
+ fields.put(field.getName(), annotation);
+ }
+ }
- /**
- * Converts a method name to a javabeans field name.
- *
- * @param methodName method name
- * @return field name
- */
- public static String getFieldName(String methodName) {
- return methodName.substring(3, 4).toLowerCase() + methodName.substring(4);
- }
+ // try the super class
+ klass = klass.getSuperclass();
+ }
- // todo: use Apache commons lang StringUtils.isBlank() instead
- private boolean isBlank(String value) {
- return value == null || value.trim().equals("");
+ return fields;
}
}
View
2  billing-ng-core/src/main/java/com/billing/ng/plugin/annotation/Parameter.java
@@ -28,7 +28,7 @@
* @author Brian Cowdery
* @since 15/02/11
*/
-@Target({ ElementType.METHOD })
+@Target({ ElementType.FIELD, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface Parameter {
View
9 billing-ng-core/src/main/java/com/billing/ng/util/ClassUtils.java
@@ -53,4 +53,13 @@ public static Class forName(String className) throws ClassNotFoundException {
}
}
+ /**
+ * Converts a method name to a javabeans field name.
+ *
+ * @param methodName method name
+ * @return field name
+ */
+ public static String getFieldName(String methodName) {
+ return methodName.substring(3, 4).toLowerCase() + methodName.substring(4);
+ }
}
View
8 billing-ng-core/src/test/java/com/billing/ng/plugin/ParametersTest.java
@@ -20,6 +20,7 @@
import com.billing.ng.plugin.annotation.Parameter;
import com.billing.ng.plugin.test.TestPlugin;
import com.billing.ng.plugin.test.TestPluginImpl;
+import com.billing.ng.util.ClassUtils;
import org.hamcrest.Matchers;
import org.joda.time.DateMidnight;
import org.testng.annotations.Test;
@@ -135,11 +136,4 @@ public void testGetAnnotatedFields() throws Exception {
assertThat(fields.get("decimal").name(), is("decimal"));
assertThat(fields.get("decimal").defaultValue(), is("0.00"));
}
-
- @Test
- public void testGetFieldName() throws Exception {
- assertThat(Parameters.getFieldName("getSomeField"), is("someField"));
- assertThat(Parameters.getFieldName("setAnotherFieldName"), is("anotherFieldName"));
- assertThat(Parameters.getFieldName("getField"), is("field"));
- }
}
View
5 billing-ng-core/src/test/java/com/billing/ng/plugin/test/TestPluginImpl.java
@@ -35,6 +35,8 @@
@Plugin(name = "test plugin")
public class TestPluginImpl implements TestPlugin {
+ @NotNull
+ @Parameter(name = "string")
private String string;
private Integer number;
private BigDecimal decimal;
@@ -42,8 +44,7 @@
public TestPluginImpl() {
}
- @NotNull
- @Parameter(name = "string")
+ // annotation at field level
public String getString() {
return string;
}
View
38 billing-ng-core/src/test/java/com/billing/ng/util/ClassUtilsTest.java
@@ -0,0 +1,38 @@
+/*
+ BillingNG, a next-generation billing solution
+ Copyright (C) 2011 Brian Cowdery
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see http://www.gnu.org/licenses/agpl-3.0.html
+ */
+
+package com.billing.ng.util;
+
+import org.testng.annotations.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+
+/**
+ * @author Brian Cowdery
+ * @since 28-10-2011
+ */
+@Test(groups = { "quick" })
+public class ClassUtilsTest {
+
+ @Test
+ public void testGetFieldName() throws Exception {
+ assertThat(ClassUtils.getFieldName("getSomeField"), is("someField"));
+ assertThat(ClassUtils.getFieldName("setAnotherFieldName"), is("anotherFieldName"));
+ assertThat(ClassUtils.getFieldName("getField"), is("field"));
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.