Author: tchemit Date: 2010-11-25 10:04:45 +0100 (Thu, 25 Nov 2010) New Revision: 999 Url: http://nuiton.org/repositories/revision/eugene/999 Log: Evolution #1096: Add method GeneratorUtil.removeGenericsDefinition Modified: trunk/eugene/src/main/java/org/nuiton/eugene/GeneratorUtil.java trunk/eugene/src/test/java/org/nuiton/eugene/GeneratorUtilTest.java Modified: trunk/eugene/src/main/java/org/nuiton/eugene/GeneratorUtil.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/GeneratorUtil.java 2010-11-23 16:49:52 UTC (rev 998) +++ trunk/eugene/src/main/java/org/nuiton/eugene/GeneratorUtil.java 2010-11-25 09:04:45 UTC (rev 999) @@ -557,13 +557,30 @@ /** * Suppress packageName from a fullQualifiedName, even if it contains List, Map or other generics. * See JUnit test for matching expressions. - * + * * @param str FullQualifiedName for an attribute type (for exemple) * @return the simple name associated to the str given */ public static String getSimpleName(String str) { + String result = getSimpleName(str, false); + return result; + } + + /** + * Suppress packageName from a fullQualifiedName, even if it contains List, Map or other generics. + * See JUnit test for matching expressions. + * + * @param str FullQualifiedName for an attribute type (for exemple) + * @param removeGenericDefinition a flag to remove any generic definition at the beginning of the expression + * @return the simple name associated to the str given + * @since 2.2.1 + */ + public static String getSimpleName(String str, boolean removeGenericDefinition) { + if (removeGenericDefinition) { + str = removeGenericDefinition(str); + } // variable array type - boolean variableArrayType=false; + boolean variableArrayType = false; if (str.endsWith("...")) { variableArrayType = true; str = str.substring(0, str.length() - 3); @@ -580,6 +597,53 @@ } /** + * Remove any generics definition at the beginning of a string. + * <p/> + * For example : + * <pre> + * <T> T -> T + * </pre> + * + * @param str the string to parse + * @return the string without any + * @since 2.2.1 + */ + public static String removeGenericDefinition(String str) { + + // always trim the string + String result = str.trim(); + + if (!result.startsWith("<")) { + + // not starting by a generics definition, no treatment to do + return str; + } + + int i = 0; + for (int length = result.length(), count = 0; i < length; i++) { + char c = result.charAt(i); + if ('<' == c) { + count++; + } else if ('>' == c) { + count--; + } + if (count == 0) { + break; + } + } + + // the i position was on the last closing caracter, can safely + // remove until this position + 1 + result = result.substring(i + 1); + + // remove any starting spaces + while (result.startsWith(" ")) { + result = result.substring(1); + } + return result; + } + + /** * Parse a fully qualified generic java type, and extract each * imbricated types. * Modified: trunk/eugene/src/test/java/org/nuiton/eugene/GeneratorUtilTest.java =================================================================== --- trunk/eugene/src/test/java/org/nuiton/eugene/GeneratorUtilTest.java 2010-11-23 16:49:52 UTC (rev 998) +++ trunk/eugene/src/test/java/org/nuiton/eugene/GeneratorUtilTest.java 2010-11-25 09:04:45 UTC (rev 999) @@ -26,18 +26,16 @@ package org.nuiton.eugene; -import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.nuiton.eugene.models.object.xml.ObjectModelOperationImpl; import org.nuiton.eugene.models.object.xml.ObjectModelParameterImpl; -import static org.junit.Assert.*; +import java.util.Set; + +import static org.junit.Assert.assertEquals; + /** * * @author fdesbois @@ -49,22 +47,6 @@ public GeneratorUtilTest() { } - @BeforeClass - public static void setUpClass() throws Exception { - } - - @AfterClass - public static void tearDownClass() throws Exception { - } - - @Before - public void setUp() { - } - - @After - public void tearDown() { - } - @Test public void testGetOperationParametersListName() { log.info("getOperationParametersListName"); @@ -121,6 +103,7 @@ assertEquals("String param1, Date param2", result); } + /** * Test of getSimpleName method, of class GeneratorUtil. */ @@ -140,6 +123,12 @@ log.info(str + " -> " + result); assertEquals(expResult, result); + str = "<T> java.util.List<T>"; + expResult = "<T> List<T>"; + result = GeneratorUtil.getSimpleName(str); + log.info(str + " -> " + result); + assertEquals(expResult, result); + str = "java.util.List<? extends org.chorem.bonzoms.Bonzoms>"; expResult = "List<? extends Bonzoms>"; result = GeneratorUtil.getSimpleName(str); @@ -214,7 +203,196 @@ assertEquals(expResult, result); } + /** + * Test of getSimpleName method with removing generics definition, + * of class GeneratorUtil. + */ @Test + public void testGetSimpleNameAndRemoveGenericsDefinition() { + log.info("getSimpleName"); + + String str = "List"; + String expResult = "List"; + String result = GeneratorUtil.getSimpleName(str, true); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "java.util.Regex"; + expResult = "Regex"; + result = GeneratorUtil.getSimpleName(str, true); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "<T> java.util.List<T>"; + expResult = "List<T>"; + result = GeneratorUtil.getSimpleName(str, true); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "java.util.List<? extends org.chorem.bonzoms.Bonzoms>"; + expResult = "List<? extends Bonzoms>"; + result = GeneratorUtil.getSimpleName(str, true); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "java.util.List<org.chorem.jtimer.Jtimer>"; + expResult = "List<Jtimer>"; + result = GeneratorUtil.getSimpleName(str, true); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "java.util.Set<java.util.Collection<java.util.Collection<java.util.Collection" + + "<java.lang.String>>>>"; + expResult = "Set<Collection<Collection<Collection<String>>>>"; + result = GeneratorUtil.getSimpleName(str, true); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "java.util.Map<org.chorem.jtimer.Jtimer, java.util.Collection<java.lang.String>>"; + expResult = "Map<Jtimer, Collection<String>>"; + result = GeneratorUtil.getSimpleName(str, true); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "new java.util.HashMap<org.chorem.jtimer.Jtimer, T extends java.lang.String>()"; + expResult = "new HashMap<Jtimer, T extends String>()"; + result = GeneratorUtil.getSimpleName(str, true); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "Class<O>"; + expResult = "Class<O>"; + result = GeneratorUtil.getSimpleName(str, true); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "2.0"; + expResult = "2.0"; + result = GeneratorUtil.getSimpleName(str, true); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "\"eric.chatellier\""; + expResult = "\"eric.chatellier\""; + result = GeneratorUtil.getSimpleName(str, true); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "<T extends org.nuiton.topia.TopiaEntity, D extends org.nuiton.topia.TopiaDAO<? super T>> D"; + expResult = "D"; + result = GeneratorUtil.getSimpleName(str, true); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "Object..."; + expResult = "Object..."; + result = GeneratorUtil.getSimpleName(str, true); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "Class<A>..."; + expResult = "Class<A>..."; + result = GeneratorUtil.getSimpleName(str, true); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "java.lang.Class<A>..."; + expResult = "Class<A>..."; + result = GeneratorUtil.getSimpleName(str, true); + log.info(str + " -> " + result); + assertEquals(expResult, result); + } + + + /** + * Test of removeGenericDefinition method, of class GeneratorUtil. + */ + @Test + public void testRemoveGenericDefinition() { + log.info("removeGenericDefinition"); + + String str = "List"; + String expResult = "List"; + String result = GeneratorUtil.removeGenericDefinition(str); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "java.util.Regex"; + expResult = "java.util.Regex"; + result = GeneratorUtil.removeGenericDefinition(str); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "<T> java.util.List<T>"; + expResult = "java.util.List<T>"; + result = GeneratorUtil.removeGenericDefinition(str); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + + str = " <T> java.util.List<T> "; + expResult = "java.util.List<T>"; + result = GeneratorUtil.removeGenericDefinition(str); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + + str = "java.util.List<? extends org.chorem.bonzoms.Bonzoms>"; + expResult = "java.util.List<? extends org.chorem.bonzoms.Bonzoms>"; + result = GeneratorUtil.removeGenericDefinition(str); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "java.util.List<org.chorem.jtimer.Jtimer>"; + expResult = "java.util.List<org.chorem.jtimer.Jtimer>"; + result = GeneratorUtil.removeGenericDefinition(str); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "Class<O>"; + expResult = "Class<O>"; + result = GeneratorUtil.removeGenericDefinition(str); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "2.0"; + expResult = "2.0"; + result = GeneratorUtil.removeGenericDefinition(str); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "\"eric.chatellier\""; + expResult = "\"eric.chatellier\""; + result = GeneratorUtil.removeGenericDefinition(str); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "<T extends org.nuiton.topia.TopiaEntity, D extends org.nuiton.topia.TopiaDAO<? super T>> D"; + expResult = "D"; + result = GeneratorUtil.removeGenericDefinition(str); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "Object..."; + expResult = "Object..."; + result = GeneratorUtil.removeGenericDefinition(str); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "Class<A>..."; + expResult = "Class<A>..."; + result = GeneratorUtil.removeGenericDefinition(str); + log.info(str + " -> " + result); + assertEquals(expResult, result); + + str = "java.lang.Class<A>..."; + expResult = "java.lang.Class<A>..."; + result = GeneratorUtil.removeGenericDefinition(str); + log.info(str + " -> " + result); + assertEquals(expResult, result); + } + + @Test public void testGetTypesList() { String str = "List"; Set<String> results = GeneratorUtil.getTypesList(str);
participants (1)
-
tchemit@users.nuiton.org