javassist.bytecode.annotation.BooleanMemberValue Java Examples

The following examples show how to use javassist.bytecode.annotation.BooleanMemberValue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavassistDynamicField.java    From gecco with MIT License 6 votes vote down vote up
@Override
public DynamicField href(boolean click, String... value) {
	Annotation annot = new Annotation(Href.class.getName(), cpool);
       annot.addMemberValue("click", new BooleanMemberValue(click, cpool));
       
       ArrayMemberValue arrayMemberValue = new ArrayMemberValue(cpool);
       MemberValue[] memberValues = new StringMemberValue[value.length];
       for(int i = 0; i < value.length; i++) {
       	memberValues[i] = new StringMemberValue(value[i], cpool);
       }
       arrayMemberValue.setValue(memberValues);
       annot.addMemberValue("value", arrayMemberValue);
       
       attr.addAnnotation(annot);
	return this;
}
 
Example #2
Source File: AbstractMethodCreator.java    From minnal with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the api path parameter annotations
 * 
 * @return
 */
protected List<Annotation> getApiPathParamAnnotations() {
	List<Annotation> annotations = new ArrayList<Annotation>();
	List<String> parameters = getRoutePattern().getParameterNames();
	for (int i = 0; i < parameters.size(); i++) {
		Annotation annotation = new Annotation(ApiImplicitParam.class.getCanonicalName(), ctClass.getClassFile().getConstPool());
		annotation.addMemberValue("name", new StringMemberValue(parameters.get(i), ctClass.getClassFile().getConstPool()));
		annotation.addMemberValue("paramType", new StringMemberValue("path", ctClass.getClassFile().getConstPool()));
		annotation.addMemberValue("dataType", new StringMemberValue(String.class.getCanonicalName(), ctClass.getClassFile().getConstPool()));
		annotation.addMemberValue("value", new StringMemberValue("The " + getResourcePath().getNodePath().get(i).getEntityMetaData().getName() + " identifier", ctClass.getClassFile().getConstPool()));
		annotation.addMemberValue("required", new BooleanMemberValue(true, ctClass.getClassFile().getConstPool()));
		if (i == parameters.size() - 1) {
			annotation.addMemberValue("allowMultiple", new BooleanMemberValue(true, ctClass.getClassFile().getConstPool()));
		}
		annotations.add(annotation);
	}
	return annotations;
}
 
Example #3
Source File: JValidator.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
private static MemberValue createMemberValue(ConstPool cp, CtClass type, Object value) throws NotFoundException {
    MemberValue memberValue = javassist.bytecode.annotation.Annotation.createMemberValue(cp, type);
    if (memberValue instanceof BooleanMemberValue)
        ((BooleanMemberValue) memberValue).setValue((Boolean) value);
    else if (memberValue instanceof ByteMemberValue)
        ((ByteMemberValue) memberValue).setValue((Byte) value);
    else if (memberValue instanceof CharMemberValue)
        ((CharMemberValue) memberValue).setValue((Character) value);
    else if (memberValue instanceof ShortMemberValue)
        ((ShortMemberValue) memberValue).setValue((Short) value);
    else if (memberValue instanceof IntegerMemberValue)
        ((IntegerMemberValue) memberValue).setValue((Integer) value);
    else if (memberValue instanceof LongMemberValue)
        ((LongMemberValue) memberValue).setValue((Long) value);
    else if (memberValue instanceof FloatMemberValue)
        ((FloatMemberValue) memberValue).setValue((Float) value);
    else if (memberValue instanceof DoubleMemberValue)
        ((DoubleMemberValue) memberValue).setValue((Double) value);
    else if (memberValue instanceof ClassMemberValue)
        ((ClassMemberValue) memberValue).setValue(((Class<?>) value).getName());
    else if (memberValue instanceof StringMemberValue)
        ((StringMemberValue) memberValue).setValue((String) value);
    else if (memberValue instanceof EnumMemberValue)
        ((EnumMemberValue) memberValue).setValue(((Enum<?>) value).name());
    /* else if (memberValue instanceof AnnotationMemberValue) */
    else if (memberValue instanceof ArrayMemberValue) {
        CtClass arrayType = type.getComponentType();
        int len = Array.getLength(value);
        MemberValue[] members = new MemberValue[len];
        for (int i = 0; i < len; i++) {
            members[i] = createMemberValue(cp, arrayType, Array.get(value, i));
        }
        ((ArrayMemberValue) memberValue).setValue(members);
    }
    return memberValue;
}
 
