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) { clazz.accept(visitor); Field[] fields = clazz.getFields(); for (int i = 0; i < fields.length; i++) fields[i].accept(this); Method[] methods = clazz.getMethods(); for (int i = 0; i < methods.length; i++) methods[i].accept(this); Attribute[] attributes = clazz.getAttributes(); for (int i = 0; i < attributes.length; i++) attributes[i].accept(this); clazz.getConstantPool().accept(this); }
/** * 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); } }
/** * 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]); }
private static boolean hasStaticMembers(@NotNull JavaClass javaClass) { for (JavaMethod method : javaClass.getMethods()) { if (method.isStatic() && !shouldBeInEnumClassObject(method)) { return true; } } for (JavaField field : javaClass.getFields()) { if (field.isStatic() && !field.isEnumEntry()) { return true; } } for (JavaClass nestedClass : javaClass.getInnerClasses()) { if (SingleAbstractMethodUtils.isSamInterface(nestedClass)) { return true; } if (nestedClass.isStatic() && hasStaticMembers(nestedClass)) { return true; } } return false; }