From portal-commits at lists.jboss.org Thu Jul 17 16:53:59 2008 Content-Type: multipart/mixed; boundary="===============5522183142516514083==" MIME-Version: 1.0 From: portal-commits at lists.jboss.org To: portal-commits at lists.jboss.org Subject: [portal-commits] JBoss Portal SVN: r11482 - in modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src: test/java/org/jboss/portal/test/common and 1 other directory. Date: Thu, 17 Jul 2008 16:53:58 -0400 Message-ID: --===============5522183142516514083== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Author: chris.laprun(a)jboss.com Date: 2008-07-17 16:53:57 -0400 (Thu, 17 Jul 2008) New Revision: 11482 Modified: modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/j= boss/portal/common/mx/JavaBeanModelMBeanBuilder.java modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/test/java/org/j= boss/portal/test/common/JavaBeanModelMBeanBuilderTestCase.java Log: - Fixed a bug where overriden setters and getters would be improperly detec= ted as duplicated methods. = - Added more tests. Modified: modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/jav= a/org/jboss/portal/common/mx/JavaBeanModelMBeanBuilder.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/= jboss/portal/common/mx/JavaBeanModelMBeanBuilder.java 2008-07-17 20:48:39 U= TC (rev 11481) +++ modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/= jboss/portal/common/mx/JavaBeanModelMBeanBuilder.java 2008-07-17 20:53:57 U= TC (rev 11482) @@ -22,6 +22,13 @@ *************************************************************************= *****/ package org.jboss.portal.common.mx; = +import javax.management.Descriptor; +import javax.management.modelmbean.ModelMBeanAttributeInfo; +import javax.management.modelmbean.ModelMBeanConstructorInfo; +import javax.management.modelmbean.ModelMBeanInfo; +import javax.management.modelmbean.ModelMBeanInfoSupport; +import javax.management.modelmbean.ModelMBeanNotificationInfo; +import javax.management.modelmbean.ModelMBeanOperationInfo; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; @@ -31,14 +38,6 @@ import java.util.Map; import java.util.Set; = -import javax.management.Descriptor; -import javax.management.modelmbean.ModelMBeanAttributeInfo; -import javax.management.modelmbean.ModelMBeanConstructorInfo; -import javax.management.modelmbean.ModelMBeanInfo; -import javax.management.modelmbean.ModelMBeanInfoSupport; -import javax.management.modelmbean.ModelMBeanNotificationInfo; -import javax.management.modelmbean.ModelMBeanOperationInfo; - = = /** * @author Julien Viet @@ -46,7 +45,7 @@ * @version $Revision: 7452 $ */ public class JavaBeanModelMBeanBuilder -{ = +{ private final static String CURRENCY_TIME_LIMIT =3D "currencyTimeLimit"; private final static String GET_METHOD =3D "getMethod"; private final static String SET_METHOD =3D "setMethod"; @@ -56,6 +55,9 @@ private ArrayList mmais; private ArrayList mmois; private String className; + private static final String GET =3D "get"; + private static final String IS =3D "is"; + private static final String SET =3D "set"; = public JavaBeanModelMBeanBuilder(Class from, Class to) throws Exception { @@ -87,119 +89,49 @@ // for (Class c =3D from;c !=3D null && !c.equals(to);c =3D c.getSuperc= lass()) { - Map currentClassGetters =3D new HashMap(); - Map currentClassSetters =3D new HashMap(); + Map currentClassGetters =3D new HashMap(); + Map currentClassSetters =3D new HashMap(); = Method[] methods =3D c.getDeclaredMethods(); - for (int i =3D 0; i < methods.length; i++) + for (Method method : methods) { - Method method =3D methods[i]; int modifiers =3D method.getModifiers(); if (Modifier.isPublic(modifiers) && - !Modifier.isAbstract(modifiers) && - !Modifier.isStatic(modifiers)) + !Modifier.isAbstract(modifiers) && + !Modifier.isStatic(modifiers)) { String methodName =3D method.getName(); Class returnType =3D method.getReturnType(); Class[] parameterTypes =3D method.getParameterTypes(); - if (methodName.startsWith("get") && - !void.class.equals(returnType) && - parameterTypes.length =3D=3D 0 && - methodName.length() > 3) - { - String propertyName =3D methodName.substring(3); = - // Try to locate an existing setter for the same property - Method beanSetter =3D (Method)currentClassSetters.get(pr= opertyName); - if (beanSetter =3D=3D null) - { - beanSetter =3D (Method)beanSetters.get(propertyName); - } - - // Check we do not have a setter with a different return= type - if (beanSetter !=3D null && !beanSetter.getParameterType= s()[0].equals(returnType)) - { - throw new IllegalArgumentException("Property " + prop= ertyName + " has a setter" + - " type " + beanSetter.getParameterTypes()[0] + " diff= erent from the corresponding" + - " getter type " + returnType); - } - - // Get an existing bean getter - Method beanGetter =3D (Method)currentClassGetters.get(pr= opertyName); - if (beanGetter !=3D null) - { - throw new IllegalArgumentException("Property " + prop= ertyName + " has two getters " + - beanGetter + " and " + method); - } - - // - currentClassGetters.put(propertyName, method); - } - else if (methodName.startsWith("is") && - !void.class.equals(returnType) && - parameterTypes.length =3D=3D 0 && - methodName.length() > 2) + int prefixLength =3D 0; + boolean isPotentialGetter =3D false; + if(methodName.startsWith(GET)) { - String propertyName =3D methodName.substring(2); - - // Try to locate an existing setter for the same property - Method beanSetter =3D (Method)beanSetters.get(propertyNa= me); - if (beanSetter !=3D null) - { - beanSetter =3D (Method)currentClassSetters.get(proper= tyName); - } - - // Check we do not have a setter with a different return= type - if (beanSetter !=3D null && !beanSetter.getParameterType= s()[0].equals(returnType)) - { - throw new IllegalArgumentException("Property " + prop= ertyName + " has a setter" + - " type " + beanSetter.getParameterTypes()[0] + " diff= erent from the corresponding" + - " getter type " + returnType); - } - - // Get an existing getter - Method beanGetter =3D (Method)beanGetters.get(propertyNa= me); - if (beanGetter !=3D null) - { - throw new IllegalArgumentException("Property " + prop= ertyName + " has two getters " + - beanGetter + " and " + method); - } - - // - currentClassGetters.put(propertyName, method); + prefixLength =3D 3; + isPotentialGetter =3D true; } - else if (methodName.startsWith("set") && - void.class.equals(returnType) && - parameterTypes.length =3D=3D 1 && - methodName.length() > 3) + else if(methodName.startsWith(IS)) { - String propertyName =3D methodName.substring(3); + prefixLength =3D 2; + isPotentialGetter =3D true; + } else if (methodName.startsWith(SET)) + { + prefixLength =3D 3; + } = - // Try to locate an existing getter - Method beanGetter =3D (Method)beanGetters.get(propertyNa= me); - if (beanGetter =3D=3D null) + if (methodName.length() > prefixLength) + { + if (isPotentialGetter && !void.class.equals(returnType) = && parameterTypes.length =3D=3D 0) { - beanGetter =3D (Method)currentClassGetters.get(proper= tyName); + processPropertyOperation(method, currentClassGetters,= currentClassSetters, beanSetters, + prefixLength, false); } - - // Check we do not have a getter with a different return= type - if (beanGetter !=3D null && !beanGetter.getReturnType().= equals(parameterTypes[0])) + else if (methodName.startsWith(SET) && void.class.equals= (returnType) && parameterTypes.length =3D=3D 1) { - throw new IllegalArgumentException("Property " + prop= ertyName + " has a setter" + - " type " + parameterTypes[0] + " different from the c= orresponding" + - " getter type " + beanGetter.getReturnType()); + processPropertyOperation(method, currentClassSetters,= currentClassGetters, beanGetters, + prefixLength, true); } - - // Get an existing setter - Method beanSetter =3D (Method)beanSetters.get(propertyNa= me); - if (beanSetter !=3D null) - { - throw new IllegalArgumentException("Property " + prop= ertyName + " cannot have two setters " + - beanSetter + " and " + method); - } - - // - currentClassSetters.put(propertyName, method); } = // @@ -283,6 +215,63 @@ } = /** + * Process a property operation either setter or getter, checking for c= onsistency. "Reverse" operation is defined here + * as a setter for a getter operation, and a getter for a setter operat= ion. + * Hence, if we are currently checking a getter, beanReverseOpera= tions will refer to known setters so far, + * currentClassOperations to the already known getter for = this class at this hierachical level, + * currentClassReverseOperations to the already known sett= er for this class at this hierachical level. + * + * @param operation + * @param currentClassOperations known property operations for the hier= archy level being currently examined + * @param currentClassReverseOperations known property "reverse" operat= ions for the hierarchy level being currently examined + * @param beanReverseOperations known "reverse" property operations for= this bean + * @param prefixLength + * @param isSetter + */ + private void processPropertyOperation(Method operation, Map currentClassOperations, + Map currentClassR= everseOperations, Map beanReverseOperations, + int prefixLength, boolean isSette= r) + { + String propertyName =3D operation.getName().substring(prefixLength); + + // Try to locate an existing setter for the same property + Method reverseOp =3D currentClassReverseOperations.get(propertyName); + if (reverseOp =3D=3D null) + { + reverseOp =3D (Method)beanReverseOperations.get(propertyName); + } + + // check that if we know a reverse operation, the types match + if (reverseOp !=3D null) + { + Class opType =3D isSetter ? operation.getParameterTypes()[0] : op= eration.getReturnType(); + Class reverseOpType =3D isSetter ? reverseOp.getReturnType() : re= verseOp.getParameterTypes()[0]; + if (!reverseOpType.equals(opType)) + { + throw new IllegalArgumentException("Property " + propertyName = + " has a " + getterOrSetter(isSetter) + + " type " + reverseOpType + " different from the corres= ponding " + getterOrSetter(!isSetter) + + " type " + opType); + } + } + + // Check that we don't have twice the same operation + Method op =3D currentClassOperations.get(propertyName); + if (op !=3D null) + { + throw new IllegalArgumentException("Property " + propertyName + "= has two " + getterOrSetter(isSetter) + "s " + + op + " and " + operation); + } + + // + currentClassOperations.put(propertyName, operation); + } + + private String getterOrSetter(boolean isSetter) + { + return isSetter ? "setter" : "getter"; + } + + /** * Remove an interface from the management interface. */ public void remove(Class itf) Modified: modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/test/jav= a/org/jboss/portal/test/common/JavaBeanModelMBeanBuilderTestCase.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/test/java/org/= jboss/portal/test/common/JavaBeanModelMBeanBuilderTestCase.java 2008-07-17 = 20:48:39 UTC (rev 11481) +++ modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/test/java/org/= jboss/portal/test/common/JavaBeanModelMBeanBuilderTestCase.java 2008-07-17 = 20:53:57 UTC (rev 11482) @@ -25,20 +25,20 @@ import junit.framework.TestCase; import org.jboss.portal.common.mx.JavaBeanModelMBeanBuilder; = +import javax.management.Attribute; import javax.management.AttributeNotFoundException; +import javax.management.Descriptor; import javax.management.MBeanAttributeInfo; import javax.management.MBeanOperationInfo; import javax.management.MBeanServer; import javax.management.MBeanServerFactory; import javax.management.ObjectName; -import javax.management.Attribute; -import javax.management.Descriptor; import javax.management.modelmbean.ModelMBeanInfo; +import javax.management.modelmbean.ModelMBeanOperationInfo; import javax.management.modelmbean.RequiredModelMBean; -import javax.management.modelmbean.ModelMBeanOperationInfo; +import java.util.Arrays; import java.util.HashSet; import java.util.Set; -import java.util.Arrays; = /** * @author Julien Viet @@ -131,10 +131,26 @@ Set ops =3D getOperations(info); Set expectedOps =3D new HashSet(); expectedOps.add(TestOperation.newGetter("getTest", "java.util.Set")); + expectedOps.add(TestOperation.newGetter("isSet", "boolean")); + expectedOps.add(TestOperation.newSetter("setSet", "boolean")); + expectedOps.add(TestOperation.newSetter("setFoo", "java.lang.String"= )); assertEquals(expectedOps, ops); = } = + public void testMismatchedPropertyOperations() + { + try + { + JavaBeanModelMBeanBuilder builder =3D new JavaBeanModelMBeanBuild= er(TestMismatchedSetterGetter.class, Object.class); + fail(); + } + catch (Exception expected) + { + // expected + } + } + public void testAttributesAreNotCached() throws Exception { AttributesAreNotCached aanc =3D new AttributesAreNotCached(); @@ -337,6 +353,21 @@ //nothing return new HashSet(); } + + public boolean isSet() + { + return false; + } + + public void setSet(boolean set) + { + // nothing + } + + public void setFoo(String foo) + { + // nothing + } } = public class TestOverridenExtend extends TestOverridenBase @@ -346,8 +377,31 @@ //nothing return new HashSet(); } + + public boolean isSet() + { + return true; + } + + public void setFoo(String foo) + { + // nothing + } } = + public class TestMismatchedSetterGetter + { + public void setFoo(String foo) + { + // nothing + } + + public boolean getFoo() + { + return false; + } + } + public static class TestAttribute { = --===============5522183142516514083==--