Java Code Examples for javassist.bytecode.annotation.Annotation

The following examples show how to use javassist.bytecode.annotation.Annotation. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
private static AttributeInfo constructAttribute(AttributeInfo info, ConstPool cp, int fields, int fieldIdx, String annotationName, JavassistAnnParam... params) {
    ParameterAnnotationsAttribute attribute = (ParameterAnnotationsAttribute) info;
    if (attribute == null)
        attribute = new ParameterAnnotationsAttribute(cp, ParameterAnnotationsAttribute.visibleTag);

    Annotation[][] annotations = attribute.getAnnotations();
    if (annotations == null || annotations.length == 0)
        annotations = new Annotation[fields][];
    for (int i = 0; i < annotations.length; i++)
        if (annotations[i] == null)
            annotations[i] = new Annotation[0];

    Annotation[] oldA = annotations[fieldIdx];
    annotations[fieldIdx] = new Annotation[oldA.length + 1];
    System.arraycopy(oldA, 0, annotations[fieldIdx], 0, oldA.length);
    annotations[fieldIdx][oldA.length] = constructAnnotation(cp, annotationName, params);

    attribute.setAnnotations(annotations);
    return attribute;
}
 
Example 2
Source Project: minnal   Source File: AbstractMethodCreator.java    License: 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 3
@SuppressWarnings("unchecked")
public Class<? extends Supplier<T>> build() {
    ClassPool classPool = ClassPool.getDefault();
    CtClass classBuilder = classPool.makeClass(providerClass.getCanonicalName() + "$Bride" + INDEX.incrementAndGet());
    try {
        classBuilder.addInterface(classPool.get(Supplier.class.getName()));
        ConstPool constPool = classBuilder.getClassFile().getConstPool();
        CtField field = CtField.make(String.format("%s provider;", Types.className(providerClass)), classBuilder);
        Annotation ctAnnotation = new Annotation(Inject.class.getCanonicalName(), constPool);
        AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
        attr.addAnnotation(ctAnnotation);
        field.getFieldInfo().addAttribute(attr);
        classBuilder.addField(field);

        CtMethod ctMethod = CtMethod.make("public Object get() {return provider.get();}", classBuilder);
        classBuilder.addMethod(ctMethod);

        return classBuilder.toClass();
    } catch (CannotCompileException | NotFoundException e) {
        throw new ApplicationException("failed to create provider bride, providerType={}", providerClass, e);
    }
}
 
Example 4
Source Project: japicmp   Source File: CtMethodBuilder.java    License: Apache License 2.0 6 votes vote down vote up
public CtMethod addToClass(CtClass declaringClass) throws CannotCompileException {
	if (this.returnType == null) {
		this.returnType = declaringClass;
	}
	CtMethod ctMethod = CtNewMethod.make(this.modifier, this.returnType, this.name, this.parameters, this.exceptions, this.body, declaringClass);
	ctMethod.setModifiers(this.modifier);
	declaringClass.addMethod(ctMethod);
	for (String annotation : annotations) {
		ClassFile classFile = declaringClass.getClassFile();
		ConstPool constPool = classFile.getConstPool();
		AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
		Annotation annot = new Annotation(annotation, constPool);
		attr.setAnnotation(annot);
		ctMethod.getMethodInfo().addAttribute(attr);
	}
	return ctMethod;
}
 
Example 5
Source Project: anno4j   Source File: ClassTemplate.java    License: 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 6
Source Project: panda   Source File: JavassistAdapter.java    License: Apache License 2.0 6 votes vote down vote up
private List<String> getAnnotationNames(AnnotationsAttribute... annotationsAttributes) {
    List<String> result = new ArrayList<>();

    if (annotationsAttributes == null) {
        return result;
    }

    for (AnnotationsAttribute annotationsAttribute : annotationsAttributes) {
        if (annotationsAttribute == null) {
            continue;
        }

        for (Annotation annotation : annotationsAttribute.getAnnotations()) {
            result.add(annotation.getTypeName());
        }
    }

    return result;
}
 
