/** * Creates a public setter method. The setter method assigns the value of the first parameter to * the specified field in the class to which this method is added. The created method is not * static even if the field is static. You may not change it to be static by <code>setModifiers() * </code> in <code>CtBehavior</code>. * * @param methodName the name of the setter * @param field the field accessed. */ public static CtMethod setter(String methodName, CtField field) throws CannotCompileException { FieldInfo finfo = field.getFieldInfo2(); String fieldType = finfo.getDescriptor(); String desc = "(" + fieldType + ")V"; ConstPool cp = finfo.getConstPool(); MethodInfo minfo = new MethodInfo(cp, methodName, desc); minfo.setAccessFlags(AccessFlag.PUBLIC); Bytecode code = new Bytecode(cp, 3, 3); try { String fieldName = finfo.getName(); if ((finfo.getAccessFlags() & AccessFlag.STATIC) == 0) { code.addAload(0); code.addLoad(1, field.getType()); code.addPutfield(Bytecode.THIS, fieldName, fieldType); } else { code.addLoad(1, field.getType()); code.addPutstatic(Bytecode.THIS, fieldName, fieldType); } code.addReturn(null); } catch (NotFoundException e) { throw new CannotCompileException(e); } minfo.setCodeAttribute(code.toCodeAttribute()); return new CtMethod(minfo, field.getDeclaringClass()); }
public String apply(StaticFieldDescriptor from) { // this is quite simple. First we create a proxy String proxyName = ProxyDefinitionStore.getProxyName(); ClassFile proxy = new ClassFile(false, proxyName, "java.lang.Object"); ClassDataStore.instance().registerProxyName(from.getClazz(), proxyName); proxy.setAccessFlags(AccessFlag.PUBLIC); FieldInfo newField = new FieldInfo(proxy.getConstPool(), from.getName(), from.getDescriptor()); newField.setAccessFlags(AccessFlag.PUBLIC | AccessFlag.STATIC); if (from.getSigniture() != null) { SignatureAttribute sig = new SignatureAttribute(proxy.getConstPool(), from.getSigniture()); newField.addAttribute(sig); } try { proxy.addField(newField); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bytes); try { proxy.write(dos); } catch (IOException e) { throw new RuntimeException(e); } ProxyDefinitionStore.saveProxyDefinition( from.getClazz().getClassLoader(), proxyName, bytes.toByteArray()); } catch (DuplicateMemberException e) { // can't happen } return proxyName; }
@Override protected void doInjection(String value) { final FieldInfo ctFieldInfo = ctField.getFieldInfo(); ctFieldInfo.addAttribute( new ConstantAttribute( ctFieldInfo.getConstPool(), ctFieldInfo.getConstPool().addStringInfo(value))); }
public BaseClassData(ClassFile file, ClassLoader loader, boolean replaceable) { className = file.getName(); this.replaceable = replaceable; internalName = Descriptor.toJvmName(file.getName()); this.loader = loader; superClassName = file.getSuperclass(); boolean finalMethod = false; Set<MethodData> meths = new HashSet<MethodData>(); for (Object o : file.getMethods()) { String methodClassName = className; MethodInfo m = (MethodInfo) o; MemberType type = MemberType.NORMAL; if ((m.getDescriptor().equals(Constants.ADDED_METHOD_DESCRIPTOR) && m.getName().equals(Constants.ADDED_METHOD_NAME)) || (m.getDescriptor().equals(Constants.ADDED_STATIC_METHOD_DESCRIPTOR) && m.getName().equals(Constants.ADDED_STATIC_METHOD_NAME)) || (m.getDescriptor().equals(Constants.ADDED_CONSTRUCTOR_DESCRIPTOR))) { type = MemberType.ADDED_SYSTEM; } else if (m.getAttribute(Constants.FINAL_METHOD_ATTRIBUTE) != null) { finalMethod = true; } MethodData md = new MethodData( m.getName(), m.getDescriptor(), methodClassName, type, m.getAccessFlags(), finalMethod); meths.add(md); } this.methods = Collections.unmodifiableSet(meths); Set<FieldData> fieldData = new HashSet<FieldData>(); for (Object o : file.getFields()) { FieldInfo m = (FieldInfo) o; MemberType mt = MemberType.NORMAL; fieldData.add(new FieldData(m, mt, className, m.getAccessFlags())); } this.fields = Collections.unmodifiableSet(fieldData); }