Example #4
Source File: ClassUtils.java    From swagger-more with Apache License 2.0 5 votes vote down vote up
private static CtField createField(Class aClass, String name, String value, CtClass ctClass) throws NotFoundException, CannotCompileException {
    ClassPool.getDefault().insertClassPath(new ClassClassPath(aClass));
    CtField field = new CtField(ClassPool.getDefault().get(aClass.getName()), name, ctClass);
    field.setModifiers(javassist.Modifier.PUBLIC);
    ConstPool constPool = ctClass.getClassFile().getConstPool();
    AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
    Annotation ann = new Annotation(ApiModelProperty.class.getName(), constPool);
    ann.addMemberValue("value", new StringMemberValue(value, constPool));
    ann.addMemberValue("name", new StringMemberValue(name, constPool));
    ann.addMemberValue("required", new BooleanMemberValue(true, constPool));
    attr.addAnnotation(ann);
    field.getFieldInfo().addAttribute(attr);
    return field;
}
 
Example #5
Source File: ClassGenerator.java    From flower with Apache License 2.0 5 votes vote down vote up
private MemberValue getMemberValue(Object obj, ConstPool cp) {
  if (obj == null) {
    return null;
  }
  if (obj instanceof Integer) {
    return new IntegerMemberValue(cp, (Integer) obj);
  } else if (obj instanceof Boolean) {
    return new BooleanMemberValue((Boolean) obj, cp);
  } else if (obj instanceof Double) {
    return new DoubleMemberValue((Double) obj, cp);
  } else if (obj instanceof Float) {
    return new FloatMemberValue((Float) obj, cp);
  } else if (obj instanceof Short) {
    return new ShortMemberValue((Short) obj, cp);
  } else if (obj instanceof String) {
    return new StringMemberValue((String) obj, cp);
  } else if (obj instanceof String[]) {
    String[] oo = (String[]) obj;
    MemberValue[] memberValues = new MemberValue[oo.length];
    ArrayMemberValue value = new ArrayMemberValue(cp);
    for (int i = 0; i < oo.length; i++) {
      memberValues[i] = getMemberValue(oo[i], cp);
    }
    value.setValue(memberValues);
    return value;
  } else if (obj instanceof Byte) {
    return new ByteMemberValue((Byte) obj, cp);
  } else if (obj instanceof Annotation) {
    return new AnnotationMemberValue((Annotation) obj, cp);
  } else if (obj instanceof ArrayMemberValue) {
    return new ArrayMemberValue((MemberValue) obj, cp);
  } else if (obj instanceof Character) {
    return new CharMemberValue((char) obj, cp);
  } else if (obj instanceof Long) {
    return new LongMemberValue((Long) obj, cp);
  }
  return null;
}
 
