Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

Util.java

00001 package edu.ksu.cis.bandera.jjjc.util;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 1999, 2000   Robby (robby@cis.ksu.edu)              *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project in the SAnToS Laboratory,         *
00009  * Department of Computing and Information Sciences, Kansas State    *
00010  * University, USA (http://www.cis.ksu.edu/santos).                  *
00011  * It is understood that any modification not identified as such is  *
00012  * not covered by the preceding statement.                           *
00013  *                                                                   *
00014  * This work is free software; you can redistribute it and/or        *
00015  * modify it under the terms of the GNU Library General Public       *
00016  * License as published by the Free Software Foundation; either      *
00017  * version 2 of the License, or (at your option) any later version.  *
00018  *                                                                   *
00019  * This work is distributed in the hope that it will be useful,      *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00022  * Library General Public License for more details.                  *
00023  *                                                                   *
00024  * You should have received a copy of the GNU Library General Public *
00025  * License along with this toolkit; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * Java is a trademark of Sun Microsystems, Inc.                     *
00030  *                                                                   *
00031  * To submit a bug report, send a comment, or get the latest news on *
00032  * this project and other SAnToS projects, please visit the web-site *
00033  *                http://www.cis.ksu.edu/santos                      *
00034  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00035 import java.io.*;
00036 import java.util.*;
00037 import edu.ksu.cis.bandera.jjjc.exception.*;
00038 
00039 public final class Util {
00040 /**
00041  * 
00042  * @return java.lang.String
00043  * @param strs java.lang.String[]
00044  * @param s java.lang.String
00045  */
00046 public static String combineStrings(String[] strs, String s) {
00047     StringBuffer result = new StringBuffer();
00048     int length = strs.length;
00049 
00050     for (int i = 0; i < (length - 1); i++) {
00051         result.append(strs[i]);
00052         result.append(s);   
00053     }
00054     
00055     if (length > 0)
00056         result.append(strs[length - 1]);
00057     
00058     return result.toString();
00059 }
00060 /**
00061  * 
00062  * @return int
00063  * @param modifiers int
00064  */
00065 public static int convertModifiers(int modifiers) {
00066     int result = 0;
00067     
00068     if (java.lang.reflect.Modifier.isAbstract(modifiers))
00069         result |= ca.mcgill.sable.soot.Modifier.ABSTRACT;
00070     if (java.lang.reflect.Modifier.isFinal(modifiers))
00071         result |= ca.mcgill.sable.soot.Modifier.FINAL;
00072     if (java.lang.reflect.Modifier.isNative(modifiers))
00073         result |= ca.mcgill.sable.soot.Modifier.NATIVE;
00074     if (java.lang.reflect.Modifier.isPrivate(modifiers))
00075         result |= ca.mcgill.sable.soot.Modifier.PRIVATE;
00076     if (java.lang.reflect.Modifier.isProtected(modifiers))
00077         result |= ca.mcgill.sable.soot.Modifier.PROTECTED;
00078     if (java.lang.reflect.Modifier.isPublic(modifiers))
00079         result |= ca.mcgill.sable.soot.Modifier.PUBLIC;
00080     if (java.lang.reflect.Modifier.isStatic(modifiers))
00081         result |= ca.mcgill.sable.soot.Modifier.STATIC;
00082     if (java.lang.reflect.Modifier.isSynchronized(modifiers))
00083         result |= ca.mcgill.sable.soot.Modifier.SYNCHRONIZED;
00084     if (java.lang.reflect.Modifier.isTransient(modifiers))
00085         result |= ca.mcgill.sable.soot.Modifier.TRANSIENT;
00086     if (java.lang.reflect.Modifier.isVolatile(modifiers))
00087         result |= ca.mcgill.sable.soot.Modifier.VOLATILE;
00088         
00089     return result;
00090 }
00091 /**
00092  * 
00093  * @return int
00094  * @param modifier java.lang.String
00095  */
00096 public static int convertModifiers(String modifiers) throws InvalidModifiersException {
00097     String[] modifier = splitString(modifiers, " \t\r\n{},");
00098     int result = 0;
00099     for (int i = 0; i < modifier.length; i++) {
00100         if ("abstract".equals(modifier[i]) && !java.lang.reflect.Modifier.isAbstract(result))
00101             result |= java.lang.reflect.Modifier.ABSTRACT;
00102         else if ("final".equals(modifier[i]) && !java.lang.reflect.Modifier.isFinal(result))
00103             result |= java.lang.reflect.Modifier.FINAL;
00104         //else if ("interface".equals(modifier[i]) && !Modifier.isInterface(result))
00105             //result |= Modifier.INTERFACE;
00106         else if ("native".equals(modifier[i]) && !java.lang.reflect.Modifier.isNative(result))
00107             result |= java.lang.reflect.Modifier.NATIVE;
00108         else if ("private".equals(modifier[i]) && !java.lang.reflect.Modifier.isPrivate(result))
00109             result |= java.lang.reflect.Modifier.PRIVATE;
00110         else if ("protected".equals(modifier[i]) && !java.lang.reflect.Modifier.isProtected(result))
00111             result |= java.lang.reflect.Modifier.PROTECTED;
00112         else if ("public".equals(modifier[i]) && !java.lang.reflect.Modifier.isPublic(result))
00113             result |= java.lang.reflect.Modifier.PUBLIC;
00114         else if ("static".equals(modifier[i]) && !java.lang.reflect.Modifier.isStatic(result))
00115             result |= java.lang.reflect.Modifier.STATIC;
00116         else if ("synchronized".equals(modifier[i]) && !java.lang.reflect.Modifier.isSynchronized(result))
00117             result |= java.lang.reflect.Modifier.SYNCHRONIZED;
00118         else if ("transient".equals(modifier[i]) && !java.lang.reflect.Modifier.isTransient(result))
00119             result |= java.lang.reflect.Modifier.TRANSIENT;
00120         else if ("volatile".equals(modifier[i]) && !java.lang.reflect.Modifier.isVolatile(result))
00121             result |= java.lang.reflect.Modifier.VOLATILE;
00122         else
00123             throw new InvalidModifiersException("Invalid modifiers: " + modifiers);
00124     }
00125     return result;
00126 }
00127 /**
00128  * 
00129  * @return edu.ksu.cis.bandera.jjjc.symboltable.Type
00130  * @param type ca.mcgill.sable.soot.Type
00131  * @param symbolTable SymbolTable
00132  */
00133 public static edu.ksu.cis.bandera.jjjc.symboltable.Type convertType(ca.mcgill.sable.soot.Type type,
00134         edu.ksu.cis.bandera.jjjc.symboltable.SymbolTable symbolTable) throws CompilerException{
00135     if (type instanceof ca.mcgill.sable.soot.VoidType)
00136         return edu.ksu.cis.bandera.jjjc.symboltable.VoidType.TYPE;
00137     else if (type instanceof ca.mcgill.sable.soot.BooleanType)
00138         return edu.ksu.cis.bandera.jjjc.symboltable.BooleanType.TYPE;
00139     else if (type instanceof ca.mcgill.sable.soot.ByteType)
00140         return edu.ksu.cis.bandera.jjjc.symboltable.ByteType.TYPE;
00141     else if (type instanceof ca.mcgill.sable.soot.CharType)
00142         return edu.ksu.cis.bandera.jjjc.symboltable.CharType.TYPE;
00143     else if (type instanceof ca.mcgill.sable.soot.IntType)
00144         return edu.ksu.cis.bandera.jjjc.symboltable.IntType.TYPE;
00145     else if (type instanceof ca.mcgill.sable.soot.ShortType)
00146         return edu.ksu.cis.bandera.jjjc.symboltable.ShortType.TYPE;
00147     else if (type instanceof ca.mcgill.sable.soot.LongType)
00148         return edu.ksu.cis.bandera.jjjc.symboltable.LongType.TYPE;
00149     else if (type instanceof ca.mcgill.sable.soot.FloatType)
00150         return edu.ksu.cis.bandera.jjjc.symboltable.FloatType.TYPE;
00151     else if (type instanceof ca.mcgill.sable.soot.DoubleType)
00152         return edu.ksu.cis.bandera.jjjc.symboltable.DoubleType.TYPE;
00153     else if (type instanceof ca.mcgill.sable.soot.RefType) {
00154         edu.ksu.cis.bandera.jjjc.symboltable.Name name =
00155                 new edu.ksu.cis.bandera.jjjc.symboltable.Name(((ca.mcgill.sable.soot.RefType) type).className);
00156         return symbolTable.resolveClassOrInterfaceType(name);
00157     } else if (type instanceof ca.mcgill.sable.soot.NullType)
00158         return edu.ksu.cis.bandera.jjjc.symboltable.NullType.TYPE;
00159     else if (type instanceof ca.mcgill.sable.soot.VoidType)
00160         return edu.ksu.cis.bandera.jjjc.symboltable.VoidType.TYPE;
00161     
00162     ca.mcgill.sable.soot.ArrayType arrayType = (ca.mcgill.sable.soot.ArrayType) type;
00163     return new edu.ksu.cis.bandera.jjjc.symboltable.ArrayType(convertType(arrayType.baseType, symbolTable),
00164             arrayType.numDimensions);
00165 }
00166 /**
00167  * 
00168  * @return ca.mcgill.sable.soot.Type
00169  * @param type edu.ksu.cis.bandera.jjjc.symboltable.Type
00170  */
00171 public static ca.mcgill.sable.soot.Type convertType(edu.ksu.cis.bandera.jjjc.symboltable.Type type) {
00172     if (type instanceof edu.ksu.cis.bandera.jjjc.symboltable.VoidType)
00173         return ca.mcgill.sable.soot.VoidType.v();
00174     else if (type instanceof edu.ksu.cis.bandera.jjjc.symboltable.BooleanType)
00175         return ca.mcgill.sable.soot.BooleanType.v();
00176     else if (type instanceof edu.ksu.cis.bandera.jjjc.symboltable.ByteType)
00177         return ca.mcgill.sable.soot.ByteType.v();
00178     else if (type instanceof edu.ksu.cis.bandera.jjjc.symboltable.CharType)
00179         return ca.mcgill.sable.soot.CharType.v();
00180     else if (type instanceof edu.ksu.cis.bandera.jjjc.symboltable.IntType)
00181         return ca.mcgill.sable.soot.IntType.v();
00182     else if (type instanceof edu.ksu.cis.bandera.jjjc.symboltable.ShortType)
00183         return ca.mcgill.sable.soot.ShortType.v();
00184     else if (type instanceof edu.ksu.cis.bandera.jjjc.symboltable.LongType)
00185         return ca.mcgill.sable.soot.LongType.v();
00186     else if (type instanceof edu.ksu.cis.bandera.jjjc.symboltable.FloatType)
00187         return ca.mcgill.sable.soot.FloatType.v();
00188     else if (type instanceof edu.ksu.cis.bandera.jjjc.symboltable.DoubleType)
00189         return ca.mcgill.sable.soot.DoubleType.v();
00190     else if (type instanceof edu.ksu.cis.bandera.jjjc.symboltable.ClassOrInterfaceType)
00191         return ca.mcgill.sable.soot.RefType.v(((edu.ksu.cis.bandera.jjjc.symboltable.ClassOrInterfaceType)
00192                 type).getName().toString());
00193     else if (type instanceof edu.ksu.cis.bandera.jjjc.symboltable.NullType)
00194         return ca.mcgill.sable.soot.NullType.v();
00195 
00196     edu.ksu.cis.bandera.jjjc.symboltable.ArrayType arrayType = (edu.ksu.cis.bandera.jjjc.symboltable.ArrayType) type;
00197     return ca.mcgill.sable.soot.ArrayType.v((ca.mcgill.sable.soot.BaseType) convertType(arrayType.baseType),
00198             arrayType.nDimensions);
00199 }
00200 /**
00201  * 
00202  * @return int
00203  * @param s java.lang.String
00204  */
00205 public static int countArrayDimensions(String s) {
00206     int result = 0;
00207     
00208     for (int i = 0; i < s.length(); i++) 
00209         if (s.charAt(i) == '[')
00210             result++;
00211             
00212     return result;
00213 }
00214 /**
00215  * 
00216  * @return java.lang.String
00217  * @param s java.lang.String
00218  */
00219 public static String decodeString(String s) {
00220     StreamTokenizer st = new StreamTokenizer(new StringReader(s));
00221     try {
00222         st.nextToken();
00223         return st.sval;
00224     } catch (Exception e) {
00225         return null;
00226     }
00227 }
00228 /**
00229  * 
00230  * @return java.lang.String
00231  * @param s java.lang.String
00232  */
00233 public static String encodeString(String s) {
00234     StringBuffer buffer = new StringBuffer();
00235     for (int i = 0; i < s.length(); i++) {
00236         int c = s.charAt(i);
00237         int hibyte = c & 0xff00;
00238         if (hibyte != 0) {
00239             String temp = Integer.toString(c & 0xffff, 16);
00240             for (int j = 0; j < (4 - temp.length()); j++) {
00241                 temp = "0" + temp;
00242             }
00243             buffer.append("\\u" + temp);
00244         } else {
00245             switch ((char) c) {
00246                 case '\b':
00247                     buffer.append("\\b");
00248                     break;
00249                 case '\t':
00250                     buffer.append("\\t");
00251                     break;
00252                 case '\n':
00253                     buffer.append("\\n");
00254                     break;
00255                 case '\f':
00256                     buffer.append("\\f");
00257                     break;
00258                 case '\r':
00259                     buffer.append("\\r");
00260                     break;
00261                 case '\"':
00262                     buffer.append("\\\"");
00263                     break;
00264                 case '\'':
00265                     buffer.append("\\\'");
00266                     break;
00267                 case '\\':
00268                     buffer.append("\\\\");
00269                     break;
00270                 default:
00271                     buffer.append((char) c);
00272             }
00273         }
00274     }
00275     return buffer.toString();
00276 }
00277 /**
00278  * 
00279  * @return java.lang.String[]
00280  * @param s java.lang.String
00281  * @param limiter java.lang.String
00282  */
00283 public static String[] splitString(String s, String limiter) {
00284     StringTokenizer tokens = new StringTokenizer(s, limiter);
00285     int length = tokens.countTokens();
00286     String[] result = new String[length];
00287 
00288     for (int i = 0; i < length; i++) {
00289         result[i] = tokens.nextToken();
00290     }
00291     
00292     return result;
00293 }
00294 }

Generated at Thu Feb 7 06:59:02 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001