/** * Write contents of the given JavaClass into HTML files. * * @param java_class The class to write * @param dir The directory to put the files in */ public Class2HTML(JavaClass java_class, String dir) throws IOException { Method[] methods = java_class.getMethods(); this.java_class = java_class; this.dir = dir; class_name = java_class.getClassName(); // Remember full name constant_pool = java_class.getConstantPool(); // Get package name by tacking off everything after the last `.' int index = class_name.lastIndexOf('.'); if (index > -1) class_package = class_name.substring(0, index); else class_package = ""; // default package ConstantHTML constant_html = new ConstantHTML(dir, class_name, class_package, methods, constant_pool); /* Attributes can't be written in one step, so we just open a file * which will be written consequently. */ AttributeHTML attribute_html = new AttributeHTML(dir, class_name, constant_pool, constant_html); // MethodHTML method_html = new MethodHTML(dir, class_name, methods, java_class.getFields(), // constant_html, attribute_html); // Write main file (with frames, yuk) writeMainHTML(attribute_html); new CodeHTML(dir, class_name, methods, constant_pool, constant_html); attribute_html.close(); }
/** * Dumps the contents of the specified class to the specified directory. The file is named * dump_dir/[class].bcel. It contains a synopsis of the fields and methods followed by the jvm * code for each method. * * @param jc javaclass to dump * @param dump_dir directory in which to write the file */ public static void dump(JavaClass jc, File dump_dir) { try { dump_dir.mkdir(); File path = new File(dump_dir, jc.getClassName() + ".bcel"); PrintStream p = new PrintStream(path); // Print the class, super class and interfaces p.printf("class %s extends %s\n", jc.getClassName(), jc.getSuperclassName()); String[] inames = jc.getInterfaceNames(); if ((inames != null) && (inames.length > 0)) { p.printf(" "); for (String iname : inames) p.printf("implements %s ", iname); p.printf("\n"); } // Print each field p.printf("\nFields\n"); for (Field f : jc.getFields()) p.printf(" %s\n", f); // Print the signature of each method p.printf("\nMethods\n"); for (Method m : jc.getMethods()) p.printf(" %s\n", m); // If this is not an interface, print the code for each method if (!jc.isInterface()) { for (Method m : jc.getMethods()) { p.printf("\nMethod %s\n", m); Code code = m.getCode(); if (code != null) p.printf(" %s\n", code.toString().replace("\n", "\n ")); } } // Print the details of the constant pool. p.printf("Constant Pool:\n"); ConstantPool cp = jc.getConstantPool(); Constant[] constants = cp.getConstantPool(); for (int ii = 0; ii < constants.length; ii++) { p.printf(" %d %s\n", ii, constants[ii]); } p.close(); } catch (Exception e) { throw new Error("Unexpected error dumping javaclass", e); } }
/** * Constructor is only used by following two factory methods: getTemplate for the dispatch of the * creation with newClassInfo * * @param clazz * @param ai */ private JopClassInfo(JavaClass clazz, OldAppInfo ai) { super(clazz, ai); methodsAddress = 0; cpoolAddress = 0; instSize = 0; instGCinfo = 0; cpoolUsed = new LinkedList<Integer>(); // the template class info is created with a null pointer if (clazz != null) { if (clazz.getClassName().equals(JOPizer.stringClass)) { StringInfo.cli = this; } if (clazz.getClassName().equals(JOPizer.objectClass)) { nrObjMethods = clazz.getMethods().length; } } }
private DbJVClass importClass(JavaClass claz, Controller controller) throws DbException { String packName = claz.getPackageName(); String qualifiedName = claz.getClassName(); int idx = qualifiedName.lastIndexOf('.'); String classname = qualifiedName.substring(idx + 1); DbJVClass dbClaz = null; try { if (m_classModel != null) { DbJVPackage pack = findPackageByName(packName); // create class or interface int value = claz.isInterface() ? JVClassCategory.INTERFACE : JVClassCategory.CLASS; JVClassCategory catg = JVClassCategory.getInstance(value); dbClaz = (pack == null) ? new DbJVClass(this.m_classModel, catg) : new DbJVClass(pack, catg); dbClaz.setName(classname); // set class modifiers dbClaz.setAbstract(claz.isAbstract()); dbClaz.setFinal(claz.isFinal()); dbClaz.setStatic(claz.isStatic()); dbClaz.setStrictfp(claz.isStrictfp()); dbClaz.setVisibility(toVisibility(claz)); // create inheritances importInheritances(dbClaz, claz); // create fields if (m_params.createFields) { Field[] fields = claz.getFields(); for (Field field : fields) { importField(dbClaz, field); } } // create methods if (m_params.createMethods) { Method[] methods = claz.getMethods(); for (Method method : methods) { importMethod(dbClaz, method); } } // keep user informed of progression String pattern = LocaleMgr.misc.getString("0SuccessfullyCreated"); String msg = MessageFormat.format(pattern, qualifiedName); controller.println(msg); } // end if } catch (DbException ex) { controller.println(ex.toString()); } // end try return dbClaz; } // end importClass()
public void visitJavaClass(JavaClass clazz) { super.visitJavaClass(clazz); JopClassInfo cli = (JopClassInfo) this.cli; cpool = clazz.getConstantPool(); if (clazz.isInterface()) { cli.interfaceID = ++cli.interfaceCnt; cli.interfaceList.add(cli.interfaceID - 1, clazz.getClassName()); } }
/** @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor */ public void visitObject(Object aJavaClass) { final JavaClass javaClass = (JavaClass) aJavaClass; final String className = javaClass.getClassName(); final JavaClass[] superClasses = javaClass.getSuperClasses(); final Method[] methods = javaClass.getMethods(); // Check all methods for (int i = 0; i < methods.length; i++) { final Method method = methods[i]; // Check that the method is a possible match if (!method.isPrivate() && method.isStatic()) { // Go through all their superclasses for (int j = 0; j < superClasses.length; j++) { final JavaClass superClass = superClasses[j]; final String superClassName = superClass.getClassName(); final Method[] superClassMethods = superClass.getMethods(); // Go through the methods of the superclasses for (int k = 0; k < superClassMethods.length; k++) { final Method superClassMethod = superClassMethods[k]; if (superClassMethod.getName().equals(method.getName()) && !ignore(className, method)) { Type[] methodTypes = method.getArgumentTypes(); Type[] superTypes = superClassMethod.getArgumentTypes(); if (methodTypes.length == superTypes.length) { boolean match = true; for (int arg = 0; arg < methodTypes.length; arg++) { if (!methodTypes[arg].equals(superTypes[arg])) { match = false; } } // Same method parameters if (match) { log(javaClass, 0, "hidden.static.method", new Object[] {method, superClassName}); } } } } } } } }
/** * Initialize with existing class. * * @param clazz JavaClass object (e.g. read from file) */ public ClassGen(JavaClass clazz) { class_name_index = clazz.getClassNameIndex(); superclass_name_index = clazz.getSuperclassNameIndex(); class_name = clazz.getClassName(); super_class_name = clazz.getSuperclassName(); file_name = clazz.getSourceFileName(); access_flags = clazz.getAccessFlags(); cp = new ConstantPoolGen(clazz.getConstantPool()); major = clazz.getMajor(); minor = clazz.getMinor(); Attribute[] attributes = clazz.getAttributes(); Method[] methods = clazz.getMethods(); Field[] fields = clazz.getFields(); String[] interfaces = clazz.getInterfaceNames(); for (int i = 0; i < interfaces.length; i++) addInterface(interfaces[i]); for (int i = 0; i < attributes.length; i++) addAttribute(attributes[i]); for (int i = 0; i < methods.length; i++) addMethod(methods[i]); for (int i = 0; i < fields.length; i++) addField(fields[i]); }
/** * Given another class, return a transformed version of the class which replaces specified calls * with alternative static implementations */ public byte[] transform( ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { // debug = className.equals ("chicory/Test"); String fullClassName = className.replace("/", "."); debug_transform.log("In Transform: class = %s%n", className); // Don't instrument boot classes. We only want to instrument // user classes classpath. // Most boot classes have the null loader, // but some generated classes (such as those in sun.reflect) will // have a non-null loader. Some of these have a null parent loader, // but some do not. The check for the sun.reflect package is a hack // to catch all of these. A more consistent mechanism to determine // boot classes would be preferrable. if (loader == null) { debug_transform.log("ignoring system class %s, class loader == null", fullClassName); return (null); } else if (loader.getParent() == null) { debug_transform.log("ignoring system class %s, parent loader == null\n", fullClassName); return (null); } else if (fullClassName.startsWith("sun.reflect")) { debug_transform.log("ignoring system class %s, in sun.reflect package", fullClassName); return (null); } else if (fullClassName.startsWith("com.sun")) { System.out.printf("Class from com.sun package %s with nonnull loaders\n", fullClassName); } // Don't intrument our code if (className.startsWith("randoop.")) { debug_transform.log("Not considering randoop class %s%n", fullClassName); return (null); } // Look for match with specified regular expressions for class method_map = null; debug_class = false; for (MethodMapInfo mmi : map_list) { if (mmi.class_regex.matcher(className).matches()) { if (false && className.startsWith("RandoopTest")) debug_class = true; if (debug_class) System.out.printf("Classname %s matches re %s%n", className, mmi.class_regex); method_map = mmi.map; break; } } if (method_map == null) return null; debug_transform.log( "transforming class %s, loader %s - %s%n", className, loader, loader.getParent()); // Parse the bytes of the classfile, die on any errors JavaClass c = null; ClassParser parser = new ClassParser(new ByteArrayInputStream(classfileBuffer), className); try { c = parser.parse(); } catch (Exception e) { throw new RuntimeException("Unexpected error", e); } try { // Get the class information ClassGen cg = new ClassGen(c); ifact = new InstructionFactory(cg); map_calls(cg, className, loader); JavaClass njc = cg.getJavaClass(); if (debug) njc.dump("/tmp/ret/" + njc.getClassName() + ".class"); if (true) { return (cg.getJavaClass().getBytes()); } else { debug_transform.log("not including class %s (filtered out)", className); return null; } } catch (Throwable e) { out.format("Unexpected error %s in transform", e); e.printStackTrace(); return (null); } }
/** * Adds all the constants found in the given class into the given ConstantSet, and returns it. * * @see #getConstants(String) */ public static ConstantSet getConstants(String classname, ConstantSet result) { ClassParser cp; JavaClass jc; try { String classfileBase = classname.replace('.', '/'); InputStream is = ClassPath.SYSTEM_CLASS_PATH.getInputStream(classfileBase, ".class"); cp = new ClassParser(is, classname); jc = cp.parse(); } catch (java.io.IOException e) { throw new Error("IOException while reading '" + classname + "': " + e.getMessage()); } result.classname = jc.getClassName(); // Get all of the constants from the pool ConstantPool constant_pool = jc.getConstantPool(); for (Constant c : constant_pool.getConstantPool()) { // System.out.printf ("*Constant = %s%n", c); if (c == null || c instanceof ConstantClass || c instanceof ConstantFieldref || c instanceof ConstantInterfaceMethodref || c instanceof ConstantMethodref || c instanceof ConstantNameAndType || c instanceof ConstantUtf8) { continue; } if (c instanceof ConstantString) { result.strings.add((String) ((ConstantString) c).getConstantValue(constant_pool)); } else if (c instanceof ConstantDouble) { result.doubles.add((Double) ((ConstantDouble) c).getConstantValue(constant_pool)); } else if (c instanceof ConstantFloat) { result.floats.add((Float) ((ConstantFloat) c).getConstantValue(constant_pool)); } else if (c instanceof ConstantInteger) { result.ints.add((Integer) ((ConstantInteger) c).getConstantValue(constant_pool)); } else if (c instanceof ConstantLong) { result.longs.add((Long) ((ConstantLong) c).getConstantValue(constant_pool)); } else { throw new RuntimeException("Unrecognized constant of type " + c.getClass() + ": " + c); } } ClassGen gen = new ClassGen(jc); ConstantPoolGen pool = gen.getConstantPool(); // Process the code in each method looking for literals for (Method m : jc.getMethods()) { MethodGen mg = new MethodGen(m, jc.getClassName(), pool); InstructionList il = mg.getInstructionList(); if (il == null) { // System.out.println("No instructions for " + mg); } else { for (Instruction inst : il.getInstructions()) { switch (inst.getOpcode()) { // Compare two objects, no literals case Constants.IF_ACMPEQ: case Constants.IF_ACMPNE: break; // These instructions compare the integer on the top of the stack // to zero. There are no literals here (except 0) case Constants.IFEQ: case Constants.IFNE: case Constants.IFLT: case Constants.IFGE: case Constants.IFGT: case Constants.IFLE: { break; } // Instanceof pushes either 0 or 1 on the stack depending on whether // the object on top of stack is of the specified type. // If were interested in class literals, this would be interesting case Constants.INSTANCEOF: break; // Duplicates the item on the top of stack. No literal. case Constants.DUP: { break; } // Duplicates the item on the top of the stack and inserts it 2 // values down in the stack. No literals case Constants.DUP_X1: { break; } // Duplicates either the top 2 category 1 values or a single // category 2 value and inserts it 2 or 3 values down on the // stack. case Constants.DUP2_X1: { break; } // Duplicate either one category 2 value or two category 1 values. case Constants.DUP2: { break; } // Dup the category 1 value on the top of the stack and insert it either // two or three values down on the stack. case Constants.DUP_X2: { break; } case Constants.DUP2_X2: { break; } // Pop instructions discard the top of the stack. case Constants.POP: { break; } // Pops either the top 2 category 1 values or a single category 2 value // from the top of the stack. case Constants.POP2: { break; } // Swaps the two category 1 types on the top of the stack. case Constants.SWAP: { break; } // Compares two integers on the stack case Constants.IF_ICMPEQ: case Constants.IF_ICMPGE: case Constants.IF_ICMPGT: case Constants.IF_ICMPLE: case Constants.IF_ICMPLT: case Constants.IF_ICMPNE: { break; } // Get the value of a field case Constants.GETFIELD: { break; } // stores the top of stack into a field case Constants.PUTFIELD: { break; } // Pushes the value of a static field on the stack case Constants.GETSTATIC: { break; } // Pops a value off of the stack into a static field case Constants.PUTSTATIC: { break; } // pushes a local onto the stack case Constants.DLOAD: case Constants.DLOAD_0: case Constants.DLOAD_1: case Constants.DLOAD_2: case Constants.DLOAD_3: case Constants.FLOAD: case Constants.FLOAD_0: case Constants.FLOAD_1: case Constants.FLOAD_2: case Constants.FLOAD_3: case Constants.ILOAD: case Constants.ILOAD_0: case Constants.ILOAD_1: case Constants.ILOAD_2: case Constants.ILOAD_3: case Constants.LLOAD: case Constants.LLOAD_0: case Constants.LLOAD_1: case Constants.LLOAD_2: case Constants.LLOAD_3: { break; } // Pops a value off of the stack into a local case Constants.DSTORE: case Constants.DSTORE_0: case Constants.DSTORE_1: case Constants.DSTORE_2: case Constants.DSTORE_3: case Constants.FSTORE: case Constants.FSTORE_0: case Constants.FSTORE_1: case Constants.FSTORE_2: case Constants.FSTORE_3: case Constants.ISTORE: case Constants.ISTORE_0: case Constants.ISTORE_1: case Constants.ISTORE_2: case Constants.ISTORE_3: case Constants.LSTORE: case Constants.LSTORE_0: case Constants.LSTORE_1: case Constants.LSTORE_2: case Constants.LSTORE_3: { break; } // Push a value from the runtime constant pool. We'll get these // values when processing the constant pool itself case Constants.LDC: case Constants.LDC_W: case Constants.LDC2_W: { break; } // Push the length of an array on the stack case Constants.ARRAYLENGTH: { break; } // Push small constants (-1..5) on the stack. These literals are // too common to bother mentioning case Constants.DCONST_0: case Constants.DCONST_1: case Constants.FCONST_0: case Constants.FCONST_1: case Constants.FCONST_2: case Constants.ICONST_0: case Constants.ICONST_1: case Constants.ICONST_2: case Constants.ICONST_3: case Constants.ICONST_4: case Constants.ICONST_5: case Constants.ICONST_M1: case Constants.LCONST_0: case Constants.LCONST_1: { break; } case Constants.BIPUSH: case Constants.SIPUSH: { ConstantPushInstruction cpi = (ConstantPushInstruction) inst; result.ints.add((Integer) cpi.getValue()); break; } // Primitive Binary operators. case Constants.DADD: case Constants.DCMPG: case Constants.DCMPL: case Constants.DDIV: case Constants.DMUL: case Constants.DREM: case Constants.DSUB: case Constants.FADD: case Constants.FCMPG: case Constants.FCMPL: case Constants.FDIV: case Constants.FMUL: case Constants.FREM: case Constants.FSUB: case Constants.IADD: case Constants.IAND: case Constants.IDIV: case Constants.IMUL: case Constants.IOR: case Constants.IREM: case Constants.ISHL: case Constants.ISHR: case Constants.ISUB: case Constants.IUSHR: case Constants.IXOR: case Constants.LADD: case Constants.LAND: case Constants.LCMP: case Constants.LDIV: case Constants.LMUL: case Constants.LOR: case Constants.LREM: case Constants.LSHL: case Constants.LSHR: case Constants.LSUB: case Constants.LUSHR: case Constants.LXOR: break; case Constants.LOOKUPSWITCH: case Constants.TABLESWITCH: break; case Constants.ANEWARRAY: case Constants.NEWARRAY: { break; } case Constants.MULTIANEWARRAY: { break; } // push the value at an index in an array case Constants.AALOAD: case Constants.BALOAD: case Constants.CALOAD: case Constants.DALOAD: case Constants.FALOAD: case Constants.IALOAD: case Constants.LALOAD: case Constants.SALOAD: { break; } // Pop the top of stack into an array location case Constants.AASTORE: case Constants.BASTORE: case Constants.CASTORE: case Constants.DASTORE: case Constants.FASTORE: case Constants.IASTORE: case Constants.LASTORE: case Constants.SASTORE: break; case Constants.ARETURN: case Constants.DRETURN: case Constants.FRETURN: case Constants.IRETURN: case Constants.LRETURN: case Constants.RETURN: { break; } // subroutine calls. case Constants.INVOKESTATIC: case Constants.INVOKEVIRTUAL: case Constants.INVOKESPECIAL: case Constants.INVOKEINTERFACE: break; // Throws an exception. case Constants.ATHROW: break; // Opcodes that don't need any modifications. Here for reference case Constants.ACONST_NULL: case Constants.ALOAD: case Constants.ALOAD_0: case Constants.ALOAD_1: case Constants.ALOAD_2: case Constants.ALOAD_3: case Constants.ASTORE: case Constants.ASTORE_0: case Constants.ASTORE_1: case Constants.ASTORE_2: case Constants.ASTORE_3: case Constants.CHECKCAST: case Constants.D2F: // double to float case Constants.D2I: // double to integer case Constants.D2L: // double to long case Constants.DNEG: // Negate double on top of stack case Constants.F2D: // float to double case Constants.F2I: // float to integer case Constants.F2L: // float to long case Constants.FNEG: // Negate float on top of stack case Constants.GOTO: case Constants.GOTO_W: case Constants.I2B: // integer to byte case Constants.I2C: // integer to char case Constants.I2D: // integer to double case Constants.I2F: // integer to float case Constants.I2L: // integer to long case Constants.I2S: // integer to short case Constants.IFNONNULL: case Constants.IFNULL: case Constants.IINC: // increment local variable by a constant case Constants.INEG: // negate integer on top of stack case Constants.JSR: // pushes return address on the stack, case Constants.JSR_W: case Constants.L2D: // long to double case Constants.L2F: // long to float case Constants.L2I: // long to int case Constants.LNEG: // negate long on top of stack case Constants.MONITORENTER: case Constants.MONITOREXIT: case Constants.NEW: case Constants.NOP: case Constants.RET: // this is the internal JSR return break; // Make sure we didn't miss anything default: throw new Error("instruction " + inst + " unsupported"); } } } } return result; }