Example #6
Source File: JValidator.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private static MemberValue createMemberValue(ConstPool cp, CtClass type, Object value) throws NotFoundException {
    MemberValue memberValue = javassist.bytecode.annotation.Annotation.createMemberValue(cp, type);
    if (memberValue instanceof BooleanMemberValue)
        ((BooleanMemberValue) memberValue).setValue((Boolean) value);
    else if (memberValue instanceof ByteMemberValue)
        ((ByteMemberValue) memberValue).setValue((Byte) value);
    else if (memberValue instanceof CharMemberValue)
        ((CharMemberValue) memberValue).setValue((Character) value);
    else if (memberValue instanceof ShortMemberValue)
        ((ShortMemberValue) memberValue).setValue((Short) value);
    else if (memberValue instanceof IntegerMemberValue)
        ((IntegerMemberValue) memberValue).setValue((Integer) value);
    else if (memberValue instanceof LongMemberValue)
        ((LongMemberValue) memberValue).setValue((Long) value);
    else if (memberValue instanceof FloatMemberValue)
        ((FloatMemberValue) memberValue).setValue((Float) value);
    else if (memberValue instanceof DoubleMemberValue)
        ((DoubleMemberValue) memberValue).setValue((Double) value);
    else if (memberValue instanceof ClassMemberValue)
        ((ClassMemberValue) memberValue).setValue(((Class<?>)value).getName());
    else if (memberValue instanceof StringMemberValue)
        ((StringMemberValue) memberValue).setValue((String) value);
    else if (memberValue instanceof EnumMemberValue) 
        ((EnumMemberValue) memberValue).setValue(((Enum<?>) value).name());
    /* else if (memberValue instanceof AnnotationMemberValue) */
    else if (memberValue instanceof ArrayMemberValue) {
        CtClass arrayType = type.getComponentType();
        int len = Array.getLength(value);
        MemberValue[] members = new MemberValue[len];
        for (int i = 0; i < len; i ++) {
            members[i] = createMemberValue(cp, arrayType, Array.get(value, i));
        }
        ((ArrayMemberValue) memberValue).setValue(members);
    }
    return memberValue;
}
 
Example #7
Source File: JValidator.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
private static MemberValue createMemberValue(ConstPool cp, CtClass type, Object value) throws NotFoundException {
    MemberValue memberValue = javassist.bytecode.annotation.Annotation.createMemberValue(cp, type);
    if (memberValue instanceof BooleanMemberValue)
        ((BooleanMemberValue) memberValue).setValue((Boolean) value);
    else if (memberValue instanceof ByteMemberValue)
        ((ByteMemberValue) memberValue).setValue((Byte) value);
    else if (memberValue instanceof CharMemberValue)
        ((CharMemberValue) memberValue).setValue((Character) value);
    else if (memberValue instanceof ShortMemberValue)
        ((ShortMemberValue) memberValue).setValue((Short) value);
    else if (memberValue instanceof IntegerMemberValue)
        ((IntegerMemberValue) memberValue).setValue((Integer) value);
    else if (memberValue instanceof LongMemberValue)
        ((LongMemberValue) memberValue).setValue((Long) value);
    else if (memberValue instanceof FloatMemberValue)
        ((FloatMemberValue) memberValue).setValue((Float) value);
    else if (memberValue instanceof DoubleMemberValue)
        ((DoubleMemberValue) memberValue).setValue((Double) value);
    else if (memberValue instanceof ClassMemberValue)
        ((ClassMemberValue) memberValue).setValue(((Class<?>)value).getName());
    else if (memberValue instanceof StringMemberValue)
        ((StringMemberValue) memberValue).setValue((String) value);
    else if (memberValue instanceof EnumMemberValue) 
        ((EnumMemberValue) memberValue).setValue(((Enum<?>) value).name());
    /* else if (memberValue instanceof AnnotationMemberValue) */
    else if (memberValue instanceof ArrayMemberValue) {
        CtClass arrayType = type.getComponentType();
        int len = Array.getLength(value);
        MemberValue[] members = new MemberValue[len];
        for (int i = 0; i < len; i ++) {
            members[i] = createMemberValue(cp, arrayType, Array.get(value, i));
        }
        ((ArrayMemberValue) memberValue).setValue(members);
    }
    return memberValue;
}
 
