/** * Adds a field. * * <p>The <code>CtField</code> belonging to another * <code>CtClass</code> cannot be directly added to this class. * Only a field created for this class can be added. * * @see javassist.CtField#CtField(CtField,CtClass) */ public void addField(CtField f) throws CannotCompileException { addField(f, (CtField.Initializer)null); }
/** * Adds a field. * * <p>The <code>CtField</code> belonging to another * <code>CtClass</code> cannot be directly added to this class. * Only a field created for this class can be added. * * @see javassist.CtField#CtField(CtField,CtClass) */ public void addField(CtField f) throws CannotCompileException { addField(f, (CtField.Initializer)null); }
private static void addConstants(Map<String, String> constants, CtClass cls) throws CannotCompileException { for (Map.Entry<String, String> expr : constants.entrySet()) { String fld = "private static final %s = (%s);"; String field = U.frmt(fld, expr.getKey(), expr.getValue()); cls.addField(CtField.make(field, cls)); } }
public static CtClass addPatchConstruct(CtClass patchClass, CtClass sourceClass) { try { CtField originField = new CtField(sourceClass, ORIGINCLASS, patchClass); patchClass.addField(originField); StringBuilder patchClassConstruct = new StringBuilder(); patchClassConstruct.append(" public Patch(Object o) {"); patchClassConstruct.append(ORIGINCLASS + "=(" + sourceClass.getName() + ")o;"); patchClassConstruct.append("}"); CtConstructor constructor = CtNewConstructor.make(patchClassConstruct.toString(), patchClass); patchClass.addConstructor(constructor); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(); } return patchClass; }
cls.addConstructor(CtNewConstructor.make("public " + method, cls)); } else if (FIELD_PATTERN.matcher(method).matches()) { cls.addField(CtField.make("private " + method, cls)); } else { cls.addMethod(CtNewMethod.make("public " + method, cls));
cls.addConstructor(CtNewConstructor.make("public " + method, cls)); } else if (FIELD_PATTERN.matcher(method).matches()) { cls.addField(CtField.make("private " + method, cls)); } else { cls.addMethod(CtNewMethod.make("public " + method, cls));
private static void addExpressions(Map<String, String> expressions, CtClass cls) throws CannotCompileException { for (Map.Entry<String, String> expr : expressions.entrySet()) { String fld = "private static final org.rapidoid.render.retriever.ValueRetriever %s = org.rapidoid.render.retriever.Retriever.of(%s);"; String retrieverId = retrieverId(expr.getKey()); String prop = expr.getValue(); String field = U.frmt(fld, retrieverId, prop); cls.addField(CtField.make(field, cls)); } }
CtField ctField = CtField.make("public " + type.getCanonicalName() + " " + fieldName + ";", pool.getCtClass(parameterClassName)); ctField.getFieldInfo().addAttribute(attribute); ctClass.addField(ctField);
CtField ctField = CtField.make("public " + type.getCanonicalName() + " " + fieldName + ";", pool.getCtClass(parameterClassName)); ctField.getFieldInfo().addAttribute(attribute); ctClass.addField(ctField);
mCtc.addField(CtField.make(code, mCtc));
mCtc.addField(CtField.make(code, mCtc));
private static void addWithModifiers(CtClass target, CtClass type, String name, int modifiers, Class<?> ... annotations ) { try { final CtField f = new CtField( type, name, target ); f.setModifiers( f.getModifiers() | modifiers ); addAnnotations( f.getFieldInfo(), annotations ); target.addField( f ); } catch (CannotCompileException cce) { final String msg = String.format( "Could not enhance class [%s] to add field [%s]", target.getName(), name ); throw new EnhancementException( msg, cce ); } }
private void ensureType(CtClass cc) throws NotFoundException, CannotCompileException { CtMethod ccms[] = cc.getDeclaredMethods(); if( !javassist.Modifier.isAbstract(cc.getModifiers()) && !hasExisting("frozenType", "()I", ccms) ) { // Build a simple field & method returning the type token cc.addField(new CtField(CtClass.intType, "_frozen$type", cc)); cc.addMethod(CtNewMethod.make("public int frozenType() {" + " return _frozen$type == 0 ? (_frozen$type=water.TypeMap.onIce(\""+cc.getName()+"\")) : _frozen$type;" + "}",cc)); } }
/** * Adds serialVersionUID if one does not already exist. Call this before * modifying a class to maintain serialization compatability. */ public static void setSerialVersionUID(CtClass clazz) throws CannotCompileException, NotFoundException { // check for pre-existing field. try { clazz.getDeclaredField("serialVersionUID"); return; } catch (NotFoundException e) {} // check if the class is serializable. if (!isSerializable(clazz)) return; // add field with default value. CtField field = new CtField(CtClass.longType, "serialVersionUID", clazz); field.setModifiers(Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL); clazz.addField(field, calculateDefault(clazz) + "L"); }
@SneakyThrows public Proxy<I> addField(String code, Class<? extends java.lang.annotation.Annotation> annotation, Map<String, Object> annotationProperties) { return handleException(() -> { CtField ctField = CtField.make(code, ctClass); if (null != annotation) { ConstPool constPool = ctClass.getClassFile().getConstPool(); AnnotationsAttribute attributeInfo = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); Annotation ann = new javassist.bytecode.annotation.Annotation(annotation.getName(), constPool); if (null != annotationProperties) { annotationProperties.forEach((key, value) -> { MemberValue memberValue = createMemberValue(value, constPool); if (memberValue != null) { ann.addMemberValue(key, memberValue); } }); } attributeInfo.addAnnotation(ann); ctField.getFieldInfo().addAttribute(attributeInfo); } ctClass.addField(ctField); }); }
/** * Adds serialVersionUID if one does not already exist. Call this before * modifying a class to maintain serialization compatability. */ public static void setSerialVersionUID(CtClass clazz) throws CannotCompileException, NotFoundException { // check for pre-existing field. try { clazz.getDeclaredField("serialVersionUID"); return; } catch (NotFoundException e) {} // check if the class is serializable. if (!isSerializable(clazz)) return; // add field with default value. CtField field = new CtField(CtClass.longType, "serialVersionUID", clazz); field.setModifiers(Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL); clazz.addField(field, calculateDefault(clazz) + "L"); }
CtField field = new CtField(type, fname, cc); field.setModifiers(Modifier.PUBLIC | Modifier.STATIC); cc.addField(field, CtField.Initializer.byNew(type)); insertBefore(fname + ".enter();", false); String src = fname + ".exit();";
/** * 如果当前属性不存在先创建属性以及setter/getter方法和注解。 如果已经属性返回当前属性 */ @Override public DynamicField field(String fieldName, CtClass fieldType) { try { clazz.getField(fieldName); } catch (NotFoundException e) { try { CtField f = new CtField(fieldType, fieldName, clazz); clazz.addField(f); getter(fieldName, f); setter(fieldName, f); } catch (CannotCompileException ex) { ex.printStackTrace(); } } return new JavassistDynamicField(this, clazz, cpool, fieldName); }