/** * Generate code to invoke the Class.forName with the name of the given * class to get its Class object at runtime. The code is written to the * supplied stream. Note that the code generated by this method may caused * the checked ClassNotFoundException to be thrown. */ private void codeClassForName(CtClass cl, DataOutputStream out) throws IOException { if (cl.isArray()) { code_ldc(cp.getString(Descriptor.of(cl).replace("/", ".")), out); } else code_ldc(cp.getString(cl.getName()), out); out.writeByte(opc_invokestatic); out.writeShort(cp.getMethodRef("java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;")); }
/** * Return the "field type" string for the given type, appropriate for a * field descriptor, a parameter descriptor, or a return descriptor other * than "void". See JVMS section 4.3.2. */ private static String getFieldType(CtClass type) { if (type.isPrimitive()) { return PrimitiveTypeInfo.get(type).baseTypeString; } else if (type.isArray()) { /* * According to JLS 20.3.2, the getName() method on Class does * return the VM type descriptor format for array classes (only); * using that should be quicker than the otherwise obvious code: * * return "[" + getTypeDescriptor(type.getComponentType()); */ return Descriptor.of(type); } else { return "L" + dotToSlash(type.getName()) + ";"; } }