Example 7
Source Project: gecco   Source File: JavassistDynamicField.java    License: 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 8
Source Project: gecco   Source File: JavassistDynamicField.java    License: 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 9
Source Project: statefulj   Source File: SpringMVCBinder.java    License: 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 10
Source Project: statefulj   Source File: JavassistUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static void addClassAnnotation(CtClass clazz, Class<?> annotationClass, Object... values) {
	ClassFile ccFile = clazz.getClassFile();
	ConstPool constPool = ccFile.getConstPool();
	AnnotationsAttribute attr = getAnnotationsAttribute(ccFile);
	Annotation annot = new Annotation(annotationClass.getName(), constPool);
	
	for(int i = 0; i < values.length; i = i + 2) {
		String valueName = (String)values[i];
		Object value = values[i+1];
		if (valueName != null && value != null) {
			MemberValue memberValue = createMemberValue(constPool, value);
			annot.addMemberValue(valueName, memberValue);
		}
	}
	
	attr.addAnnotation(annot);
}
 
Example 11
Source Project: statefulj   Source File: JavassistUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static void addMethodAnnotations(CtMethod ctMethod, Method method) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
	if (method != null) {
		MethodInfo methodInfo = ctMethod.getMethodInfo();
		ConstPool constPool = methodInfo.getConstPool();
		AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
		methodInfo.addAttribute(attr);
		for(java.lang.annotation.Annotation anno : method.getAnnotations()) {

			// If it's a Transition skip
			// TODO : Make this a parameterized set of Filters instead of hardcoding
			//
			Annotation clone = null;
			if (anno instanceof Transitions || anno instanceof Transition) {
				// skip
			} else {
				clone = cloneAnnotation(constPool, anno);
				attr.addAnnotation(clone);
			}
		}
	}
}
 
Example 12
Source Project: statefulj   Source File: JavassistUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static List<Method> getMethodsAnnotatedWith(final Class<?> type, final Class<? extends java.lang.annotation.Annotation> annotation) {
    final List<Method> methods = new ArrayList<Method>();
    Class<?> clazz = type;
    while (clazz != Object.class) { // need to iterated thought hierarchy in order to retrieve methods from above the current instance
        // iterate though the list of methods declared in the class represented by clazz variable, and add those annotated with the specified annotation
        final List<Method> allMethods = new ArrayList<Method>(Arrays.asList(clazz.getDeclaredMethods()));       
        for (final Method method : allMethods) {
            if (annotation == null || method.isAnnotationPresent(annotation)) {
                methods.add(method);
            }
        }
        // move to the upper class in the hierarchy in search for more methods
        clazz = clazz.getSuperclass();
    }
    return methods;
}
 
Example 13
Source Project: statefulj   Source File: AbstractRestfulBinder.java    License: 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 14
Source Project: minnal   Source File: CreateMethodCreator.java    License: 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 15
Source Project: geowave   Source File: JavassistUtilsTest.java    License: Apache License 2.0 6 votes vote down vote up
private void annotateField(
    final CtField ctfield,
    final String annotationName,
    final int annotationValue) {
  final AnnotationsAttribute attr =
      new AnnotationsAttribute(
          ctfield.getFieldInfo().getConstPool(),
          AnnotationsAttribute.visibleTag);
  final Annotation anno =
      new Annotation("java.lang.Integer", ctfield.getFieldInfo().getConstPool());
  anno.addMemberValue(
      annotationName,
      new IntegerMemberValue(ctfield.getFieldInfo().getConstPool(), annotationValue));
  attr.addAnnotation(anno);

  ctfield.getFieldInfo().addAttribute(attr);
}
 
Example 16
Source Project: jadira   Source File: JField.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Set<JAnnotation<?>> getAnnotations() {

    AnnotationsAttribute visible = (AnnotationsAttribute) fieldInfo.getAttribute(AnnotationsAttribute.visibleTag);
    AnnotationsAttribute invisible = (AnnotationsAttribute) fieldInfo.getAttribute(AnnotationsAttribute.invisibleTag);

    Set<JAnnotation<?>> annotations = new HashSet<JAnnotation<?>>();

    List<Annotation> annotationsList = new ArrayList<Annotation>();
    if (visible != null) {
        annotationsList.addAll(Arrays.asList(visible.getAnnotations()));
    }
    if (invisible != null) {
        annotationsList.addAll(Arrays.asList(invisible.getAnnotations()));
    }

    for (Annotation nextAnnotation : annotationsList) {
        annotations.add(JAnnotation.getJAnnotation(nextAnnotation, this, getResolver()));
    }

    return annotations;
}
 
