Search in sources :

Example 1 with AnnotationMemberValue

use of javassist.bytecode.annotation.AnnotationMemberValue in project fakereplace by fakereplace.

the class AnnotationBuilder method createMemberValue.

private static MemberValue createMemberValue(Class<?> type, Object val, ConstPool cp) {
    if (type == int.class) {
        return new IntegerMemberValue(cp, (Integer) val);
    } else if (type == short.class) {
        return new ShortMemberValue((Short) val, cp);
    } else if (type == long.class) {
        return new LongMemberValue((Long) val, cp);
    } else if (type == byte.class) {
        return new ByteMemberValue((Byte) val, cp);
    } else if (type == float.class) {
        return new FloatMemberValue((Float) val, cp);
    } else if (type == double.class) {
        return new DoubleMemberValue((Double) val, cp);
    } else if (type == char.class) {
        return new CharMemberValue((Character) val, cp);
    } else if (type == boolean.class) {
        return new BooleanMemberValue((Boolean) val, cp);
    } else if (type == String.class) {
        return new StringMemberValue((String) val, cp);
    } else if (type == Class.class) {
        return new ClassMemberValue(((Class<?>) val).getName(), cp);
    } else if (type.isEnum()) {
        EnumMemberValue e = new EnumMemberValue(cp);
        e.setType(type.getName());
        e.setValue(((Enum<?>) val).name());
        return e;
    } else if (type.isAnnotation()) {
        return new AnnotationMemberValue(createJavassistAnnotation((java.lang.annotation.Annotation) val, cp), cp);
    } else if (type.isArray()) {
        Class<?> arrayType = type.getComponentType();
        int length = Array.getLength(val);
        MemberValue arrayval = createEmptyMemberValue(arrayType, cp);
        ArrayMemberValue ret = new ArrayMemberValue(arrayval, cp);
        MemberValue[] vals = new MemberValue[length];
        for (int i = 0; i < length; ++i) {
            vals[i] = createMemberValue(arrayType, Array.get(val, i), cp);
        }
        ret.setValue(vals);
        return ret;
    }
    throw new RuntimeException("Invalid array type " + type + " value: " + val);
}
Also used : EnumMemberValue(javassist.bytecode.annotation.EnumMemberValue) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) CharMemberValue(javassist.bytecode.annotation.CharMemberValue) DoubleMemberValue(javassist.bytecode.annotation.DoubleMemberValue) LongMemberValue(javassist.bytecode.annotation.LongMemberValue) IntegerMemberValue(javassist.bytecode.annotation.IntegerMemberValue) ByteMemberValue(javassist.bytecode.annotation.ByteMemberValue) AnnotationMemberValue(javassist.bytecode.annotation.AnnotationMemberValue) BooleanMemberValue(javassist.bytecode.annotation.BooleanMemberValue) ShortMemberValue(javassist.bytecode.annotation.ShortMemberValue) ClassMemberValue(javassist.bytecode.annotation.ClassMemberValue) FloatMemberValue(javassist.bytecode.annotation.FloatMemberValue) MemberValue(javassist.bytecode.annotation.MemberValue) AnnotationMemberValue(javassist.bytecode.annotation.AnnotationMemberValue) ArrayMemberValue(javassist.bytecode.annotation.ArrayMemberValue) DoubleMemberValue(javassist.bytecode.annotation.DoubleMemberValue) ShortMemberValue(javassist.bytecode.annotation.ShortMemberValue) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) IntegerMemberValue(javassist.bytecode.annotation.IntegerMemberValue) LongMemberValue(javassist.bytecode.annotation.LongMemberValue) ClassMemberValue(javassist.bytecode.annotation.ClassMemberValue) CharMemberValue(javassist.bytecode.annotation.CharMemberValue) EnumMemberValue(javassist.bytecode.annotation.EnumMemberValue) BooleanMemberValue(javassist.bytecode.annotation.BooleanMemberValue) ByteMemberValue(javassist.bytecode.annotation.ByteMemberValue) FloatMemberValue(javassist.bytecode.annotation.FloatMemberValue) ArrayMemberValue(javassist.bytecode.annotation.ArrayMemberValue)

Example 2 with AnnotationMemberValue

use of javassist.bytecode.annotation.AnnotationMemberValue in project BroadleafCommerce by BroadleafCommerce.

the class QueryConfigurationClassTransformer method prepareNativeQueries.

