⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tablecreator.java

📁 该工具也是用于字节码插桩
💻 JAVA
字号:
/* * TableCreator.java * * Created on August 14, 2001, 7:34 AM * * See LICENSE file for license conditions. */package residue.tables;import java.io.*;import java.util.*;import org.apache.bcel.generic.InstructionHandle;/** * This class is used to create a Table that will be parsed * correctly. * * @author  chowells * @version  */public class TableCreator{	private List tempHitEntries = new ArrayList();	private List tempRefEntries = new ArrayList();		private List hitEntries = new ArrayList();	private List refEntries = new ArrayList();	private Set sourceRoots = new TreeSet();		private boolean namesvalid;	private String classname;	private String methodname;	private String sourcename;		private int hitIndex;	private int refIndex;		private Map refIndices;		/**	 * This adds a block to the entries which are directly probed.	 *	 * Its return value is the index of the entry in the	 * probed part of the table.	 */	public int addHitEntry(Object ref,	                       InstructionHandle start,	                       InstructionHandle probe,	                       InstructionHandle end,	                       Set lines)	{		tempHitEntries.add(new TempHitEntry(hitIndex, ref, start, probe, end, lines));				// postIncrement, folks...  The value returned is the value		// before it is incremented		return hitIndex++;	} // addHitEntry			/**	 * This adds a block to the entries which aren't directly probed.	 *	 * Its return value is the index of the entry in the reference	 * part of the table.	 */	public int addRefEntry(Object ref,	                       InstructionHandle start,	                       InstructionHandle end,	                       Set lines)	{		tempRefEntries.add(new TempRefEntry(refIndex, ref, start, end, lines));				// postIncrement, folks...  The value returned is the value		// before it is incremented		return refIndex++;	} // addRefEntry		public int getHitIndex()	{		return hitIndex;	} // getHitIndex		public int getRefIndex()	{		return refIndex;	} // getRefIndex		/**	 * This is called at the beginning of a method, to set the various names	 * used in this method's blocks.	 */	public void startMethod(String classname, String methodname, String sourcename)	{		if (namesvalid) throw new IllegalStateException("Forgot to end the method in the table");		this.classname = filter(classname);		this.methodname = filter(methodname);		this.sourcename = filter(sourcename);		namesvalid = true;	} // startMethod		/**	 * This method adds a source root to the set stored in this table	 */	public void addSourceRoot(String root)	{		sourceRoots.add(filter(root));	} // addRoot		/**	 * This should be called when there are no more entries to be added	 * during a method.  When it is called, the InstructionHandles should	 * still be valid, and they should have the correct positions available.	 *	 * The argument should be a Map from Objects to Integers, used to	 * determine the indices on for the ref component of the output.	 */	public void endMethod(Map refIndices)	{		if (!namesvalid) throw new IllegalStateException("Forgot to start the method in the table");		namesvalid = false;				this.refIndices = refIndices;				Iterator it = tempHitEntries.iterator();		while (it.hasNext())			hitEntries.add(it.next().toString());				it = tempRefEntries.iterator();		while (it.hasNext())			refEntries.add(it.next().toString());				tempHitEntries.clear();		tempRefEntries.clear();	} // endMethod		/**	 * Writes the representation of the create Table to the given	 * PrintStream.	 */	public void writeTo(PrintStream p)	{		if (namesvalid)			throw new IllegalStateException("Illegal attempt to print before ending the last method");				p.println("[probed blocks]");		Iterator it = hitEntries.iterator();		while (it.hasNext())			p.println(it.next());				p.println("[referenced blocks]");		it = refEntries.iterator();		while (it.hasNext())			p.println(it.next());				p.println("[source roots]");		it = sourceRoots.iterator();		while (it.hasNext())			p.println(it.next());	} // writeTo		/**	 * Writes the representation of the create Table to the given	 * File.	 */	public void writeTo(File f) throws IOException	{		PrintStream p = new PrintStream(new BufferedOutputStream(new FileOutputStream(f)));				writeTo(p);				p.close();	} // writeTo		/**	 * Writes the representation of the create Table to the file	 * specified in the String.	 */	public void writeTo(String filename) throws IOException	{		writeTo(new File(filename));	} // writeTo		private class TempHitEntry	{		private int index;		private Object ref;				private InstructionHandle start;		private InstructionHandle probe;		private InstructionHandle end;				private Set lines;				public TempHitEntry(int index,		                    Object ref,		                    InstructionHandle start,		                    InstructionHandle probe,		                    InstructionHandle end,		                    Set lines)		{			this.index = index;			this.ref = ref;			this.start = start;			this.probe = probe;			this.end = end;			this.lines = new TreeSet(lines);		} // TempHitEntry				public String toString()		{			StringBuffer result = new StringBuffer();			result.append(index);			result.append(' ');			if (refIndices.containsKey(ref))				result.append(refIndices.get(ref));			else				result.append(-1);			result.append(' ');			result.append(classname);			result.append(' ');			result.append(methodname);			result.append(' ');			result.append(start.getPosition());			result.append(' ');			result.append(probe.getPosition());			result.append(' ');			result.append(end.getPosition());			result.append(' ');			result.append(sourcename);						Iterator it = lines.iterator();			while (it.hasNext())			{				result.append(' ');				result.append(it.next().toString());			}						result.append(';');						return result.toString();		} // toString	} // class TempHitEntry		private class TempRefEntry	{		private int index;		private Object ref;				private InstructionHandle start;		private InstructionHandle end;				private Set lines;				public TempRefEntry(int index,		                    Object ref,		                    InstructionHandle start,		                    InstructionHandle end,		                    Set lines)		{			this.index = index;			this.ref = ref;			this.start = start;			this.end = end;			this.lines = new TreeSet(lines);		} // TempHitEntry				public String toString()		{			StringBuffer result = new StringBuffer();			result.append(index);			result.append(' ');			if (refIndices.containsKey(ref))				result.append(refIndices.get(ref));			else				result.append(-1);			result.append(' ');			result.append(classname);			result.append(' ');			result.append(methodname);			result.append(' ');			result.append(start.getPosition());			result.append(' ');			result.append(end.getPosition());			result.append(' ');			result.append(sourcename);						Iterator it = lines.iterator();			while (it.hasNext())			{				result.append(' ');				result.append(it.next().toString());			}						result.append(';');						return result.toString();		} // toString	} // class TempRefEntry		public static String filter(String s)	{		StringBuffer result = new StringBuffer();		result.append('"');				for (int i = 0; i < s.length(); i++)		{			char c = s.charAt(i);						if (c == '\\' || c == '"')			{				result.append('\\');				result.append(c);			}			else if (c < ' ' || c > '~')			{				result.append("\\u");								String hex = Integer.toHexString(c);				for (int j = hex.length(); j < 4; j++)					result.append('0');								result.append(hex);			}			else			{				result.append(c);			}		} // for				result.append('"');				return result.toString();	} // filter	} // class TableCreator

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -