]> source.dussan.org Git - aspectj.git/commitdiff
141730 - latest variant of program element signature processing
authoraclement <aclement>
Wed, 7 Jun 2006 15:52:35 +0000 (15:52 +0000)
committeraclement <aclement>
Wed, 7 Jun 2006 15:52:35 +0000 (15:52 +0000)
12 files changed:
asm/src/org/aspectj/asm/AsmManager.java
asm/src/org/aspectj/asm/IHierarchy.java
asm/src/org/aspectj/asm/INameConvertor.java [deleted file]
asm/src/org/aspectj/asm/internal/AspectJElementHierarchy.java
asm/src/org/aspectj/asm/internal/ProgramElement.java
org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/core/builder/AjBuildManager.java
org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/core/builder/IncrementalStateManager.java
tests/src/org/aspectj/systemtest/ajc152/Ajc152Tests.java
util/src/org/aspectj/util/CharOperation.java
util/src/org/aspectj/util/NameConvertor.java [new file with mode: 0644]
util/testsrc/org/aspectj/util/NameConvertorTest.java [new file with mode: 0644]
util/testsrc/org/aspectj/util/UtilTests.java

index f7d0c677eb74ddb937972b57d3b6fa74e07ee8e6..e9b8c4bc163de23615f34bb707c72b5ac8ed4815 100644 (file)
@@ -77,9 +77,8 @@ public class AsmManager {
         handleProvider = new OptimizedFullPathHandleProvider(); 
     }
        