Example #8
Source File: JValidator.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private static MemberValue createMemberValue(ConstPool cp, CtClass type, Object value) throws NotFoundException {
    MemberValue memberValue = javassist.bytecode.annotation.Annotation.createMemberValue(cp, type);
    if (memberValue instanceof BooleanMemberValue)
        ((BooleanMemberValue) memberValue).setValue((Boolean) value);
    else if (memberValue instanceof ByteMemberValue)
        ((ByteMemberValue) memberValue).setValue((Byte) value);
    else if (memberValue instanceof CharMemberValue)
        ((CharMemberValue) memberValue).setValue((Character) value);
    else if (memberValue instanceof ShortMemberValue)
        ((ShortMemberValue) memberValue).setValue((Short) value);
    else if (memberValue instanceof IntegerMemberValue)
        ((IntegerMemberValue) memberValue).setValue((Integer) value);
    else if (memberValue instanceof LongMemberValue)
        ((LongMemberValue) memberValue).setValue((Long) value);
    else if (memberValue instanceof FloatMemberValue)
        ((FloatMemberValue) memberValue).setValue((Float) value);
    else if (memberValue instanceof DoubleMemberValue)
        ((DoubleMemberValue) memberValue).setValue((Double) value);
    else if (memberValue instanceof ClassMemberValue)
        ((ClassMemberValue) memberValue).setValue(((Class<?>)value).getName());
    else if (memberValue instanceof StringMemberValue)
        ((StringMemberValue) memberValue).setValue((String) value);
    else if (memberValue instanceof EnumMemberValue) 
        ((EnumMemberValue) memberValue).setValue(((Enum<?>) value).name());
    /* else if (memberValue instanceof AnnotationMemberValue) */
    else if (memberValue instanceof ArrayMemberValue) {
        CtClass arrayType = type.getComponentType();
        int len = Array.getLength(value);
        MemberValue[] members = new MemberValue[len];
        for (int i = 0; i < len; i ++) {
            members[i] = createMemberValue(cp, arrayType, Array.get(value, i));
        }
        ((ArrayMemberValue) memberValue).setValue(members);
    }
    return memberValue;
}
 
Example #9
Source File: JValidator.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private static MemberValue createMemberValue(ConstPool cp, CtClass type, Object value) throws NotFoundException {
    MemberValue memberValue = javassist.bytecode.annotation.Annotation.createMemberValue(cp, type);
    if (memberValue instanceof BooleanMemberValue)
        ((BooleanMemberValue) memberValue).setValue((Boolean) value);
    else if (memberValue instanceof ByteMemberValue)
        ((ByteMemberValue) memberValue).setValue((Byte) value);
    else if (memberValue instanceof CharMemberValue)
        ((CharMemberValue) memberValue).setValue((Character) value);
    else if (memberValue instanceof ShortMemberValue)
        ((ShortMemberValue) memberValue).setValue((Short) value);
    else if (memberValue instanceof IntegerMemberValue)
        ((IntegerMemberValue) memberValue).setValue((Integer) value);
    else if (memberValue instanceof LongMemberValue)
        ((LongMemberValue) memberValue).setValue((Long) value);
    else if (memberValue instanceof FloatMemberValue)
        ((FloatMemberValue) memberValue).setValue((Float) value);
    else if (memberValue instanceof DoubleMemberValue)
        ((DoubleMemberValue) memberValue).setValue((Double) value);
    else if (memberValue instanceof ClassMemberValue)
        ((ClassMemberValue) memberValue).setValue(((Class<?>)value).getName());
    else if (memberValue instanceof StringMemberValue)
        ((StringMemberValue) memberValue).setValue((String) value);
    else if (memberValue instanceof EnumMemberValue) 
        ((EnumMemberValue) memberValue).setValue(((Enum<?>) value).name());
    /* else if (memberValue instanceof AnnotationMemberValue) */
    else if (memberValue instanceof ArrayMemberValue) {
        CtClass arrayType = type.getComponentType();
        int len = Array.getLength(value);
        MemberValue[] members = new MemberValue[len];
        for (int i = 0; i < len; i ++) {
            members[i] = createMemberValue(cp, arrayType, Array.get(value, i));
        }
        ((ArrayMemberValue) memberValue).setValue(members);
    }
    return memberValue;
}
 
Example #10
Source File: JavassistDynamicField.java    From gecco with MIT License 5 votes vote down vote up
@Override
public DynamicField text(boolean own) {
       Annotation annot = new Annotation(Text.class.getName(), cpool);
       annot.addMemberValue("own", new BooleanMemberValue(own, cpool));
       attr.addAnnotation(annot);
	return this;
}
 
Example #11
Source File: JavassistDynamicField.java    From gecco with MIT License 5 votes vote down vote up
@Override
public DynamicField html(boolean outer) {
	Annotation annot = new Annotation(Html.class.getName(), cpool);
       annot.addMemberValue("outer", new BooleanMemberValue(outer, cpool));
       attr.addAnnotation(annot);
	return this;
}
 