Example 17
Source Project: jadira   Source File: JClass.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Set<JAnnotation<?>> getAnnotations() {

    AnnotationsAttribute visible = (AnnotationsAttribute) getClassFile().getAttribute(AnnotationsAttribute.visibleTag);
    AnnotationsAttribute invisible = (AnnotationsAttribute) getClassFile().getAttribute(AnnotationsAttribute.invisibleTag);

    Set<JAnnotation<?>> annotations = new HashSet<JAnnotation<?>>();

    List<Annotation> annotationsList = new ArrayList<Annotation>();
    if (visible != null) {
        annotationsList.addAll(Arrays.asList(visible.getAnnotations()));
    }
    if (invisible != null) {
        annotationsList.addAll(Arrays.asList(invisible.getAnnotations()));
    }

    for (Annotation nextAnnotation : annotationsList) {
        annotations.add(JAnnotation.getJAnnotation(nextAnnotation, this, getResolver()));
    }

    return annotations;
}
 
Example 18
Source Project: japicmp   Source File: JApiAnnotation.java    License: Apache License 2.0 6 votes vote down vote up
private Map<String, Optional<MemberValue>> buildMemberValueMap(Annotation annotation) {
	Map<String, Optional<MemberValue>> map = new HashMap<>();
	@SuppressWarnings("unchecked")
	Set<String> memberNames = annotation.getMemberNames();
	if (memberNames != null) {
		for (String memberName : memberNames) {
			MemberValue memberValue = annotation.getMemberValue(memberName);
			if (memberValue == null) {
				map.put(memberName, Optional.<MemberValue>absent());
			} else {
				map.put(memberName, Optional.of(memberValue));
			}
		}
	}
	return map;
}
 
Example 19
Source Project: jadira   Source File: JAnnotation.java    License: Apache License 2.0 6 votes vote down vote up
public A getActualAnnotation() throws ClasspathAccessException {

        final java.lang.annotation.Annotation[] annotations;
        if (enclosingElement instanceof JOperation) {
            annotations = JavassistAnnotationsHelper.getAnnotationsForMethod(((JOperation)enclosingElement).getMethodInfo());
        } else if (enclosingElement instanceof JField) {
            annotations = JavassistAnnotationsHelper.getAnnotationsForFieldInfo(((JField)enclosingElement).getFieldInfo());
        } else if (enclosingElement instanceof JParameter) {
            annotations = JavassistAnnotationsHelper.getAnnotationsForMethodParameter(((JMethod)enclosingElement).getMethodInfo(), ((JParameter)enclosingElement).getIndex());
        } else if (enclosingElement instanceof JPackage) {
            annotations = ((JPackage)enclosingElement).getAnnotationsForPackage();
        } else {
            annotations = JavassistAnnotationsHelper.getAnnotationsForClass(((JType)enclosingElement).getClassFile());
        }

        String requiredName = getActualClass().getName();
        for (java.lang.annotation.Annotation next : annotations) {
            String nextName = next.annotationType().getName();
            if (nextName.equals(requiredName)) {
                @SuppressWarnings("unchecked") final A retVal = (A) next;
                return retVal;
            }
        }
        throw new ClasspathAccessException("Could not find annotation of type " + getActualClass() + " for " + enclosingElement);
    }
 
Example 20
Source Project: jadira   Source File: JInterface.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Set<JAnnotation<?>> getAnnotations() throws ClasspathAccessException {

    AnnotationsAttribute visible = (AnnotationsAttribute) getClassFile().getAttribute(AnnotationsAttribute.visibleTag);
    AnnotationsAttribute invisible = (AnnotationsAttribute) getClassFile().getAttribute(AnnotationsAttribute.invisibleTag);

    Set<JAnnotation<?>> annotations = new HashSet<JAnnotation<?>>();

    List<Annotation> annotationsList = new ArrayList<Annotation>();
    if (visible != null) {
        annotationsList.addAll(Arrays.asList(visible.getAnnotations()));
    }
    if (invisible != null) {
        annotationsList.addAll(Arrays.asList(invisible.getAnnotations()));
    }

    for (Annotation nextAnnotation : annotationsList) {
        annotations.add(JAnnotation.getJAnnotation(nextAnnotation, this, getResolver()));
    }

    return annotations;
}
 