/**
 * Prepare the {@link NamedNativeQueries} declaration
 *
 * @param constantPool
 * @param pool
 * @param queryArray
 * @throws NotFoundException
 */
protected void prepareNativeQueries(ConstPool constantPool, ClassPool pool, ArrayMemberValue queryArray) throws NotFoundException {
    List<MemberValue> values;
    if (queryArray.getValue() != null) {
        values = new ArrayList<MemberValue>(Arrays.asList(queryArray.getValue()));
    } else {
        values = new ArrayList<MemberValue>();
    }
    for (NamedNativeQuery query : nativeQueries) {
        Annotation namedQuery = new Annotation(NamedNativeQuery.class.getName(), constantPool);
        StringMemberValue name = new StringMemberValue(constantPool);
        name.setValue(query.name());
        namedQuery.addMemberValue("name", name);
        StringMemberValue queryString = new StringMemberValue(constantPool);
        queryString.setValue(query.query());
        namedQuery.addMemberValue("query", queryString);
        ClassMemberValue resultClass = new ClassMemberValue(constantPool);
        resultClass.setValue(query.resultClass().getName());
        namedQuery.addMemberValue("resultClass", resultClass);
        StringMemberValue resultSetMapping = new StringMemberValue(constantPool);
        resultSetMapping.setValue(query.resultSetMapping());
        namedQuery.addMemberValue("resultSetMapping", resultSetMapping);
        List<AnnotationMemberValue> queryHints = new ArrayList<AnnotationMemberValue>();
        for (QueryHint hint : query.hints()) {
            prepareQueryHints(constantPool, queryHints, hint);
        }
        ArrayMemberValue hintArray = new ArrayMemberValue(constantPool);
        hintArray.setValue(queryHints.toArray(new AnnotationMemberValue[queryHints.size()]));
        namedQuery.addMemberValue("hints", hintArray);
        AnnotationMemberValue queryAnnotation = new AnnotationMemberValue(namedQuery, constantPool);
        values.add(queryAnnotation);
    }
    queryArray.setValue(values.toArray(new MemberValue[values.size()]));
}
Also used : MemberValue(javassist.bytecode.annotation.MemberValue) AnnotationMemberValue(javassist.bytecode.annotation.AnnotationMemberValue) ArrayMemberValue(javassist.bytecode.annotation.ArrayMemberValue) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) ClassMemberValue(javassist.bytecode.annotation.ClassMemberValue) EnumMemberValue(javassist.bytecode.annotation.EnumMemberValue) NamedNativeQuery(javax.persistence.NamedNativeQuery) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) QueryHint(javax.persistence.QueryHint) AnnotationMemberValue(javassist.bytecode.annotation.AnnotationMemberValue) ArrayList(java.util.ArrayList) Annotation(javassist.bytecode.annotation.Annotation) ClassMemberValue(javassist.bytecode.annotation.ClassMemberValue) ArrayMemberValue(javassist.bytecode.annotation.ArrayMemberValue)

Example 3 with AnnotationMemberValue

use of javassist.bytecode.annotation.AnnotationMemberValue in project BroadleafCommerce by BroadleafCommerce.

the class DirectCopyClassTransformer method reviewDirectCopyTransformAnnotations.

/**
 * Retrieves {@link DirectCopyTransformTypes} that are placed as annotations on classes.
 * @param clazz
 * @param mySkipOverlaps
 * @param myRenameMethodOverlaps
 * @param matchedPatterns
 * @return
 */
