private void getter(String fieldName, CtField field) { try { CtMethod m = CtNewMethod.getter("get" + StringUtils.capitalize(fieldName), field); clazz.addMethod(m); } catch (Exception ex) { ex.printStackTrace(); } }
public static CtMethod addGetter(CtClass target, String field, String name) { CtField actualField = null; try { actualField = target.getField( field ); log.debugf( "Writing getter method [%s] into [%s] for field [%s]", name, target.getName(), field ); CtMethod method = CtNewMethod.getter( name, target.getField( field ) ); target.addMethod( method ); return method; } catch (CannotCompileException cce) { try { // Fall back to create a getter from delegation. CtMethod method = CtNewMethod.delegator( CtNewMethod.getter( name, actualField ), target ); target.addMethod( method ); return method; } catch (CannotCompileException ignored) { String msg = String.format( "Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field ); throw new EnhancementException( msg, cce ); } } catch (NotFoundException nfe) { String msg = String.format( "Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field ); throw new EnhancementException( msg, nfe ); } }
private boolean modifyClassfile(CtClass clazz, CtClass metaobject, CtClass metaclass) throws CannotCompileException, NotFoundException { if (clazz.getAttribute("Reflective") != null) return false; // this is already reflective. clazz.setAttribute("Reflective", new byte[0]); CtClass mlevel = classPool.get("javassist.tools.reflect.Metalevel"); boolean addMeta = !clazz.subtypeOf(mlevel); if (addMeta) clazz.addInterface(mlevel); processMethods(clazz, addMeta); processFields(clazz); CtField f; if (addMeta) { f = new CtField(classPool.get("javassist.tools.reflect.Metaobject"), metaobjectField, clazz); f.setModifiers(Modifier.PROTECTED); clazz.addField(f, CtField.Initializer.byNewWithParams(metaobject)); clazz.addMethod(CtNewMethod.getter(metaobjectGetter, f)); clazz.addMethod(CtNewMethod.setter(metaobjectSetter, f)); } f = new CtField(classPool.get("javassist.tools.reflect.ClassMetaobject"), classobjectField, clazz); f.setModifiers(Modifier.PRIVATE | Modifier.STATIC); clazz.addField(f, CtField.Initializer.byNew(metaclass, new String[] { clazz.getName() })); clazz.addMethod(CtNewMethod.getter(classobjectAccessor, f)); return true; }
proxy.addField(f, CtField.Initializer.byParameter(1)); proxy.addMethod(CtNewMethod.getter(accessorObjectId, f));
proxy.addField(f, CtField.Initializer.byParameter(1)); proxy.addMethod(CtNewMethod.getter(accessorObjectId, f));
private CtMethod createGetterMethod(CtField field) throws CannotCompileException { String fn = field.getName(); return CtNewMethod.getter(this.naming.createGetterName(fn), field); }
public static CtMethod addGetter(CtClass target, String field, String name) { CtField actualField = null; try { actualField = target.getField( field ); log.debugf( "Writing getter method [%s] into [%s] for field [%s]", name, target.getName(), field ); CtMethod method = CtNewMethod.getter( name, target.getField( field ) ); target.addMethod( method ); return method; } catch (CannotCompileException cce) { try { // Fall back to create a getter from delegation. CtMethod method = CtNewMethod.delegator( CtNewMethod.getter( name, actualField ), target ); target.addMethod( method ); return method; } catch (CannotCompileException ignored) { String msg = String.format( "Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field ); throw new EnhancementException( msg, cce ); } } catch (NotFoundException nfe) { String msg = String.format( "Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field ); throw new EnhancementException( msg, nfe ); } }
/** * Generates a getter for the field (get<FieldName>) and adds it to the declaring class. * * @param field * @param interceptorAccess * @return * @throws NotFoundException * @throws CannotCompileException */ public static CtMethod generateGetter(CtField field, String interceptorAccess) throws NotFoundException, CannotCompileException { String getterName = "get" + capitalize(field.getName()); CtMethod getter = CtNewMethod.getter( getterName, field ); if(interceptorAccess != null) getter.setBody( interceptorAccess + "." + getterName + "($$);" ); field.getDeclaringClass().addMethod(getter); return getter; }
proxy.addField(f, CtField.Initializer.byParameter(1)); proxy.addMethod(CtNewMethod.getter(accessorObjectId, f));
proxy.addField(f, CtField.Initializer.byParameter(1)); proxy.addMethod(CtNewMethod.getter(accessorObjectId, f));
proxy.addField(f, CtField.Initializer.byParameter(1)); proxy.addMethod(CtNewMethod.getter(accessorObjectId, f));
proxy.addField(f, CtField.Initializer.byParameter(1)); proxy.addMethod(CtNewMethod.getter(accessorObjectId, f));
proxy.addField(f, CtField.Initializer.byParameter(1)); proxy.addMethod(CtNewMethod.getter(accessorObjectId, f));