List of usage examples for org.aspectj.apache.bcel.classfile JavaClass getBytes
public byte[] getBytes()
From source file:br.jabuti.device.ProberInstrum.java
License:Open Source License
public static void main(String args[]) throws Throwable { String workDir = null;/* ww w . j a v a 2 s.co m*/ String projectName = null; JabutiProject project = null; String baseClass = null; // The class file to be executed Program program = null; // The program to be tested HashSet toInstrumenter = null; String outName = null; // Name of the output file String server = null; // Address of the server String tempFile = null; // Name of temporary file String midletName = null; int memTreshold = 0; boolean keepAlive = true; if (args.length > 0) { int i = 0; while (args[i].startsWith("-")) { // -CP: Class path if (("-d".equals(args[i])) && (i < args.length - 1)) { i++; workDir = args[i]; } // -P: project name else if (("-p".equals(args[i])) && (i < args.length - 1)) { i++; projectName = args[i]; } else if (("-o".equals(args[i])) && (i < args.length - 1)) { i++; outName = args[i]; } else if (("-name".equals(args[i])) && (i < args.length - 1)) { i++; midletName = args[i]; } else if (("-h".equals(args[i])) && (i < args.length - 1)) { i++; server = args[i]; } else if (("-temp".equals(args[i])) && (i < args.length - 1)) { i++; tempFile = args[i]; } else if (("-nokeepalive".equals(args[i])) && (i < args.length - 1)) { keepAlive = false; } else if (("-mem".equals(args[i])) && (i < args.length - 1)) { i++; int f = 1; StringBuffer s = new StringBuffer(args[i]); if (args[i].endsWith("M") || args[i].endsWith("m")) { f = 1024 * 1024; s = s.delete(s.length() - 1, s.length()); } else if (args[i].endsWith("K") || args[i].endsWith("k")) { f = 1024; s = s.delete(s.length() - 1, s.length()); } memTreshold = f * Integer.parseInt(s.toString()); } else { System.out.println("Error: Unrecognized option: " + args[i]); System.exit(0); } i++; } if (i >= args.length) { System.out.println("Error: Missing base class!!!"); System.exit(0); } // Ultimo parametro... file seguido dos parametros baseClass = args[i++]; // Checking if all essential parameters are not null if (projectName == null || outName == null || midletName == null) { System.out.println("Error: Missing parameter!!!"); usage(); System.exit(0); } // Creating the absolute path to a given project String absoluteName = null; if (workDir != null) { absoluteName = workDir + File.separator + projectName; } else { absoluteName = projectName; } try { File theFile = new File(absoluteName); if (!theFile.isFile()) // verifica se existe { System.out.println("File " + theFile.getName() + " not found"); System.exit(0); } project = JabutiProject.reloadProj(theFile.toString(), false); program = project.getProgram(); toInstrumenter = project.getInstr(); } catch (Exception e) { ToolConstants.reportException(e, ToolConstants.STDERR); System.exit(0); } DeviceProbeInsert dpi = new DeviceProbeInsert(program, toInstrumenter, baseClass, tempFile, server, memTreshold, keepAlive, midletName); Map mp = null; try { mp = dpi.instrument(); // substitui os objetos JavaClass por byte[] JavaClass jv; Iterator it0 = mp.keySet().iterator(); Hashtable ht = new Hashtable(); while (it0.hasNext()) { String clName = (String) it0.next(); jv = (JavaClass) mp.get(clName); ht.put(clName, jv.getBytes()); } mp = ht; File outFile = new File(outName); FileOutputStream fos = new FileOutputStream(outFile); JarOutputStream outJar = new JarOutputStream(fos); Iterator it = mp.keySet().iterator(); System.out.println(); System.out.println("----------------------------------------"); System.out.println("Files to insert:"); while (it.hasNext()) { String clName = (String) it.next(); System.out.println("\t" + clName); byte[] b = (byte[]) mp.get(clName); clName = clName.replace('.', '/'); JarEntry jarEntry = new JarEntry(clName + ".class"); outJar.putNextEntry(jarEntry); outJar.write(b); } ClassPath cp = new ClassPath(System.getProperty("java.class.path")); for (int z1 = 0; z1 < ProberClasses.length; z1++) { String clName = ProberClasses[z1]; System.out.println("\t" + clName); byte[] b = cp.getBytes(clName, ".class"); clName = clName.replace('.', '/'); JarEntry jarEntry = new JarEntry(clName + ".class"); outJar.putNextEntry(jarEntry); outJar.write(b); } System.out.println("----------------------------------------"); outJar.close(); } catch (Exception eu) { System.err.println(eu); eu.printStackTrace(); } } else { usage(); } }
From source file:br.jabuti.lookup.java.bytecode.RClassCode.java
License:Open Source License
/** Creates a RClassCode given its name and a {@link JavaClass} object /*w w w . jav a 2 s. c o m*/ @param y The {@link JavaClass} object already created for this class @param x The complete name of the class */ public RClassCode(JavaClass y, String x) { super(x); theClass = null; theClassPersistent = Persistency.add(y.getBytes()); if (theClassPersistent == null) theClass = y; superName = y.getSuperclassName(); interfaces = y.getInterfaceNames(); isInter = y.isInterface(); }
From source file:br.jabuti.probe.desktop.ProberInstrum.java
License:Open Source License
/** * // ww w . j ava2 s . co m * @param program * - classes that are part of the program * @param toInstrumenter * - list of classes to instrument. null == all * @param CFGOption * - type of CFG used * @param outName * - name of the output file * @param traceFileName * - name of the trace file, in case there is a main * @return OK? */ public static boolean instrumentProgram(Program program, HashSet toInstrumenter, String outName, String traceFileName) { DefaultProbeInsert dpi = new DefaultProbeInsert(program, toInstrumenter); Map mp = null; // esta com pau aqui.... Precisa ignorar as classes com // ClassFormatException geradas pelo BCEL. // Essas classes devem ser excluidas da instrumentao mas incluidas no // jar sem serem instrumentadas. try { mp = dpi.instrument(); // substitui os objetos JavaClass por byte[] Iterator it0 = mp.keySet().iterator(); Hashtable ht = new Hashtable(); while (it0.hasNext()) { String clName = (String) it0.next(); JavaClass jv = (JavaClass) mp.get(clName); ht.put(clName, jv.getBytes()); } mp = ht; File outFile = new File(outName); FileOutputStream fos = new FileOutputStream(outFile); JarOutputStream outJar = new JarOutputStream(fos); Iterator it = mp.keySet().iterator(); while (it.hasNext()) { String clName = (String) it.next(); byte[] b = (byte[]) mp.get(clName); clName = clName.replace('.', '/'); JarEntry jarEntry = new JarEntry(clName + ".class"); outJar.putNextEntry(jarEntry); outJar.write(b); } ClassPath cp = new ClassPath(System.getProperty("java.class.path")); for (int z1 = 0; z1 < ProberClasses.length; z1++) { String clName = ProberClasses[z1]; byte[] b = cp.getBytes(clName, ".class"); clName = clName.replace('.', '/'); JarEntry jarEntry = new JarEntry(clName + ".class"); outJar.putNextEntry(jarEntry); outJar.write(b); } outJar.close(); } catch (Exception eu) { eu.printStackTrace(); return false; } return true; }
From source file:br.jabuti.probe.desktop.ProberLoader.java
License:Open Source License
public static void main(String args[]) throws Throwable { String classPath = null;// w ww. j a v a 2s . c o m String workDir = null; String projectName = null; String baseClass = null; // The class file to be executed String[] parameters = null; // The paramenters used to execute the main // file JabutiProject project = null; Program program = null; // The program to be tested HashSet toInstrumenter = null; String traceFileName = null; // A given class or compressed file name if (args.length > 0) { int i = 0; while (args[i].startsWith("-")) { // -cp: Class path if (("-cp".equals(args[i])) && (i < args.length - 1)) { i++; classPath = args[i]; } // -d: work directory else if (("-d".equals(args[i])) && (i < args.length - 1)) { i++; workDir = args[i]; } // -p: project name else if (("-p".equals(args[i])) && (i < args.length - 1)) { i++; projectName = args[i]; } else { System.out.println("Error: Unrecognized option: " + args[i]); System.exit(0); } i++; } if (i >= args.length) { System.out.println("Error: Missing base class!!!"); System.exit(0); } // Ultimo parmetro... file seguido dos parmetros baseClass = args[i++]; // System.out.println("Loaded: " + clo.loadClass(baseClass)); parameters = new String[args.length - i]; for (int j = 0; i < args.length; j++, i++) { parameters[j] = args[i]; } // Checking if all essential parameters are not null if ((projectName == null) || (baseClass == null)) { System.out.println("Error: Missing parameter!!!"); usage(); System.exit(0); } // Creating the absolute path to a given project String absoluteName = null; if (workDir != null) { absoluteName = workDir + File.separator + projectName; } else { absoluteName = projectName; } traceFileName = absoluteName.substring(0, absoluteName.length() - ToolConstants.traceExtension.length()) + ToolConstants.traceExtension; // System.setProperty("DEFAULT_PROBER", traceFileName); System.out.println("Project Name: " + absoluteName); System.out.println("Trace File Name: " + traceFileName); try { File theFile = new File(absoluteName); if (!theFile.isFile()) // verifica se existe { System.out.println("File " + theFile.getName() + " not found"); System.exit(0); } project = JabutiProject.reloadProj(theFile.toString(), false); program = project.getProgram(); toInstrumenter = project.getInstr(); } catch (Exception e) { ToolConstants.reportException(e, ToolConstants.STDERR); System.exit(0); } DefaultProbeInsert dpi = new DefaultProbeInsert(program, toInstrumenter); Map mp = null; try { mp = dpi.instrument(); // substitui os objetos JavaClass por byte[] Iterator it0 = mp.keySet().iterator(); Hashtable ht = new Hashtable(); while (it0.hasNext()) { String clName = (String) it0.next(); JavaClass jv = (JavaClass) mp.get(clName); ht.put(clName, jv.getBytes()); } ClassPath cp = new ClassPath(System.getProperty("java.class.path")); for (int z1 = 0; z1 < ProberClasses.length; z1++) { String clName = ProberClasses[z1]; byte[] b = cp.getBytes(clName, ".class"); clName = clName.replace('.', '/'); ht.put(clName, b); } mp = ht; // Alterado: agora o programa soh procura as classes no caminho // especificado // no argumento -cp /* * if (classPath != null) { classPath = classPath + * System.getProperty("path.separator") + * System.getProperty("java.class.path"); } else { classPath = * System.getProperty("java.class.path"); } */ if (classPath == null) { classPath = "."; } InstrumentLoader myLoader = new InstrumentLoader(mp, classPath); Class xcl = myLoader.loadClass("br.jabuti.probe.DefaultProber"); Class str = myLoader.loadClass("java.lang.String"); java.lang.reflect.Method init = xcl.getMethod("init", new Class[] { str }); init.invoke((Object) null, new Object[] { traceFileName }); //Included to start the trace collection java.lang.reflect.Method start = xcl.getMethod("startTrace", new Class[] {}); start.invoke((Object) null, new Object[] {}); myLoader.runClass(baseClass, parameters); } catch (Throwable eu) { System.err.println(eu); eu.printStackTrace(); } } else { usage(); } }
From source file:br.jabuti.project.Project2XML.java
License:Open Source License
public boolean project2XML(JabutiProject prj, File xmlFile) { try {//from www. j a v a 2s .c o m this.prj = prj; out = new OutputStreamWriter(new FileOutputStream(xmlFile), "UTF8"); } catch (Throwable t) { ToolConstants.reportException(t, ToolConstants.STDERR); return false; } StringBuilder xmlOut = new StringBuilder(); // Saving the high level element: JABUTI emit("<" + XML2Project.JABUTI + ">"); // Saving the PROJECT and its attributes indentLevel++; xmlOut.append("<" + XML2Project.PROJECT); xmlOut.append(" name=\"" + prj.getProjectFileName() + "\""); xmlOut.append(" type=\"research\""); xmlOut.append(" mobility=\"" + (prj.isMobility() ? "Y" : "N") + "\""); xmlOut.append("\">"); nl(); emit(xmlOut.toString()); // Saving the CLASSPATH and its attributes xmlOut.setLength(0); xmlOut.append("<" + XML2Project.CLASSPATH); // TODO: save each path of the classpath as a single element // Converting the classpath in a system independent way (more less) StringTokenizer st = new StringTokenizer(prj.getClasspath(), File.pathSeparator); StringBuffer sb = new StringBuffer(); while (st.hasMoreTokens()) { String s = st.nextToken(); if (File.pathSeparatorChar == ';') // If windows s = s.replace(':', '?'); // replacing driver letter d: by d? for instance. sb.append(s + " "); } xmlOut.append(" path=\"" + sb.toString().trim() + "\"/>"); nl(); emit(xmlOut.toString()); // Saving the JUNIT_SRC_DIR and its attributes xmlOut = new StringBuffer("<" + XML2Project.JUNIT_SRC_DIR); xmlOut.append(" dir=\"" + prj.getJunitSrcDir() + "\"/>"); nl(); emit(xmlOut.toString()); // Saving the JUNIT_BIN_DIR and its attributes xmlOut = new StringBuffer("<" + XML2Project.JUNIT_BIN_DIR); xmlOut.append(" dir=\"" + prj.getJunitBinDir() + "\"/>"); nl(); emit(xmlOut.toString()); // Saving the JUNIT_TEST_SET and its attributes xmlOut = new StringBuffer("<" + XML2Project.JUNIT_TEST_SET); xmlOut.append(" name=\"" + prj.getJunitTestSet() + "\"/>"); nl(); emit(xmlOut.toString()); // Saving the JABUTI_BIN and its attributes xmlOut = new StringBuffer("<" + XML2Project.JUNIT_JAR); xmlOut.append(" name=\"" + prj.getJUnitJar() + "\"/>"); nl(); emit(xmlOut.toString()); // Saving the AVOIDED_PACKAGES and its attributes nl(); emit("<" + XML2Project.AVOIDED_PACKAGES + ">"); // Saving the individual PACKAGE and its attributes indentLevel++; Iterator it = prj.getAvoid().iterator(); while (it.hasNext()) { xmlOut = new StringBuffer("<" + XML2Project.PACKAGE); xmlOut.append(" name=\"" + (String) it.next() + "\"/>"); nl(); emit(xmlOut.toString()); } indentLevel--; nl(); emit("</" + XML2Project.AVOIDED_PACKAGES + ">"); // Saving CLASS and INST_CLASS tags Program pg = prj.getProgram(); String[] cnames = pg.getCodeClasses(); for (int i = 0; i < cnames.length; i++) { ClassFile cf = prj.getClassFile(cnames[i]); xmlOut = new StringBuffer("<"); String tag = null; String size = "0000"; String check = "00000000"; if (cf != null) { // INST_CLASS tag tag = XML2Project.INST_CLASS; JavaClass theClass = cf.getJavaClass(); size = new Integer(theClass.getBytes().length).toString(); check = theClass.getMajor() + "-" + theClass.getMinor(); } else { // CLASS tag tag = XML2Project.CLASS; } // CLASS commom attributes xmlOut.append(tag + " "); xmlOut.append(" name=\"" + cnames[i] + "\""); xmlOut.append(" size=\"" + size + "\""); xmlOut.append(" checksum=\"" + check + "\">"); nl(); emit(xmlOut.toString()); indentLevel++; // EXTEND attributes RClassCode cc = (RClassCode) pg.get(cnames[i]); String superC = cc.getSuperClass(); int level = pg.levelOf(cnames[i]); xmlOut = new StringBuffer("<" + XML2Project.EXTEND); xmlOut.append(" name=\"" + superC + "\""); xmlOut.append(" level=\"" + new Integer(level).toString() + "\"/>"); nl(); emit(xmlOut.toString()); // IMPLEMENT String[] interfaces = cc.getInterfaces(); for (int k = 0; k < interfaces.length; k++) { xmlOut = new StringBuffer("<" + XML2Project.IMPLEMENT); xmlOut.append(" name=\"" + interfaces[k] + "\"/>"); nl(); emit(xmlOut.toString()); } if (cf != null) classFile2XML(prj, cf); // Closing the correct tag indentLevel--; nl(); emit("</" + tag + ">"); } // The TEST_SET element and its attributes testSet2XML(); indentLevel--; // Closing PROJECT nl(); emit("</" + XML2Project.PROJECT + ">"); indentLevel--; // Closing JABUTI nl(); emit("</" + XML2Project.JABUTI + ">"); try { out.flush(); } catch (IOException ioe) { ToolConstants.reportException(ioe, ToolConstants.STDERR); return false; } try { out.close(); } catch (IOException ioe) { ToolConstants.reportException(ioe, ToolConstants.STDERR); return false; } return true; }
From source file:org.caesarj.mixer.intern.ClassModifier.java
License:Open Source License
/** * Write the class to file system /*ww w . ja v a 2 s . c om*/ * @param clazz * @throws MixerException */ protected void writeClass(String newClassName, JavaClass clazz) throws MixerException { try { String fileName = _outputDir + File.separator + newClassName + ".class"; clazz.dump(fileName); _byteCodeMap.addClassFile(fileName, clazz.getBytes()); } catch (IOException e) { throw new MixerException("Unable to write classfile:" + e); } }