KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > innig > macker > util > ClassNameTranslator


1 /*______________________________________________________________________________
2  *
3  * Macker http://innig.net/macker/
4  *
5  * Copyright 2002-2003 Paul Cantrell
6  *
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License version 2, as published by the
9  * Free Software Foundation. See the file LICENSE.html for more information.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the license for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
17  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
18  *______________________________________________________________________________
19  */

20  
21 package net.innig.macker.util;
22
23 import java.util.*;
24
25 import org.apache.regexp.RE;
26 import org.apache.regexp.RESyntaxException;
27
28 public class ClassNameTranslator
29     {
30     static public boolean isJavaIdentifier(String JavaDoc className)
31         { return legalJavaIdentRE.match(className); }
32     
33     static public List signatureToClassNames(String JavaDoc signature)
34         {
35         List names = new ArrayList();
36         for(int pos = 0; pos < signature.length(); )
37             {
38             String JavaDoc remaining = signature.substring(pos);
39             if(!sigExtractorRE.match(remaining))
40                 throw new IllegalArgumentException JavaDoc("Unable to extract type info from: " + remaining);
41             if(sigExtractorRE.getParen(2) != null)
42                 names.add((String JavaDoc) primitiveTypeMap.get(sigExtractorRE.getParen(2)));
43             if(sigExtractorRE.getParen(3) != null)
44                 names.add(resourceToClassName(sigExtractorRE.getParen(3)));
45             pos += sigExtractorRE.getParenEnd(0);
46             }
47         return names;
48         }
49     
50     static public String JavaDoc typeConstantToClassName(String JavaDoc typeName)
51         {
52         if(arrayExtractorRE.match(typeName))
53             {
54             if(arrayExtractorRE.getParen(2) != null)
55                 return (String JavaDoc) primitiveTypeMap.get(arrayExtractorRE.getParen(2));
56             if(arrayExtractorRE.getParen(3) != null)
57                 return resourceToClassName(arrayExtractorRE.getParen(3));
58             }
59         return resourceToClassName(typeName);
60         }
61     
62     static public String JavaDoc resourceToClassName(String JavaDoc className)
63         { return slashRE.subst(classSuffixRE.subst(className, ""), ".").intern(); }
64     
65     static public String JavaDoc classToResourceName(String JavaDoc resourceName)
66         { return (dotRE.subst(resourceName, "/") + ".class").intern(); }
67     
68     static private RE classSuffixRE, slashRE, dotRE, arrayExtractorRE, sigExtractorRE, legalJavaIdentRE;
69     static private Map/*<String,String>*/ primitiveTypeMap;
70     static
71         {
72         try {
73             classSuffixRE = new RE("\\.class$");
74             slashRE = new RE("/");
75             dotRE = new RE("\\.");
76             arrayExtractorRE = new RE( "^(\\[+([BSIJCFDZV])|\\[+L([^;]*);)$");
77             sigExtractorRE = new RE("^\\(?\\)?(\\[*([BSIJCFDZV])|\\[*L([^;]*);)");
78             legalJavaIdentRE = new RE("^([:javastart:][:javapart:]*)(\\.([:javastart:][:javapart:]*))*$");
79             }
80         catch(RESyntaxException rese)
81             { throw new RuntimeException JavaDoc("Can't initialize ClassNameTranslator: " + rese); }
82         
83         primitiveTypeMap = new HashMap();
84         primitiveTypeMap.put("B", "byte");
85         primitiveTypeMap.put("S", "short");
86         primitiveTypeMap.put("I", "int");
87         primitiveTypeMap.put("J", "long");
88         primitiveTypeMap.put("C", "char");
89         primitiveTypeMap.put("F", "float");
90         primitiveTypeMap.put("D", "double");
91         primitiveTypeMap.put("Z", "boolean");
92         primitiveTypeMap.put("V", "void");
93         }
94     }
95
Popular Tags