javassist.bytecode.annotation.ClassMemberValue Java Examples

The following examples show how to use javassist.bytecode.annotation.ClassMemberValue. 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: ClassTemplate.java    From anno4j with Apache License 2.0 6 votes vote down vote up
public void addAnnotation(Class<?> type, Class<?>... values) {
	ClassFile cf = cc.getClassFile();
	ConstPool cp = cf.getConstPool();
	ClassMemberValue[] elements = new ClassMemberValue[values.length];
	for (int i = 0; i < values.length; i++) {
		elements[i] = cb.createClassMemberValue(values[i], cp);
	}
	ArrayMemberValue value = new ArrayMemberValue(cp);
	value.setValue(elements);
	AnnotationsAttribute ai = (AnnotationsAttribute) cf
			.getAttribute(visibleTag);
	if (ai == null) {
		ai = new AnnotationsAttribute(cp, visibleTag);
		cf.addAttribute(ai);
	}
	try {
		Annotation annotation = new Annotation(cp, get(type));
		annotation.addMemberValue("value", value);
		ai.addAnnotation(annotation);
	} catch (NotFoundException e) {
		throw new AssertionError(e);
	}
}
 
Example #2
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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
Source File: ClassTemplate.java    From anno4j with Apache License 2.0 5 votes vote down vote up
private boolean equals(CtMethod cm, String name, CtClass[] parameters)
		throws NotFoundException {
	if (!cm.getName().equals(name))
		return false;
	if (Arrays.equals(cm.getParameterTypes(), parameters))
		return true;
	if (cm.getParameterTypes().length != 1)
		return false;
	MethodInfo mi = cm.getMethodInfo();
	AnnotationsAttribute ainfo = (AnnotationsAttribute) mi
			.getAttribute(AnnotationsAttribute.invisibleTag);
	if (ainfo == null)
		return false;
	Annotation[] anno = ainfo.getAnnotations();
	if (anno == null)
		return false;
	String typeName = ParameterTypes.class.getName();
	for (int i = 0; i < anno.length; i++) {
		if (anno[i].getTypeName().equals(typeName)) {
			ArrayMemberValue mv = (ArrayMemberValue) anno[i]
					.getMemberValue("value");
			MemberValue[] mvalues = mv.getValue();
			if (mvalues.length != parameters.length)
				return false;
			for (int j = 0; j < mvalues.length; j++) {
				ClassMemberValue cmv = (ClassMemberValue) mvalues[j];
				if (!parameters[j].getName().equals(cmv.getValue()))
					return false;
			}
			return true;
		}
	}
	return false;
}
 
Example #9
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 #10
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 #11
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 #12
Source File: ClassPathScanner.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public void visitClassMemberValue(ClassMemberValue node) {
  values.add(node.getValue());
}
 
Example #13
Source File: CodeBuilder.java    From anno4j with Apache License 2.0 4 votes vote down vote up
protected ClassMemberValue createClassMemberValue(Class<?> type, ConstPool cp) {
	int idx = cp.addUtf8Info(descriptor(type));
	return new ClassMemberValue(idx, cp);
}
 
Example #14
Source File: AsmRest.java    From fastquery with Apache License 2.0 4 votes vote down vote up
/**
 * 自动生成Repository接口的实现类并以字节的形式返回.
 * 
 * @param repositoryClazz repository class
 * @return 生成的类字节码
 */
public static byte[] generateBytes(Class<?> repositoryClazz) {

	// 生成类
	ClassPool pool = ClassPool.getDefault();
	// web容器中的repository 需要增加classPath
	ClassClassPath classClassPath = new ClassClassPath(repositoryClazz);
	pool.removeClassPath(classClassPath);
	pool.insertClassPath(classClassPath);
	String className = repositoryClazz.getName() + Placeholder.REST_SUF;
	CtClass ctClass = pool.makeClass(className);

	ClassFile ccFile = ctClass.getClassFile();
	ConstPool constpool = ccFile.getConstPool();

	try {
		// 增加接口
		ctClass.setInterfaces(new CtClass[] { pool.get(repositoryClazz.getName()) });

		// 增加字段
		CtClass executor = pool.get(repositoryClazz.getName());
		CtField field = new CtField(executor, "d", ctClass);
		field.setModifiers(Modifier.PRIVATE);
		FieldInfo fieldInfo = field.getFieldInfo();
		// 标识属性注解
		AnnotationsAttribute fieldAttr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
		javassist.bytecode.annotation.Annotation autowired = new javassist.bytecode.annotation.Annotation(
				"javax.inject.Inject", constpool);
		fieldAttr.addAnnotation(autowired);
		fieldInfo.addAttribute(fieldAttr);
		ctClass.addField(field);
		
		AsmRepository.addGetInterfaceClassMethod(repositoryClazz, ctClass);
		
		// 标识类注解
		AnnotationsAttribute classAttr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
		javassist.bytecode.annotation.Annotation singAnn = new javassist.bytecode.annotation.Annotation(
				"javax.inject.Singleton", constpool);
		classAttr.addAnnotation(singAnn);
		ccFile.addAttribute(classAttr);

		// 实现抽象方法
		Method[] methods = repositoryClazz.getMethods();
		for (Method method : methods) {
			if (!method.isDefault()) {
				CtMethod cm = CtMethod.make(AsmRepository.getMethodDef(method) + "{return d."+method.getName()+"($$);}", ctClass);
				// 标识方法注解
				AnnotationsAttribute methodAttr = new AnnotationsAttribute(constpool,
						AnnotationsAttribute.visibleTag);
				javassist.bytecode.annotation.Annotation extendsAnn = new javassist.bytecode.annotation.Annotation(
						"org.fastquery.core.Extends", constpool);
				ArrayMemberValue arrayMemberValue = new ArrayMemberValue(constpool);
				Annotation[] mas = method.getAnnotations();
				ClassMemberValue[] cmvs = new ClassMemberValue[mas.length];
				for (int i = 0; i < mas.length; i++) {
					cmvs[i] = new ClassMemberValue(mas[i].annotationType().getName(), constpool);
				}
				arrayMemberValue.setValue(cmvs);
				extendsAnn.addMemberValue("value", arrayMemberValue);
				methodAttr.addAnnotation(extendsAnn);
				MethodInfo info = cm.getMethodInfo();
				info.addAttribute(methodAttr);
				ctClass.addMethod(cm);
			}
		}
		
		return ctClass.toBytecode();

	} catch (Exception e) {
		throw new ExceptionInInitializerError(e);
	}
}
 
Example #15
Source File: ClassPathScanner.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public void visitClassMemberValue(ClassMemberValue node) {
  values.add(node.getValue());
}
 
Example #16
Source File: ClassExtractorAnnotationMemberValue.java    From smart-testing with Apache License 2.0 4 votes vote down vote up
@Override
public void visitClassMemberValue(ClassMemberValue node) {
    imports.add(node.getValue());
}
 
Example #17
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);
        }
    }
}