Refine search
throws CannotCompileException this(src.fieldInfo.getDescriptor(), src.fieldInfo.getName(), declaring); java.util.ListIterator iterator = src.fieldInfo.getAttributes().listIterator(); FieldInfo fi = fieldInfo; fi.setAccessFlags(src.fieldInfo.getAccessFlags()); ConstPool cp = fi.getConstPool(); while (iterator.hasNext()) { AttributeInfo ainfo = (AttributeInfo)iterator.next(); fi.addAttribute(ainfo.copy(cp, null));
private Object[] getAnnotations(boolean ignoreNotFound) throws ClassNotFoundException { FieldInfo fi = getFieldInfo2(); AnnotationsAttribute ainfo = (AnnotationsAttribute) fi.getAttribute(AnnotationsAttribute.invisibleTag); AnnotationsAttribute ainfo2 = (AnnotationsAttribute) fi.getAttribute(AnnotationsAttribute.visibleTag); return CtClassType.toAnnotationType(ignoreNotFound, getDeclaringClass().getClassPool(), ainfo, ainfo2); }
private CtField(String typeDesc, String name, CtClass clazz) throws CannotCompileException { super(clazz); ClassFile cf = clazz.getClassFile2(); if (cf == null) throw new CannotCompileException("bad declaring class: " + clazz.getName()); fieldInfo = new FieldInfo(cf.getConstPool(), name, typeDesc); }
/** * Returns a string representation of the object. */ public String toString() { return getName() + " " + getDescriptor(); }
/** * Appends a field to the class. * * @throws DuplicateMemberException when the field is already included. */ public void addField(FieldInfo finfo) throws DuplicateMemberException { testExistingField(finfo.getName(), finfo.getDescriptor()); fields.add(finfo); }
/** * Set the generic signature of the field. * It represents a type including type variables. * See {@link javassist.CtClass#setGenericSignature(String)} * for a code sample. * * @param sig a new generic signature. * @see javassist.bytecode.SignatureAttribute.ObjectType#encode() * @since 3.17 */ public void setGenericSignature(String sig) { declaringClass.checkModify(); fieldInfo.addAttribute(new SignatureAttribute(fieldInfo.getConstPool(), sig)); }
ByteArrayOutputStream bout = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(bout); ClassFile classFile = clazz.getClassFile(); out.writeUTF(javaName); CtMethod[] methods = clazz.getDeclaredMethods(); int classMods = clazz.getModifiers(); if ((classMods & Modifier.INTERFACE) != 0) if (methods.length > 0) String[] interfaces = classFile.getInterfaces(); for (int i = 0; i < interfaces.length; i++) interfaces[i] = javaName(interfaces[i]); int mods = field.getModifiers(); if (((mods & Modifier.PRIVATE) == 0) || ((mods & (Modifier.STATIC | Modifier.TRANSIENT)) == 0)) { out.writeUTF(field.getName()); out.writeInt(mods); out.writeUTF(field.getFieldInfo2().getDescriptor()); if (classFile.getStaticInitializer() != null) { out.writeUTF("<clinit>"); out.writeInt(Modifier.STATIC);
private CtMethod addHasParent(CtClass cc, CtField parentField) throws CannotCompileException { FieldInfo parentFieldInfo = parentField.getFieldInfo2(); String desc = "()" + Descriptor.of("boolean"); ConstPool cp = parentFieldInfo.getConstPool(); MethodInfo minfo = new MethodInfo(cp, "$$_hasParent", desc); minfo.setAccessFlags(AccessFlag.PUBLIC); String parentFieldName = parentFieldInfo.getName(); StringBuilder sb = new StringBuilder(); sb.append("{\n"); sb.append("\treturn $0.").append(parentFieldName).append(" != null;\n"); sb.append('}'); CtMethod method = CtMethod.make(minfo, cc); method.setBody(sb.toString()); cc.addMethod(method); return method; }
ClassPool pool = ClassGenerator.getClassPool(clazz.getClassLoader()); CtClass ctClass = pool.makeClass(parameterClassName); ClassFile classFile = ctClass.getClassFile(); classFile.setVersionToJava5(); ctClass.addConstructor(CtNewConstructor.defaultConstructor(pool.getCtClass(parameterClassName))); Class<?> type = parameterTypes[i]; Annotation[] annotations = parameterAnnotations[i]; AnnotationsAttribute attribute = new AnnotationsAttribute(classFile.getConstPool(), AnnotationsAttribute.visibleTag); for (Annotation annotation : annotations) { if (annotation.annotationType().isAnnotationPresent(Constraint.class)) { javassist.bytecode.annotation.Annotation ja = new javassist.bytecode.annotation.Annotation( classFile.getConstPool(), pool.getCtClass(annotation.annotationType().getName())); Method[] members = annotation.annotationType().getMethods(); for (Method member : members) { CtField ctField = CtField.make("public " + type.getCanonicalName() + " " + fieldName + ";", pool.getCtClass(parameterClassName)); ctField.getFieldInfo().addAttribute(attribute); ctClass.addField(ctField); parameterClass = ctClass.toClass(clazz.getClassLoader(), null);
throws CompileError String fieldName = finfo.getName(); String key = fieldName + ":getter"; Object res = accessors.get(key); return (MethodInfo)res; // already exists. ClassFile cf = clazz.getClassFile(); // turn on the modified flag. String accName = findAccessorName(cf); try { ConstPool cp = cf.getConstPool(); ClassPool pool = clazz.getClassPool(); String fieldType = finfo.getDescriptor(); String accDesc; if (is_static) accDesc = "()" + fieldType; else accDesc = "(" + Descriptor.of(clazz) + ")" + fieldType; MethodInfo minfo = new MethodInfo(cp, accName, accDesc); minfo.setAccessFlags(AccessFlag.STATIC); minfo.addAttribute(new SyntheticAttribute(cp)); Bytecode code = new Bytecode(cp); if (is_static) {
clazz.defrost(); CtField[] fieldsToCopy = template.getDeclaredFields(); for (CtField field : fieldsToCopy) { if (field.hasAnnotation(NonCopied.class)) { logger.debug(String.format("Not copying annotation from field [%s]", field.getName())); } else { logger.debug(String.format("Copying annotation from field [%s]", field.getName())); ConstPool constPool = clazz.getClassFile().getConstPool(); CtField fieldFromMainClass = clazz.getField(field.getName()); for (Object o : field.getFieldInfo().getAttributes()) { if (o instanceof AnnotationsAttribute) { AnnotationsAttribute templateAnnotations = (AnnotationsAttribute) o; for (Object attribute : fieldFromMainClass.getFieldInfo().getAttributes()) { if (attribute instanceof AnnotationsAttribute) { for (Annotation annotation : copied.getAnnotations()) {
@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); }); }
= AccessFlag.toModifier(cf.getAccessFlags() & ~AccessFlag.SYNCHRONIZED); out.println("major: " + cf.major + ", minor: " + cf.minor + " modifiers: " + Integer.toHexString(cf.getAccessFlags())); out.println(Modifier.toString(mod) + " class " + cf.getName() + " extends " + cf.getSuperclass()); String[] infs = cf.getInterfaces(); for (int i = 0; i < n; ++i) { FieldInfo finfo = (FieldInfo)list.get(i); int acc = finfo.getAccessFlags(); out.println(Modifier.toString(AccessFlag.toModifier(acc)) + " " + finfo.getName() + "\t" + finfo.getDescriptor()); printAttributes(finfo.getAttributes(), out, 'f'); for (int i = 0; i < n; ++i) { MethodInfo minfo = (MethodInfo)list.get(i); int acc = minfo.getAccessFlags(); out.println(Modifier.toString(AccessFlag.toModifier(acc)) + " " + minfo.getName() + "\t" + minfo.getDescriptor()); printAttributes(minfo.getAttributes(), out, 'm'); out.println();
throws CannotCompileException FieldInfo finfo = field.getFieldInfo2(); String fieldType = finfo.getDescriptor(); String desc = "()" + fieldType; ConstPool cp = finfo.getConstPool(); MethodInfo minfo = new MethodInfo(cp, methodName, desc); minfo.setAccessFlags(AccessFlag.PUBLIC); String fieldName = finfo.getName(); if ((finfo.getAccessFlags() & AccessFlag.STATIC) == 0) { code.addAload(0); code.addGetfield(Bytecode.THIS, fieldName, fieldType); code.addGetstatic(Bytecode.THIS, fieldName, fieldType); code.addReturn(field.getType()); minfo.setCodeAttribute(code.toCodeAttribute()); CtClass cc = field.getDeclaringClass(); return new CtMethod(minfo, cc);
clazz.defrost(); ClassFile classFile = clazz.getClassFile(); ConstPool constantPool = classFile.getConstPool(); List<?> attributes = classFile.getAttributes(); AnnotationsAttribute annotationsAttribute = stripAnnotation(constantPool, attributes); classFile.addAttribute(annotationsAttribute); List<FieldInfo> fieldInfos = classFile.getFields(); for (FieldInfo myField : fieldInfos) { List<?> attributes = myField.getAttributes(); AnnotationsAttribute annotationsAttribute = stripAnnotation(constantPool, attributes); myField.addAttribute(annotationsAttribute); return clazz.toBytecode();
/** * Returns null if the field is accessible. Otherwise, it throws * an exception or it returns AccessorMaker if the field is a private * one declared in an enclosing class. */ private AccessorMaker isAccessibleField(CtField f, FieldInfo finfo) throws CompileError { if (AccessFlag.isPrivate(finfo.getAccessFlags()) && f.getDeclaringClass() != thisClass) { CtClass declClass = f.getDeclaringClass(); if (isEnclosing(declClass, thisClass)) { AccessorMaker maker = declClass.getAccessorMaker(); if (maker != null) return maker; else throw new CompileError("fatal error. bug?"); } else throw new CompileError("Field " + f.getName() + " in " + declClass.getName() + " is private."); } return null; // accessible field }
/** * Returns a String representation of the object. */ public String toString() { return getDeclaringClass().getName() + "." + getName() + ":" + fieldInfo.getDescriptor(); }
private int addFieldrefInfo(CtField f, FieldInfo finfo) { ConstPool cp = bytecode.getConstPool(); String cname = f.getDeclaringClass().getName(); int ci = cp.addClassInfo(cname); String name = finfo.getName(); String type = finfo.getDescriptor(); return cp.addFieldrefInfo(ci, name, type); }
/** Adds a new field of type double to the customized class */ public void addDoubleField(String fieldName) { // FIXME: this should support default values but does not ClassFile classFile = ctClass.getClassFile(); ConstPool constPool = classFile.getConstPool(); try { // add field FieldInfo fieldInfo = new FieldInfo(constPool, fieldName, "D"); classFile.addField(fieldInfo); CtConstructor ctor = CtNewConstructor.defaultConstructor(ctClass); ctClass.addConstructor(ctor); addDoubleSetter(classFile, fieldName); addDoubleGetter(classFile, fieldName); } catch (Exception e) { throw new RuntimeException(e); } }
private static String inferFieldTypeName(CtField field) { try { if ( field.getFieldInfo2().getAttribute( SignatureAttribute.tag ) == null ) { return field.getType().getName(); } return inferGenericTypeName( field.getType(), SignatureAttribute.toTypeSignature( field.getGenericSignature() ) ); } catch (BadBytecode ignore) { return null; } catch (NotFoundException e) { return null; } }