Example 21
Source Project: swagger-more   Source File: ClassUtils.java    License: 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 22
Source Project: deadcode4j   Source File: AnnotationsAnalyzer.java    License: Apache License 2.0 5 votes vote down vote up
private void addAnnotations(@Nonnull CtClass clazz, Set<String> knownAnnotations) {
    for (Annotation annotation : getAnnotations(clazz, PACKAGE, TYPE)) {
        String annotationClassName = annotation.getTypeName();
        if (!knownAnnotations.add(annotationClassName)) {
            continue;
        }
        if (DEAD_ENDS.contains(annotationClassName)) {
            continue;
        }
        CtClass annotationClazz = getCtClass(clazz.getClassPool(), annotationClassName);
        if (annotationClazz != null) {
            addAnnotations(annotationClazz, knownAnnotations);
        }
    }
}
 
Example 23
Source Project: flower   Source File: ClassGenerator.java    License: 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 24
Source Project: minnal   Source File: ListMethodCreator.java    License: 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 25
Source Project: steady   Source File: ClassVisitor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Adds the given annotations to the given field. Can be used to avoid problems with OR mappers by adding
 * an annotation "javax.persistence.Transient".
 * @param _fld
 * @param _annotations
 */
private void addFieldAnnotations(CtField _fld, String[] _annotations) {
	if(_annotations!=null && _annotations.length>0) {
		final ConstPool cpool = this.c.getClassFile().getConstPool();
		final AnnotationsAttribute attr = new AnnotationsAttribute(cpool, AnnotationsAttribute.visibleTag);
		for(String anno: _annotations) {
			final Annotation annot = new Annotation(anno, cpool);
			attr.addAnnotation(annot);
		}
		_fld.getFieldInfo().addAttribute(attr);
	}
}
 
Example 26
Source Project: smart-testing   Source File: JavaAssistClass.java    License: Apache License 2.0 5 votes vote down vote up
private void addParameterAnnotationsFor(Collection<String> imports, MethodInfo methodInfo, String tag) {
    AttributeInfo attribute = methodInfo.getAttribute(tag);
    ParameterAnnotationsAttribute annotationAttribute = (ParameterAnnotationsAttribute) attribute;
    if (annotationAttribute != null) {
        Annotation[][] parameters = annotationAttribute.getAnnotations();
        for (Annotation[] annotations : parameters) {
            for (Annotation annotation : annotations) {
                imports.add(annotation.getTypeName());
            }
        }
    }
}
 
Example 27
Source Project: tsharding   Source File: MapperAnnotationEnhancer.java    License: MIT License 5 votes vote down vote up
public static ParameterAnnotationsAttribute duplicateParameterAnnotationsAttribute(ConstPool cp, Method method) {
    ParameterAnnotationsAttribute oldAns = new ParameterAnnotationsAttribute(cp, ParameterAnnotationsAttribute.visibleTag);
    javassist.bytecode.annotation.Annotation[][] anAr = new javassist.bytecode.annotation.Annotation[method.getParameterAnnotations().length][];
    for (int i = 0; i < anAr.length; ++i) {
        anAr[i] = new javassist.bytecode.annotation.Annotation[method.getParameterAnnotations()[i].length];
        for (int j = 0; j < anAr[i].length; ++j) {
            anAr[i][j] = createJavassistAnnotation(method.getParameterAnnotations()[i][j], cp);
        }
    }
    oldAns.setAnnotations(anAr);
    return oldAns;
}
 
Example 28
Source Project: minnal   Source File: CreateMethodCreator.java    License: 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 29
Source Project: anno4j   Source File: ClassTemplate.java    License: 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 30
Source Project: minnal   Source File: DeleteMethodCreator.java    License: 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;
}