-       public void createNewASM(INameConvertor convertor) {
+       public void createNewASM() {
                hierarchy = new AspectJElementHierarchy();
-               hierarchy.setNameConvertor(convertor);
                mapper = new RelationshipMap(hierarchy);
        }
        
index 82428481b8f291892f4118ce1861b747eb4882fc..d458da3389582f7f972636c7e7c1d792d53d0551 100644 (file)
@@ -102,7 +102,4 @@ public interface IHierarchy extends Serializable {
        public void flushHandleMap();
 
        public void updateHandleMap(Set deletedFiles);
-       
-       public void setNameConvertor(INameConvertor convertor);
-       public INameConvertor getNameConvertor();
 }
\ No newline at end of file
diff --git a/asm/src/org/aspectj/asm/INameConvertor.java b/asm/src/org/aspectj/asm/INameConvertor.java
deleted file mode 100644 (file)
index 58197bb..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-/********************************************************************
- * Copyright (c) 2006 Contributors. All rights reserved. 
- * This program and the accompanying materials are made available 
- * under the terms of the Eclipse Public License v1.0 
- * which accompanies this distribution and is available at 
- * http://eclipse.org/legal/epl-v10.html 
- *  
- * Contributors: IBM Corporation - initial API and implementation 
- *                              Helen Hawkins   - initial version
- *******************************************************************/
-package org.aspectj.asm;
-
-public interface INameConvertor {
-
-       public char[] convertName(char[] name);
-       
-}
index 62a1a31a8b23b067d9a18a49fe2e8f2854a58a62..42c60a203a123154fc958eaf2a0922b853506c01 100644 (file)
@@ -29,7 +29,6 @@ public class AspectJElementHierarchy implements IHierarchy {
        
        protected  IProgramElement root = null;
     protected String configFile = null;
-    private transient INameConvertor convertor = null;
 
     private Map fileMap = null;
     private Map handleMap = null;
@@ -450,13 +449,5 @@ public class AspectJElementHierarchy implements IHierarchy {
                return hid.substring(0,hid.indexOf("|"));
        }
 
-       public void setNameConvertor(INameConvertor convertor) {
-               this.convertor = convertor;
-       }
-
-       public INameConvertor getNameConvertor() {
-               return convertor;
-       }
-
 }
 
index 0e8826fc16d310f4cf4cb4d0372a3dddea4ec7cb..acddddfb2e951bbbd9881ae49ff6e4626e9a1dda 100644 (file)
@@ -22,11 +22,11 @@ import java.util.Map;
 
 import org.aspectj.asm.AsmManager;
 import org.aspectj.asm.HierarchyWalker;
-import org.aspectj.asm.INameConvertor;
 import org.aspectj.asm.IProgramElement;
 import org.aspectj.bridge.IMessage;
 import org.aspectj.bridge.ISourceLocation;
 import org.aspectj.util.CharOperation;
+import org.aspectj.util.NameConvertor;
 
 
 /**
@@ -519,12 +519,7 @@ public class ProgramElement implements IProgramElement {
                List params = new ArrayList();
                for (Iterator iter = l.iterator(); iter.hasNext();) {
                        char[] param = (char[])iter.next();
-                       INameConvertor convertor = AsmManager.getDefault().getHierarchy().getNameConvertor();
-                       if (convertor != null) {
-                               params.add(convertor.convertName(param));                               
-                       } else {
-                               params.add(param);
-                       }
+                       params.add(NameConvertor.convertFromSignature(param));
                }
                return params;
        }
index ac3f04b53cdebd9ef2dcc1d0f3a0887ea3941556..d2b09def22fdd5a8c772ca53430d874141425a1d 100644 (file)
@@ -49,7 +49,6 @@ import org.aspectj.ajdt.internal.compiler.lookup.EclipseFactory;
 import org.aspectj.ajdt.internal.compiler.problem.AjProblemReporter;
 import org.aspectj.asm.AsmManager;
 import org.aspectj.asm.IHierarchy;
-import org.aspectj.asm.INameConvertor;
 import org.aspectj.asm.IProgramElement;
 import org.aspectj.asm.internal.ProgramElement;
 import org.aspectj.bridge.AbortException;
@@ -84,8 +83,6 @@ import org.aspectj.org.eclipse.jdt.internal.compiler.problem.DefaultProblemFacto
 import org.aspectj.util.FileUtil;
 import org.aspectj.weaver.Dump;
 import org.aspectj.weaver.ResolvedType;
-import org.aspectj.weaver.TypeFactory;
-import org.aspectj.weaver.UnresolvedType;
 import org.aspectj.weaver.World;
 import org.aspectj.weaver.bcel.BcelWeaver;
 import org.aspectj.weaver.bcel.BcelWorld;
@@ -634,7 +631,7 @@ public class AjBuildManager implements IOutputClassFileNameProvider,IBinarySourc
        AsmManager.setCreatingModel(config.isEmacsSymMode() || config.isGenerateModelMode());
        if (!AsmManager.isCreatingModel()) return;
 
-               AsmManager.getDefault().createNewASM(new NameConverter());
+               AsmManager.getDefault().createNewASM();
                // AsmManager.getDefault().getRelationshipMap().clear();
                IHierarchy model = AsmManager.getDefault().getHierarchy();
         String rootLabel = "<root>";
@@ -1279,15 +1276,5 @@ public class AjBuildManager implements IOutputClassFileNameProvider,IBinarySourc
                }
                
        }
-       
-       private class NameConverter implements INameConvertor {
-
-               public char[] convertName(char[] name) {
-                       UnresolvedType ut = TypeFactory.createTypeFromSignature(new String(name));
-                       ResolvedType rt = getWorld().resolve(ut);
-                       return rt.getName().toCharArray();
-               }
-               
-       }
 }
 
index 4522a8b33187bfd20613003930ed2da755952adf..b9f2c966243b8aa11046430fd0fa1bef8a8b3078 100644 (file)
@@ -52,7 +52,7 @@ public class IncrementalStateManager {
                        element.wipeAllKnowledge();
                }
                incrementalStates.clear();
-               AsmManager.getDefault().createNewASM(null); // forget what you know...
+               AsmManager.getDefault().createNewASM(); // forget what you know...
        }
        
        public static Set getConfigFilesKnown() {
index bf333308eee9864a336e738e4e06f7861b580344..89962aa34de96ad7db17062df105e4a471068844 100644 (file)
@@ -21,6 +21,7 @@ import org.aspectj.asm.IHierarchy;
 import org.aspectj.asm.IProgramElement;
 import org.aspectj.asm.internal.Relationship;
 import org.aspectj.testing.XMLBasedAjcTestCase;
+import org.aspectj.util.CharOperation;
 
 public class Ajc152Tests extends org.aspectj.testing.XMLBasedAjcTestCase {
 
@@ -150,7 +151,7 @@ public class Ajc152Tests extends org.aspectj.testing.XMLBasedAjcTestCase {
       assertNotNull("Couldn't find 'twoArgsMethod(int,java.lang.String)' element in the tree",twoArgsMethod);
       List l = twoArgsMethod.getParameterSignatures();
       assertEquals("",((char[])l.get(0))[0],'I');
-      boolean eq = equals(((char[])l.get(1)),"Ljava/lang/String;".toCharArray());
+      boolean eq = CharOperation.equals(((char[])l.get(1)),"Ljava/lang/String;".toCharArray());
       assertTrue("expected parameter to be 'Ljava/lang/String;' but found '" +
                        new String(((char[])l.get(1))) + "'",eq);
   }
@@ -204,23 +205,6 @@ public class Ajc152Tests extends org.aspectj.testing.XMLBasedAjcTestCase {
 
   // ---------------- helper methods ---------------
   
-   /**
-    * taken from CharOperation
-    */
-       private final boolean equals(char[] first, char[] second) {
-               if (first == second)
-                       return true;
-               if (first == null || second == null)
-                       return false;
-               if (first.length != second.length)
-                       return false;
-
-               for (int i = first.length; --i >= 0;)
-                       if (first[i] != second[i])
-                               return false;
-               return true;
-       }
-  
        private void checkParametersForIPE(String ipeLabel, IProgramElement.Kind kind, String expectedParm, boolean getHandles) {
                IHierarchy top = AsmManager.getDefault().getHierarchy();
                IProgramElement ipe = top.findElementForLabel(top.getRoot(),kind,ipeLabel);
@@ -231,7 +215,7 @@ public class Ajc152Tests extends org.aspectj.testing.XMLBasedAjcTestCase {
            } else {
                l = ipe.getParameterTypes();
            }
-           boolean eq = equals(((char[])l.get(0)),expectedParm.toCharArray());
+           boolean eq = CharOperation.equals(((char[])l.get(0)),expectedParm.toCharArray());
            assertTrue("expected parameter to be '" + expectedParm + "' but found '" +
                        new String(((char[])l.get(0))) + "'",eq);
        }
index 423d7258d12ef9c880484c1b10d15390e4b36d4b..f9dd0da9e1414997ecf53aada91b5fd58f71e493 100644 (file)
@@ -45,4 +45,62 @@ public class CharOperation {
                return -1;
        }
        
+       /**
+        * Taken from org.aspectj.org.eclipse.jdt.core.compiler.CharOperation
+        */     
+       public static final int indexOf(char toBeFound, char[] array) {
+               for (int i = 0; i < array.length; i++)
+                       if (toBeFound == array[i])
+                               return i;
+               return -1;
+       }
+       
+       /**
+        * Taken from org.aspectj.org.eclipse.jdt.core.compiler.CharOperation
+        */     
+       public static final char[] concat(char[] first, char[] second) {
+               if (first == null)
+                       return second;
+               if (second == null)
+                       return first;
+
+               int length1 = first.length;
+               int length2 = second.length;
+               char[] result = new char[length1 + length2];
+               System.arraycopy(first, 0, result, 0, length1);
+               System.arraycopy(second, 0, result, length1, length2);
+               return result;
+       }
+       
+       /**
+        * Taken from org.aspectj.org.eclipse.jdt.core.compiler.CharOperation
+        */     
+       public static final boolean equals(char[] first, char[] second) {
+               if (first == second)
+                       return true;
+               if (first == null || second == null)
+                       return false;
+               if (first.length != second.length)
+                       return false;
+
+               for (int i = first.length; --i >= 0;)
+                       if (first[i] != second[i])
+                               return false;
+               return true;
+       }
+       
+       /**
+        * Taken from org.aspectj.org.eclipse.jdt.core.compiler.CharOperation
+        */     
+       public static final void replace(
+               char[] array,
+               char toBeReplaced,
+               char replacementChar) {
+               if (toBeReplaced != replacementChar) {
+                       for (int i = 0, max = array.length; i < max; i++) {
+                               if (array[i] == toBeReplaced)
+                                       array[i] = replacementChar;
+                       }
+               }
+       }
 }
