javassist.bytecode.annotation.StringMemberValue Java Examples

The following examples show how to use javassist.bytecode.annotation.StringMemberValue. 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: EnhanceMojo.java    From uima-uimafit with Apache License 2.0 6 votes vote down vote up
/**
 * Set a annotation member value if no value is present, if the present value is the default
 * generated by uimaFIT or if a override is active.
 * 
 * @param aAnnotation
 *          an annotation
 * @param aName
 *          the name of the member value
 * @param aNewValue
 *          the value to set
 * @param aOverride
 *          set value even if it is already set
 * @param aDefault
 *          default value set by uimaFIT - if the member has this value, it is considered unset
 */
private void enhanceMemberValue(Annotation aAnnotation, String aName, String aNewValue,
        boolean aOverride, String aDefault, ConstPool aConstPool,
        Multimap<String, String> aReportData, Class<?> aClazz) {
  String value = getStringMemberValue(aAnnotation, aName);
  boolean isEmpty = value.length() == 0;
  boolean isDefault = value.equals(aDefault);

  if (isEmpty || isDefault || aOverride) {
    if (aNewValue != null) {
      aAnnotation.addMemberValue(aName, new StringMemberValue(aNewValue, aConstPool));
      getLog().debug("Enhanced component meta data [" + aName + "]");
    } else {
      getLog().debug("No meta data [" + aName + "] found");
      aReportData.put(aClazz.getName(), "No meta data [" + aName + "] found");
    }
  } else {
    getLog().debug("Not overwriting component meta data [" + aName + "]");
  }
}
 
Example #2
Source File: AbstractRestfulBinder.java    From statefulj with Apache License 2.0 6 votes vote down vote up
protected Annotation[] addIdParameter(
		CtMethod ctMethod, 
		Class<?> idType,
		ClassPool cp) throws NotFoundException, CannotCompileException {
	// Clone the parameter Class
	//
	CtClass ctParm = cp.get(idType.getName());
	
	// Add the parameter to the method
	//
	ctMethod.addParameter(ctParm);
	
	// Add the Parameter Annotations to the Method
	//
	MethodInfo methodInfo = ctMethod.getMethodInfo();
	ConstPool constPool = methodInfo.getConstPool();
	Annotation annot = new Annotation(getPathAnnotationClass().getName(), constPool);
	
	StringMemberValue valueVal = new StringMemberValue("id", constPool); 
	annot.addMemberValue("value", valueVal);
	
	return new Annotation[]{ annot };
}
 