protected XFormParams reviewDirectCopyTransformAnnotations(CtClass clazz, boolean mySkipOverlaps, boolean myRenameMethodOverlaps, List<DirectCopyIgnorePattern> matchedPatterns) {
    List<?> attributes = clazz.getClassFile().getAttributes();
    Iterator<?> itr = attributes.iterator();
    List<String> templates = new ArrayList<>();
    List<Boolean> skips = new ArrayList<>();
    List<Boolean> renames = new ArrayList<>();
    XFormParams response = new XFormParams();
    check: {
        while (itr.hasNext()) {
            Object object = itr.next();
            if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) {
                AnnotationsAttribute attr = (AnnotationsAttribute) object;
                Annotation[] items = attr.getAnnotations();
                for (Annotation annotation : items) {
                    String typeName = annotation.getTypeName();
                    if (typeName.equals(DirectCopyTransform.class.getName())) {
                        ArrayMemberValue arrayMember = (ArrayMemberValue) annotation.getMemberValue("value");
                        for (MemberValue arrayMemberValue : arrayMember.getValue()) {
                            AnnotationMemberValue member = (AnnotationMemberValue) arrayMemberValue;
                            Annotation memberAnnot = member.getValue();
                            ArrayMemberValue annot = (ArrayMemberValue) memberAnnot.getMemberValue("templateTokens");
                            List<String> addedTemplates = new ArrayList<>();
                            for (MemberValue memberValue : annot.getValue()) {
                                String val = ((StringMemberValue) memberValue).getValue();
                                addedTemplates.addAll(reviewTemplateTokens(matchedPatterns, val));
                            }
                            templates.addAll(addedTemplates);
                            BooleanMemberValue skipAnnot = (BooleanMemberValue) memberAnnot.getMemberValue("skipOverlaps");
                            if (skipAnnot != null) {
                                for (int j = 0; j < addedTemplates.size(); j++) {
                                    skips.add(skipAnnot.getValue());
                                }
                            } else {
                                for (int j = 0; j < addedTemplates.size(); j++) {
                                    skips.add(mySkipOverlaps);
                                }
                            }
                            BooleanMemberValue renameAnnot = (BooleanMemberValue) memberAnnot.getMemberValue("renameMethodOverlaps");
                            if (renameAnnot != null) {
                                for (int j = 0; j < addedTemplates.size(); j++) {
                                    renames.add(renameAnnot.getValue());
                                }
                            } else {
                                for (int j = 0; j < addedTemplates.size(); j++) {
                                    renames.add(myRenameMethodOverlaps);
                                }
                            }
                        }
                        response.setXformVals(templates.toArray(new String[templates.size()]));
                        response.setXformSkipOverlaps(skips.toArray(new Boolean[skips.size()]));
                        response.setXformRenameMethodOverlaps(renames.toArray(new Boolean[renames.size()]));
                        break check;
                    }
                }
            }
        }
    }
    return response;
}
Also used : BooleanMemberValue(javassist.bytecode.annotation.BooleanMemberValue) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) ArrayList(java.util.ArrayList) Annotation(javassist.bytecode.annotation.Annotation) MemberValue(javassist.bytecode.annotation.MemberValue) AnnotationMemberValue(javassist.bytecode.annotation.AnnotationMemberValue) ArrayMemberValue(javassist.bytecode.annotation.ArrayMemberValue) BooleanMemberValue(javassist.bytecode.annotation.BooleanMemberValue) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) AnnotationMemberValue(javassist.bytecode.annotation.AnnotationMemberValue) ArrayMemberValue(javassist.bytecode.annotation.ArrayMemberValue)

Example 4 with AnnotationMemberValue

use of javassist.bytecode.annotation.AnnotationMemberValue in project fakereplace by fakereplace.

the class AnnotationBuilder method createEmptyMemberValue.