diff --git a/util/src/org/aspectj/util/NameConvertor.java b/util/src/org/aspectj/util/NameConvertor.java
new file mode 100644 (file)
index 0000000..df12925
--- /dev/null
@@ -0,0 +1,139 @@
+/********************************************************************
+ * Copyright (c) 2006 Contributors. All rights reserved. 
+ * This program and the accompanying materials are made available 
+ * under the terms of the Eclipse Public License v1.0 
+ * which accompanies this distribution and is available at 
+ * http://eclipse.org/legal/epl-v10.html 
+ *  
+ * Contributors: IBM Corporation - initial API and implementation 
+ *                              Helen Hawkins   - initial version
+ *******************************************************************/
+package org.aspectj.util;
+
+public class NameConvertor {
+
+       private static final char BOOLEAN       = 'Z';
+       private static final char BYTE          = 'B';
+       private static final char CHAR          = 'C';
+       private static final char DOUBLE        = 'D';
+       private static final char FLOAT         = 'F';
+       private static final char INT           = 'I';
+       private static final char LONG          = 'J';
+       private static final char SHORT         = 'S';
+       private static final char ARRAY         = '[';
+       
+       private static final char[] BOOLEAN_NAME = new char[]{'b','o','o','l','e','a','n'};
+       private static final char[] BYTE_NAME = new char[]{'b','y','t','e'};
+       private static final char[] CHAR_NAME = new char[]{'c','h','a','r'};
+       private static final char[] DOUBLE_NAME = new char[]{'d','o','u','b','l','e'};
+       private static final char[] FLOAT_NAME = new char[]{'f','l','o','a','t'};
+       private static final char[] INT_NAME = new char[]{'i','n','t'};
+       private static final char[] LONG_NAME = new char[]{'l','o','n','g'};
+       private static final char[] SHORT_NAME = new char[]{'s','h','o','r','t'};
+
+       private static final char[] SQUARE_BRACKETS = new char[]{'[',']'};
+       private static final char[] GREATER_THAN = new char[]{'>'};
+       private static final char[] LESS_THAN = new char[]{'<'};
+       private static final char[] COMMA = new char[]{','};
+       
+       
+       /**
+        * Creates a readable name from the given char array, for example, 
+        * given 'I' returns 'int'. Moreover, given 
+        * 'Ljava/lang/String;<Ljava/lang/String;>' returns
+        * 'java.lang.String<java.lang.String>'
+        */
+       public static char[] convertFromSignature(char[] c) {
+               int lt = CharOperation.indexOf('<',c);
+               int sc = CharOperation.indexOf(';',c);
+               int gt = CharOperation.indexOf('>',c);
+               
+               int smallest = 0;
+               if (lt  == -1 && sc == -1 && gt == -1) {
+                       // we have something like 'Ljava/lang/String' or 'I'
+                       return getFullyQualifiedTypeName(c);
+               } else if (lt != -1 && (sc == -1 || lt <= sc) && (gt == -1 || lt <= gt)) {
+                       // we have something like 'Ljava/lang/String<I'
+                       smallest = lt;
+               } else if (sc != -1 && (lt == -1 || sc <= lt) && (gt == -1 || sc <= gt)) {
+                       // we have something like 'Ljava/lang/String;I'
+                       smallest = sc;
+               } else {
+                       // we have something like '>;'
+                       smallest = gt;
+               }
+               char[] first = CharOperation.subarray(c,0,smallest);
+               char[] second = CharOperation.subarray(c,smallest+1,c.length);
+               if (smallest == 0 && first.length == 0 && c[0] == '>') {
+                       // c = {'>',';'} therefore we just want to return '>' to
+                       // close the generic signature
+                       return GREATER_THAN;
+               } else if (first.length == 1 && second.length == 0) {
+                       return first;
+               } else if (second.length == 0 || (second.length == 1 && second[0] == ';')){
+                       // we've reached the end of the array, therefore only care about
+                       // the first part
+                       return convertFromSignature(first);
+               } else if (smallest == lt) {
+                       // if c = 'Ljava/lang/String;<I' then first = 'Ljava/Lang/String;' and
+                       // second = 'I'. Want to end up with 'Ljava.lang.String<I' and so add
+                       // the '<' back.
+                       char[] inclLT = CharOperation.concat(convertFromSignature(first),LESS_THAN);
+                       return CharOperation.concat(inclLT,convertFromSignature(second));
+               } else if (smallest == gt) {
+                       char[] inclLT = CharOperation.concat(convertFromSignature(first),GREATER_THAN);
+                       return CharOperation.concat(inclLT,convertFromSignature(second));                       
+               } else if (second.length != 2) {
+                       // if c = 'Ljava/lang/Sting;LMyClass' then first = 'Ljava/lang/String'
+                       // and second = 'LMyClass'. Want to end up with 'java.lang.String,MyClass
+                       // so want to add a ','. However, only want to do this if we're in the 
+                       // middle of a '<...>'
+                       char[] inclComma = CharOperation.concat(convertFromSignature(first),COMMA);
+                       return CharOperation.concat(inclComma,convertFromSignature(second));
+               }
+               return CharOperation.concat(convertFromSignature(first),convertFromSignature(second));
+       }
+       
+       
+       /**
+        * Given a char array, returns the type name for this. For example
+        * 'I' returns 'int', 'Ljava/lang/String' returns 'java.lang.String' and
+        * '[Ljava/lang/String' returns 'java.lang.String[]'
+        * 
+        * NOTE: Doesn't go any deaper so given 'Ljava/lang/String;<Ljava/lang/String;>' 
+        * it would return 'java.lang.String;<Ljava.lang.String;>', however, only called
+        * with something like 'Ljava/lang/String'
+        */
+       private static char[] getFullyQualifiedTypeName(char[] c) {
+               if (c.length == 0) {
+                       return c;
+               } 
+               if (c[0] == BOOLEAN) {
+                       return BOOLEAN_NAME;
+               } else if (c[0] == BYTE) {
+                       return BYTE_NAME;
+               } else if (c[0] == CHAR) {
+                       return CHAR_NAME;
+               } else if (c[0] == DOUBLE) {
+                       return DOUBLE_NAME;
+               } else if (c[0] == FLOAT) {
+                       return FLOAT_NAME;
+               } else if (c[0] == INT) {
+                       return INT_NAME;
+               } else if (c[0] == LONG) {
+                       return LONG_NAME;
+               } else if (c[0] == SHORT) {
+                       return SHORT_NAME;
+               } else if (c[0] == ARRAY) {
+                       return CharOperation.concat(
+                                       getFullyQualifiedTypeName(CharOperation.subarray(c,1,c.length)),
+                                       SQUARE_BRACKETS);
+               } else {
+                       char[] type = CharOperation.subarray(c,1,c.length);
+                       CharOperation.replace(type,'/','.');
+                       return type;
+               }
+       }
+       
+
+}
diff --git a/util/testsrc/org/aspectj/util/NameConvertorTest.java b/util/testsrc/org/aspectj/util/NameConvertorTest.java
new file mode 100644 (file)
index 0000000..e33bf81
--- /dev/null
@@ -0,0 +1,83 @@
+/********************************************************************
+ * Copyright (c) 2006 Contributors. All rights reserved. 
+ * This program and the accompanying materials are made available 
+ * under the terms of the Eclipse Public License v1.0 
+ * which accompanies this distribution and is available at 
+ * http://eclipse.org/legal/epl-v10.html 
+ *  
+ * Contributors: IBM Corporation - initial API and implementation 
+ *                              Helen Hawkins   - initial version
+ *******************************************************************/
+package org.aspectj.util;
+
+import junit.framework.TestCase;
+
+public class NameConvertorTest extends TestCase {
+
+       public void testBoolean() {
+               checkConversion("Z","boolean");
+       }
+       
+       public void testByte() {
+               checkConversion("B","byte");
+       }
+       
+       public void testChar() {
+               checkConversion("C","char");
+       }       
+       
+       public void testDouble() {
+               checkConversion("D","double");
+       }       
+       
+       public void testFloat() {
+               checkConversion("F","float");
+       }
+       
+       public void testInt() {
+               checkConversion("I","int");
+       }
+       
+       public void testLong() {
+               checkConversion("J","long");
+       }       
+       
+       public void testShort() {
+               checkConversion("S","short");
+       }       
+       
+       public void testString() {
+               checkConversion("Ljava/lang/String;","java.lang.String");
+       }
+
+       public void testType() {
+               checkConversion("LMyClass;","MyClass");
+       }
+
+       public void testListPameterizedWithString() {
+               checkConversion("Pjava/util/List<Ljava/lang/String;>;",
+                               "java.util.List<java.lang.String>");
+       }
+       public void testClassParameterizedWithStringAndType() {
+               checkConversion("PMyGenericClass<Ljava/lang/String;LMyClass;>;",
+                               "MyGenericClass<java.lang.String,MyClass>");
+       }
+       public void testStringArray() {
+               checkConversion("[Ljava/lang/String;","java.lang.String[]");
+       }
+       public void testTwoDimensionalStringArray() {
+               checkConversion("[[Ljava/lang/String;","java.lang.String[][]");
+       }
+       public void testIntArray() {
+               checkConversion("[I","int[]");
+       }
+
+       private void checkConversion(String signature, String expected) {
+               char[] c = NameConvertor.convertFromSignature(signature.toCharArray());
+               assertTrue("converting " + signature + ", expected " + expected + "," +
+                               "but found " + String.valueOf(c),
+                               CharOperation.equals(c,expected.toCharArray()));
+       }
+
+       
+}
index dfc80b220400cacd3952a86cbd7b9a5051557eed..8d2f183c2121e06e542c31830a7f6542b2898b92 100644 (file)
@@ -23,6 +23,7 @@ public class UtilTests extends TestCase {
         //$JUnit-BEGIN$
         suite.addTestSuite(FileUtilTest.class); 
         suite.addTestSuite(LangUtilTest.class); 
+        suite.addTestSuite(NameConvertorTest.class);
         //$JUnit-END$
         return suite;
     }