Example #3
Source File: JavassistDynamicField.java    From gecco with MIT License 6 votes vote down vote up
@Override
public DynamicField image(String download, String... value) {
	Annotation annot = new Annotation(Image.class.getName(), cpool);
       annot.addMemberValue("download", new StringMemberValue(download, 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 #4
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 #5
Source File: SpringMVCBinder.java    From statefulj with Apache License 2.0 6 votes vote down vote up
@Override
protected void addEndpointMapping(CtMethod ctMethod, String method, String request) {
	MethodInfo methodInfo = ctMethod.getMethodInfo();
	ConstPool constPool = methodInfo.getConstPool();

	AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
	Annotation requestMapping = new Annotation(RequestMapping.class.getName(), constPool);

	ArrayMemberValue valueVals = new ArrayMemberValue(constPool);
	StringMemberValue valueVal = new StringMemberValue(constPool);
	valueVal.setValue(request);
	valueVals.setValue(new MemberValue[]{valueVal});

	requestMapping.addMemberValue("value", valueVals);

	ArrayMemberValue methodVals = new ArrayMemberValue(constPool);
	EnumMemberValue methodVal = new EnumMemberValue(constPool);
	methodVal.setType(RequestMethod.class.getName());
	methodVal.setValue(method);
	methodVals.setValue(new MemberValue[]{methodVal});

	requestMapping.addMemberValue("method", methodVals);
	attr.addAnnotation(requestMapping);
	methodInfo.addAttribute(attr);
}
 
Example #6
Source File: TransformationUtil.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
static
private void removeValue(ArrayMemberValue memberValue, String value){
	List<MemberValue> values = new ArrayList<>(Arrays.asList(memberValue.getValue()));

	boolean removed = false;

	Iterator<MemberValue> it = values.iterator();
	while(it.hasNext()){
		StringMemberValue stringValue = (StringMemberValue)it.next();

		if((value).equals(stringValue.getValue())){
			it.remove();

			removed = true;
		}
	}

	if(!removed){
		throw new RuntimeException(value + " not in " + values);
	}

	memberValue.setValue(values.toArray(new MemberValue[values.size()]));
}
 
Example #7
Source File: CreateMethodCreator.java    From minnal with Apache License 2.0 6 votes vote down vote up
@Override
protected void addParamAnnotations(CtMethod ctMethod) {
	Annotation[][] annotations = new Annotation[4][1];
	Annotation headersParam = new Annotation(Context.class.getCanonicalName(), ctMethod.getMethodInfo().getConstPool());
	annotations[0][0] = headersParam;
	Annotation uriInfoParam = new Annotation(Context.class.getCanonicalName(), ctMethod.getMethodInfo().getConstPool());
	annotations[1][0] = uriInfoParam;
	Annotation providersParam = new Annotation(Context.class.getCanonicalName(), ctMethod.getMethodInfo().getConstPool());
	annotations[2][0] = providersParam;
	Annotation apiParam = new Annotation(ApiParam.class.getCanonicalName(), ctMethod.getMethodInfo().getConstPool());
	apiParam.addMemberValue("name", new StringMemberValue("body", ctMethod.getMethodInfo().getConstPool()));
	apiParam.addMemberValue("access", new StringMemberValue("internal", ctMethod.getMethodInfo().getConstPool()));
	apiParam.addMemberValue("paramType", new StringMemberValue("body", ctMethod.getMethodInfo().getConstPool()));
	annotations[3][0] = apiParam;
	JavassistUtils.addParameterAnnotation(ctMethod, annotations);
}
 
Example #8
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 #9
Source File: AbstractMethodCreator.java    From minnal with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the security annotation
 * 
 * @return
 */
protected Annotation getSecurityAnnotation() {
	Set<String> permissions = getPermissions();
	if (permissions == null || permissions.isEmpty()) {
		return null;
	}
	
	Annotation rolesAllowed = new Annotation(RolesAllowed.class.getCanonicalName(), ctClass.getClassFile().getConstPool());
	ArrayMemberValue values = new ArrayMemberValue(ctClass.getClassFile().getConstPool());
	List<StringMemberValue> memberValues = new ArrayList<StringMemberValue>();
	for (String permission : permissions) {
		memberValues.add(new StringMemberValue(permission, ctClass.getClassFile().getConstPool()));
	}
	values.setValue(memberValues.toArray(new StringMemberValue[0]));
	rolesAllowed.addMemberValue("value", values);
	return rolesAllowed;
}
 
Example #10
Source File: ActionMethodCreator.java    From minnal with Apache License 2.0 5 votes vote down vote up
@Override
protected Annotation getApiOperationAnnotation() {
	ConstPool constPool = getCtClass().getClassFile().getConstPool();
	Annotation annotation = new Annotation(ApiOperation.class.getCanonicalName(), constPool);
	annotation.addMemberValue("value", new StringMemberValue("Performs action on " + getResourcePath().getNodePath().getName(), constPool));
	return annotation;
}
 
Example #11
Source File: ReadMethodCreator.java    From minnal with Apache License 2.0 5 votes vote down vote up
@Override
protected Annotation getApiOperationAnnotation() {
	ConstPool constPool = getCtClass().getClassFile().getConstPool();
	EntityNodePath path = getResourcePath().getNodePath();
	EntityMetaData metaData = path.get(path.size() - 1).getEntityMetaData();
	Annotation annotation = new Annotation(ApiOperation.class.getCanonicalName(), constPool);
	annotation.addMemberValue("value", new StringMemberValue("Find " + getResourcePath().getNodePath().getName() + " by id", constPool));
	annotation.addMemberValue("response", new ClassMemberValue(metaData.getEntityClass().getCanonicalName(), constPool));
	return annotation;
}
 
Example #12
Source File: CreateMethodCreator.java    From minnal with Apache License 2.0 5 votes vote down vote up
protected Annotation getBodyParamAnnotation() {
	EntityNodePath path = getResourcePath().getNodePath();
	EntityMetaData metaData = path.get(path.size() - 1).getEntityMetaData();
	Annotation annotation = new Annotation(ApiImplicitParam.class.getCanonicalName(), getCtClass().getClassFile().getConstPool());
	annotation.addMemberValue("name", new StringMemberValue("body", getCtClass().getClassFile().getConstPool()));
	annotation.addMemberValue("paramType", new StringMemberValue("body", getCtClass().getClassFile().getConstPool()));
	annotation.addMemberValue("dataType", new StringMemberValue("Array[" + metaData.getEntityClass().getCanonicalName() + "]", getCtClass().getClassFile().getConstPool()));
	annotation.addMemberValue("value", new StringMemberValue(metaData.getName() + " payload", getCtClass().getClassFile().getConstPool()));
	return annotation;
}
 
Example #13
Source File: CreateMethodCreator.java    From minnal with Apache License 2.0 5 votes vote down vote up
@Override
protected Annotation getApiOperationAnnotation() {
	ConstPool constPool = getCtClass().getClassFile().getConstPool();
	EntityNodePath path = getResourcePath().getNodePath();
	EntityMetaData metaData = path.get(path.size() - 1).getEntityMetaData();
	Annotation annotation = new Annotation(ApiOperation.class.getCanonicalName(), constPool);
	annotation.addMemberValue("value", new StringMemberValue("Create " + metaData.getName(), constPool));
	annotation.addMemberValue("response", new ClassMemberValue(metaData.getEntityClass().getCanonicalName(), constPool));
	annotation.addMemberValue("responseContainer", new StringMemberValue("List", constPool));
	return annotation;
}
 
Example #14
Source File: ResourceClassCreator.java    From minnal with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the api annotation
 * 
 * @param constPool
 * @return
 */
protected Annotation getApiAnnotation(ConstPool constPool) {
	Annotation apiAnnotation = new Annotation(Api.class.getCanonicalName(), constPool);
	apiAnnotation.addMemberValue("value", new StringMemberValue(path, constPool));
	apiAnnotation.addMemberValue("description", new StringMemberValue("Operations about " + namingStrategy.getResourceName(entityClass), constPool));
	return apiAnnotation;
}
 
Example #15
Source File: DeleteMethodCreator.java    From minnal with Apache License 2.0 5 votes vote down vote up
@Override
protected Annotation getApiOperationAnnotation() {
	ConstPool constPool = getCtClass().getClassFile().getConstPool();
	Annotation annotation = new Annotation(ApiOperation.class.getCanonicalName(), constPool);
	annotation.addMemberValue("value", new StringMemberValue("Delete " + getResourcePath().getNodePath().getName() + " by id", constPool));
	return annotation;
}
 
Example #16
Source File: UpdateMethodCreator.java    From minnal with Apache License 2.0 5 votes vote down vote up
@Override
protected Annotation getApiOperationAnnotation() {
	ConstPool constPool = getCtClass().getClassFile().getConstPool();
	Annotation annotation = new Annotation(ApiOperation.class.getCanonicalName(), constPool);
	annotation.addMemberValue("value", new StringMemberValue("Update " + getResourcePath().getNodePath().getName() + " by id", constPool));
	return annotation;
}
 
Example #17
Source File: JerseyBinder.java    From statefulj with Apache License 2.0 5 votes vote down vote up
@Override
protected void addEndpointMapping(
		CtMethod ctMethod, 
		String method,
		String request) {
	
	// Add Path Annotation
	//
	MethodInfo methodInfo = ctMethod.getMethodInfo();
	ConstPool constPool = methodInfo.getConstPool();

	AnnotationsAttribute annoAttr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
	Annotation pathMapping = new Annotation(Path.class.getName(), constPool);
	
	StringMemberValue valueVal = new StringMemberValue(constPool);
	valueVal.setValue(request);
	
	pathMapping.addMemberValue("value", valueVal);
	
	annoAttr.addAnnotation(pathMapping);

	// Add Verb Annotation (GET|POST|PUT|DELETE)
	//
	String verbClassName = "javax.ws.rs." + method;
	Annotation verb = new Annotation(verbClassName, constPool);
	annoAttr.addAnnotation(verb);
	
	methodInfo.addAttribute(annoAttr);
}
 
Example #18
Source File: ActionMethodCreator.java    From minnal with Apache License 2.0 5 votes vote down vote up
@Override
protected Annotation getBodyParamAnnotation() {
	Annotation annotation = new Annotation(ApiImplicitParam.class.getCanonicalName(), getCtClass().getClassFile().getConstPool());
	annotation.addMemberValue("name", new StringMemberValue("body", getCtClass().getClassFile().getConstPool()));
	annotation.addMemberValue("paramType", new StringMemberValue("body", getCtClass().getClassFile().getConstPool()));
	annotation.addMemberValue("dataType", new StringMemberValue(Map.class.getCanonicalName(), getCtClass().getClassFile().getConstPool()));
	annotation.addMemberValue("value", new StringMemberValue("Request payload", getCtClass().getClassFile().getConstPool()));
	return annotation;
}
 
Example #19
Source File: AbstractMethodCreator.java    From minnal with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the #{@link Path} annotation
 * 
 * @return
 */
protected Annotation getPathAnnotation() {
	String relativePath = getRelativePath();
	if (Strings.isNullOrEmpty(relativePath)) {
		return null;
	}
	ConstPool constPool = ctClass.getClassFile().getConstPool();
	Annotation pathAnnotation = new Annotation(Path.class.getCanonicalName(), constPool);
	pathAnnotation.addMemberValue("value", new StringMemberValue(relativePath, constPool));
	return pathAnnotation;
}
 
Example #20
Source File: AbstractMethodCreator.java    From minnal with Apache License 2.0 5 votes vote down vote up
protected Annotation getProducesAnnotation() {
	Annotation annotation = new Annotation(Produces.class.getCanonicalName(), getCtClass().getClassFile().getConstPool());
	ArrayMemberValue values = new ArrayMemberValue(getCtClass().getClassFile().getConstPool());
	StringMemberValue json = new StringMemberValue(MediaType.APPLICATION_JSON, getCtClass().getClassFile().getConstPool());
	StringMemberValue xml = new StringMemberValue(MediaType.APPLICATION_XML, getCtClass().getClassFile().getConstPool());
	values.setValue(new StringMemberValue[]{json, xml});
	annotation.addMemberValue("value", values);
	return annotation;
}
 
Example #21
Source File: AbstractMethodCreator.java    From minnal with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the 200 ok response annotation
 * 
 * @param responseClass
 * @return
 */
protected Annotation getOkResponseAnnotation(Class<?> responseClass) {
	ConstPool constPool = ctClass.getClassFile().getConstPool();
	Annotation annotation = new Annotation(ApiResponse.class.getCanonicalName(), constPool);
	IntegerMemberValue code = new IntegerMemberValue(constPool);
	code.setValue(Response.Status.OK.getStatusCode());
	annotation.addMemberValue("code", code);
	annotation.addMemberValue("message", new StringMemberValue(Response.Status.OK.getReasonPhrase(), constPool));
	annotation.addMemberValue("response", new ClassMemberValue(responseClass.getCanonicalName(), constPool));
	return annotation;
}
 
Example #22
Source File: AbstractMethodCreator.java    From minnal with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the 404 not found response annotation
 * 
 * @param responseClass
 * @return
 */
protected Annotation getNotFoundResponseAnnotation() {
	ConstPool constPool = ctClass.getClassFile().getConstPool();
	Annotation annotation = new Annotation(ApiResponse.class.getCanonicalName(), constPool);
	IntegerMemberValue code = new IntegerMemberValue(constPool);
	code.setValue(Response.Status.NOT_FOUND.getStatusCode());
	annotation.addMemberValue("code", code);
	annotation.addMemberValue("message", new StringMemberValue(Response.Status.NOT_FOUND.getReasonPhrase(), constPool));
	return annotation;
}
 
Example #23
Source File: AbstractMethodCreator.java    From minnal with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the 204 no content response annotation
 * 
 * @param responseClass
 * @return
 */
protected Annotation getNoContentResponseAnnotation() {
	ConstPool constPool = ctClass.getClassFile().getConstPool();
	Annotation annotation = new Annotation(ApiResponse.class.getCanonicalName(), constPool);
	IntegerMemberValue code = new IntegerMemberValue(constPool);
	code.setValue(Response.Status.NO_CONTENT.getStatusCode());
	annotation.addMemberValue("code", code);
	annotation.addMemberValue("message", new StringMemberValue(Response.Status.NO_CONTENT.getReasonPhrase(), constPool));
	return annotation;
}
 
Example #24
Source File: AbstractMethodCreator.java    From minnal with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the 400 bad request response annotation
 * 
 * @param responseClass
 * @return
 */
protected Annotation getBadRequestResponseAnnotation() {
	ConstPool constPool = ctClass.getClassFile().getConstPool();
	Annotation annotation = new Annotation(ApiResponse.class.getCanonicalName(), constPool);
	IntegerMemberValue code = new IntegerMemberValue(constPool);
	code.setValue(Response.Status.BAD_REQUEST.getStatusCode());
	annotation.addMemberValue("code", code);
	annotation.addMemberValue("message", new StringMemberValue(Response.Status.BAD_REQUEST.getReasonPhrase(), constPool));
	return annotation;
}
 
Example #25
Source File: ListMethodCreator.java    From minnal with Apache License 2.0 5 votes vote down vote up
@Override
protected Annotation getApiOperationAnnotation() {
	ConstPool constPool = getCtClass().getClassFile().getConstPool();
	EntityNodePath path = getResourcePath().getNodePath();
	EntityMetaData metaData = path.get(path.size() - 1).getEntityMetaData();
	Annotation annotation = new Annotation(ApiOperation.class.getCanonicalName(), constPool);
	annotation.addMemberValue("value", new StringMemberValue("Search " + metaData.getName(), constPool));
	annotation.addMemberValue("response", new ClassMemberValue(metaData.getEntityClass().getCanonicalName(), constPool));
	annotation.addMemberValue("responseContainer", new StringMemberValue("List", constPool));
	return annotation;
}
 
Example #26
Source File: ListMethodCreator.java    From minnal with Apache License 2.0 5 votes vote down vote up
protected List<Annotation> getApiQueryParamAnnotations() {
	List<Annotation> annotations = new ArrayList<Annotation>();
	for (QueryParam param : getResourcePath().getNodePath().getQueryParams()) {
		Annotation annotation = new Annotation(ApiImplicitParam.class.getCanonicalName(), getCtClass().getClassFile().getConstPool());
		annotation.addMemberValue("name", new StringMemberValue(param.getName(), getCtClass().getClassFile().getConstPool()));
		annotation.addMemberValue("paramType", new StringMemberValue("query", getCtClass().getClassFile().getConstPool()));
		annotation.addMemberValue("dataType", new StringMemberValue(param.getType().name(), getCtClass().getClassFile().getConstPool()));
		annotation.addMemberValue("value", new StringMemberValue(param.getDescription(), getCtClass().getClassFile().getConstPool()));
		annotations.add(annotation);
	}
	return annotations;
}
 
Example #27
Source File: EnhanceMojo.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
private String getStringMemberValue(Annotation aAnnotation, String aValue) {
  MemberValue v = aAnnotation.getMemberValue(aValue);
  if (v == null) {
    return "";
  } else {
    return ((StringMemberValue) v).getValue();
  }
}
 
Example #28
Source File: ModelInstrumentation.java    From javalite with Apache License 2.0 5 votes vote down vote up
private void addGeneratedAnnotation(CtMethod generatedMethod, CtClass target)
{
    ConstPool constPool = target.getClassFile().getConstPool();
    AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);

    Annotation annot = new Annotation("javax.annotation.Generated", constPool);
    annot.addMemberValue("value", new StringMemberValue("org.javalite.instrumentation.ModelInstrumentation", constPool));

    ZonedDateTime now = ZonedDateTime.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(GENERATED_DATE_PATTERN);
    annot.addMemberValue("date", new StringMemberValue(now.format(formatter), constPool));

    attr.addAnnotation(annot);
    generatedMethod.getMethodInfo().addAttribute(attr);
}
 
Example #29
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 #30
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;
}