private static MemberValue createEmptyMemberValue(Class<?> type, ConstPool cp) {
    if (type == int.class) {
        return new IntegerMemberValue(cp);
    } else if (type == short.class) {
        return new ShortMemberValue(cp);
    } else if (type == long.class) {
        return new LongMemberValue(cp);
    } else if (type == byte.class) {
        return new ByteMemberValue(cp);
    } else if (type == float.class) {
        return new FloatMemberValue(cp);
    } else if (type == double.class) {
        return new DoubleMemberValue(cp);
    } else if (type == char.class) {
        return new CharMemberValue(cp);
    } else if (type == boolean.class) {
        return new BooleanMemberValue(cp);
    } else if (type == String.class) {
        return new StringMemberValue(cp);
    } else if (type == Class.class) {
        return new ClassMemberValue(cp);
    } else if (type.isEnum()) {
        EnumMemberValue e = new EnumMemberValue(cp);
        e.setType(type.getName());
        return e;
    } else if (type.isAnnotation()) {
        return new AnnotationMemberValue(cp);
    } else if (type.isArray()) {
        Class<?> arrayType = type.getComponentType();
        MemberValue arrayval = createEmptyMemberValue(arrayType, cp);
        return new ArrayMemberValue(arrayval, cp);
    }
    throw new RuntimeException("Invalid array type " + type + " with no value ");
}
Also used : BooleanMemberValue(javassist.bytecode.annotation.BooleanMemberValue) EnumMemberValue(javassist.bytecode.annotation.EnumMemberValue) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) CharMemberValue(javassist.bytecode.annotation.CharMemberValue) ShortMemberValue(javassist.bytecode.annotation.ShortMemberValue) DoubleMemberValue(javassist.bytecode.annotation.DoubleMemberValue) LongMemberValue(javassist.bytecode.annotation.LongMemberValue) ClassMemberValue(javassist.bytecode.annotation.ClassMemberValue) IntegerMemberValue(javassist.bytecode.annotation.IntegerMemberValue) ByteMemberValue(javassist.bytecode.annotation.ByteMemberValue) FloatMemberValue(javassist.bytecode.annotation.FloatMemberValue) MemberValue(javassist.bytecode.annotation.MemberValue) AnnotationMemberValue(javassist.bytecode.annotation.AnnotationMemberValue) ArrayMemberValue(javassist.bytecode.annotation.ArrayMemberValue) DoubleMemberValue(javassist.bytecode.annotation.DoubleMemberValue) ShortMemberValue(javassist.bytecode.annotation.ShortMemberValue) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) IntegerMemberValue(javassist.bytecode.annotation.IntegerMemberValue) LongMemberValue(javassist.bytecode.annotation.LongMemberValue) ClassMemberValue(javassist.bytecode.annotation.ClassMemberValue) CharMemberValue(javassist.bytecode.annotation.CharMemberValue) EnumMemberValue(javassist.bytecode.annotation.EnumMemberValue) BooleanMemberValue(javassist.bytecode.annotation.BooleanMemberValue) ByteMemberValue(javassist.bytecode.annotation.ByteMemberValue) AnnotationMemberValue(javassist.bytecode.annotation.AnnotationMemberValue) FloatMemberValue(javassist.bytecode.annotation.FloatMemberValue) ArrayMemberValue(javassist.bytecode.annotation.ArrayMemberValue)

Example 5 with AnnotationMemberValue

use of javassist.bytecode.annotation.AnnotationMemberValue in project BroadleafCommerce by BroadleafCommerce.

the class QueryConfigurationClassTransformer method prepareQueryHints.

/**
 * Prepare any {@link QueryHint} declarations
 *
 * @param constantPool
 * @param queryHints
 * @param hint
 */
protected void prepareQueryHints(ConstPool constantPool, List<AnnotationMemberValue> queryHints, QueryHint hint) {
    Annotation queryHint = new Annotation(QueryHint.class.getName(), constantPool);
    StringMemberValue hintName = new StringMemberValue(constantPool);
    hintName.setValue(hint.name());
    queryHint.addMemberValue("name", hintName);
    StringMemberValue hintValue = new StringMemberValue(constantPool);
    hintValue.setValue(hint.value());
    queryHint.addMemberValue("value", hintValue);
    AnnotationMemberValue hintAnnotation = new AnnotationMemberValue(queryHint, constantPool);
    queryHints.add(hintAnnotation);
}
Also used : QueryHint(javax.persistence.QueryHint) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) AnnotationMemberValue(javassist.bytecode.annotation.AnnotationMemberValue) Annotation(javassist.bytecode.annotation.Annotation)

Aggregations

AnnotationMemberValue (javassist.bytecode.annotation.AnnotationMemberValue)6 StringMemberValue (javassist.bytecode.annotation.StringMemberValue)6 ArrayMemberValue (javassist.bytecode.annotation.ArrayMemberValue)5 MemberValue (javassist.bytecode.annotation.MemberValue)5 Annotation (javassist.bytecode.annotation.Annotation)4 ClassMemberValue (javassist.bytecode.annotation.ClassMemberValue)4 EnumMemberValue (javassist.bytecode.annotation.EnumMemberValue)4 ArrayList (java.util.ArrayList)3 BooleanMemberValue (javassist.bytecode.annotation.BooleanMemberValue)3 QueryHint (javax.persistence.QueryHint)3 ByteMemberValue (javassist.bytecode.annotation.ByteMemberValue)2 CharMemberValue (javassist.bytecode.annotation.CharMemberValue)2 DoubleMemberValue (javassist.bytecode.annotation.DoubleMemberValue)2 FloatMemberValue (javassist.bytecode.annotation.FloatMemberValue)2 IntegerMemberValue (javassist.bytecode.annotation.IntegerMemberValue)2 LongMemberValue (javassist.bytecode.annotation.LongMemberValue)2 ShortMemberValue (javassist.bytecode.annotation.ShortMemberValue)2 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)1 LockModeType (javax.persistence.LockModeType)1 NamedNativeQuery (javax.persistence.NamedNativeQuery)1