Example #12
Source File: JCommanderTranslationMap.java    From geowave with Apache License 2.0 5 votes vote down vote up
/**
 * Iterate the annotations, look for a 'required' parameter, and set it to false.
 */
private void disableBooleanMember(final String booleanMemberName, final CtField field) {

  // This is the JCommander package name
  final String packageName = JCommander.class.getPackage().getName();

  final AnnotationsAttribute fieldAttributes =
      (AnnotationsAttribute) field.getFieldInfo().getAttribute(AnnotationsAttribute.visibleTag);

  // Look for annotations that have a 'names' attribute, and whose package
  // starts with the expected JCommander package.
  for (final Annotation annotation : fieldAttributes.getAnnotations()) {
    if (annotation.getTypeName().startsWith(packageName)) {
      // See if it has a 'names' member variable.
      final MemberValue requiredMember = annotation.getMemberValue(booleanMemberName);

      // We have a names member!!!
      if (requiredMember != null) {
        final BooleanMemberValue booleanRequiredMember = (BooleanMemberValue) requiredMember;

        // Set it to not required.
        booleanRequiredMember.setValue(false);

        // This is KEY! For some reason, the existing annotation
        // will not be modified unless
        // you call 'setAnnotation' here. I'm guessing
        // 'getAnnotation()' creates a copy.
        fieldAttributes.setAnnotation(annotation);

        // Finished processing names.
        break;
      }
    }
  }
}
 
Example #13
Source File: ClassPathScanner.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public void visitBooleanMemberValue(BooleanMemberValue node) {
  values.add(String.valueOf(node.getValue()));
}
 
Example #14
Source File: ServiceDelegateBuilder.java    From jweb-cms with GNU Affero General Public License v3.0 4 votes vote down vote up
private MemberValue memberValue(Class<?> type, Object value) {
    if (type.isArray()) {
        Object[] array = (Object[]) value;
        MemberValue[] memberValues = new MemberValue[array.length];

        Class<?> componentType = type.getComponentType();
        for (int i = 0; i < array.length; i++) {
            memberValues[i] = memberValue(componentType, array[i]);
        }
        ArrayMemberValue arrayMemberValue = new ArrayMemberValue(constPool);
        arrayMemberValue.setValue(memberValues);
        return arrayMemberValue;
    } else {
        if (type == int.class) {
            return new IntegerMemberValue(constPool, (int) value);
        } else if (long.class.equals(type)) {
            return new LongMemberValue((long) value, constPool);
        } else if (short.class.equals(type)) {
            return new ShortMemberValue((short) value, constPool);
        } else if (byte.class.equals(type)) {
            return new ByteMemberValue((byte) value, constPool);
        } else if (char.class.equals(type)) {
            return new CharMemberValue((char) value, constPool);
        } else if (float.class.equals(type)) {
            return new FloatMemberValue((float) value, constPool);
        } else if (double.class.equals(type)) {
            return new DoubleMemberValue((double) value, constPool);
        } else if (String.class.equals(type)) {
            return new StringMemberValue((String) value, constPool);
        } else if (boolean.class.equals(type)) {
            return new BooleanMemberValue((boolean) value, constPool);
        } else if (Class.class.equals(type)) {
            return new ClassMemberValue(((Class) value).getName(), constPool);
        } else if (Enum.class.isAssignableFrom(type)) {
            EnumMemberValue enumMemberValue = new EnumMemberValue(constPool);
            enumMemberValue.setType(type.getName());
            enumMemberValue.setValue(((Enum) value).name());
            return enumMemberValue;
        } else {
            throw new ApplicationException("unsupported annotation method type, type={}", type);
        }
    }
}
 
Example #15
Source File: ClassExtractorAnnotationMemberValue.java    From smart-testing with Apache License 2.0 4 votes vote down vote up
@Override
public void visitBooleanMemberValue(BooleanMemberValue node) {
}
 
Example #16
Source File: ClassPathScanner.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public void visitBooleanMemberValue(BooleanMemberValue node) {
  values.add(String.valueOf(node.getValue()));
}