/* * Copyright 2008 Wishnu Prasetya. * * This file is part of T2. * T2 is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License (GPL) as published by the * Free Software Foundation; either version 3 of the License, or any * later version. * * T2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * A copy of the GNU General Public License can be found in T2 distribution. * If it is missing, see http://www.gnu.org/licenses. */ package t2ext.util; import java.io.File; import java.net.URI; import java.net.URISyntaxException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import t2ext.xml.XMLBuilder; import com.sun.org.apache.bcel.internal.generic.Type; /** * @author Maaike Gerritsen */ public class Util { /** * Gets the physical location of the className. * @param className The name of the class (for example: org.package.MyClass) * @return Returns a File pointing to the location of the class. */ public static File getClassLocation(String className) { String classString = className.replace('.', '/')+".class"; URI uri = null; try { uri = new Util().getClass().getClassLoader().getResource(classString).toURI(); } catch (NullPointerException e) { System.out.println("Couldn't load "+className); e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } return new File(uri); } public static String newFileName(String byteClass){ byteClass = byteClass.replace(".", ":"); String[] split = byteClass.split(":"); String result = ""; for (int i = 0; i < split.length-1; i++) { String s = split[i]; result += s+"/"; } String cleanMethodsDir = new File(result).getAbsolutePath().toString(); result = cleanMethodsDir+"/"+split[split.length-1]+".class"; return result; } // Never used? /* public static String directoryName(String byteClass){ byteClass = byteClass.replace(".", ":"); String[] split = byteClass.split(":"); String result = ""; if(split.length>1){ for (int i = 0; i < split.length-1; i++) { String s = split[i]; result += s+"/"; } return result; } return ""; } */ public static String completeMethodName(String name, Type[] arguments){ name = name +"_"+arguments.length; for (int i = 0; i < arguments.length; i++) { Type arg = arguments[i]; String argName = arg.toString(); argName = replaceIllegalChars(argName); name = name+argName; } return name; } public static String officialMethodName(String className, String methodName, Type[] arguments){ String complete = className+"."+methodName+"("; boolean argsPresent = false; for (int i = 0; i < arguments.length; i++) { Type arg = arguments[i]; String argName = arg.toString(); argName = argName.replace("$", "."); complete = complete + argName + ", "; argsPresent = true; } if(argsPresent) complete = complete.substring(0, complete.length()-2); complete = complete + ")"; return complete; } public static String officialMethodName(String className, String methodName, java.lang.reflect.Type[] arguments){ String complete = className+"."+methodName+"("; boolean argsPresent = false; for (int i = 0; i < arguments.length; i++) { java.lang.reflect.Type arg = arguments[i]; String argName = arg.toString(); if(argName.startsWith("class ")) argName = argName.replace("class ", ""); if(argName.startsWith("interface ")) argName = argName.replace("interface ", ""); argName = argName.replace("$", "."); complete = complete + argName + ", "; argsPresent = true; } if(argsPresent) complete = complete.substring(0, complete.length()-2); complete = complete + ")"; return complete; } public static String replaceIllegalChars(String name){ name = name.replace("$", ""); name = name.replace(".", ""); name = name.replace("[]", "Array"); return name; } public static boolean foundPath(Integer id, String originalName) { XMLBuilder xmlBuilder = new XMLBuilder(); Document doc = xmlBuilder.readXMLFile("paths.xml"); String condition = "//method[@official='"+originalName+"']/path[@id='"+id+"']"; NodeList list = xmlBuilder.getNodesFromXML(doc, condition); Element path = (Element) list.item(0); boolean found = new Boolean(path.getAttribute("found")); return found; } }