private String resolveTypeDiscriminatorForBackReference(Member m) { Method me = ReflectionUtils.findMethod( m.getDeclaringClass(), "get" + firstCharToUpperCase(m.getName())); if (me != null) { return resolveTypeDiscriminator(resolveReturnType(me)); } return ""; }
public ConstraintViolationException( String instanceToString, Iterable<Class<?>> instanceTypes, Member method, Collection<ConstraintViolation> violations) { this.instanceToString = instanceToString; this.instanceTypes = instanceTypes; mixinTypeName = method.getDeclaringClass().getName(); methodName = method.getName(); this.constraintViolations = violations; }
@Override void parse(TransformationSpec spec, AnnotatedElement e) { AdaptField af = e.getAnnotation(AdaptField.class); Member mem = (Member) e; String name = af.name(); if (name.length() == 0) name = mem.getName(); // default to the same name MemberAdapter mrs = null; if (e instanceof Field) mrs = fieldToField((Field) e); if (e instanceof Method) mrs = fieldToMethod((Method) e); assert mrs != null; for (Class was : af.was()) { spec.fields.addRewriteSpec(name, was, mrs); } }
/** * Returns an array containing the available subtypes of the specified type, first looking to * subtypes listed in the annotation, then attempting to find a method using reflection. */ protected Class<?>[] getSubtypes(Class<?> type) { ArrayList<Class<?>> types = new ArrayList<Class<?>>(); // start with the null class, if allowed boolean nullable = getAnnotation().nullable(); if (nullable) { types.add(null); } // look for a subtype annotation and add its types EditorTypes ownAnnotation = getAnnotation(EditorTypes.class); EditorTypes annotation = (ownAnnotation == null) ? type.getAnnotation(EditorTypes.class) : ownAnnotation; if (annotation != null) { addSubtypes(annotation, types); } // get the config key and add the config types String key = (annotation == null) ? type.getName() : annotation.key(); if (StringUtil.isBlank(key)) { if (annotation == ownAnnotation) { Member member = getMember(); key = member.getDeclaringClass().getName() + "." + member.getName(); } else { key = type.getName(); } } Class<?>[] ctypes = _configTypes.get(key); if (ctypes != null) { Collections.addAll(types, ctypes); } // if we don't have at least one non-null class, add the type itself if (types.size() == (nullable ? 1 : 0)) { types.add(type); } // convert to array, return return types.toArray(new Class<?>[types.size()]); }
/** Returns true if the binding annotation is in the wrong place. */ private static boolean checkForMisplacedBindingAnnotations(Member member, Errors errors) { Annotation misplacedBindingAnnotation = Annotations.findBindingAnnotation( errors, member, ((AnnotatedElement) member).getAnnotations()); if (misplacedBindingAnnotation == null) { return false; } // don't warn about misplaced binding annotations on methods when there's a field with the same // name. In Scala, fields always get accessor methods (that we need to ignore). See bug 242. if (member instanceof Method) { try { if (member.getDeclaringClass().getDeclaredField(member.getName()) != null) { return false; } } catch (NoSuchFieldException ignore) { } } errors.misplacedBindingAnnotation(member, misplacedBindingAnnotation); return true; }
private void generateSetBasedDocRefView( Map<String, org.ektorp.support.DesignDocument.View> views, Member me, DocumentReferences referenceMetaData) { String fieldName = firstCharToLowerCase(me.getName()); String orderBy = referenceMetaData.orderBy(); String backRef = referenceMetaData.backReference(); if (backRef.length() == 0) { throw new ViewGenerationException( String.format( "The DocumentReferences annotation in %s must specify a backReference", me.getDeclaringClass())); } String viewName = NameConventions.backReferenceViewName(fieldName); String typeDiscriminator = resolveTypeDiscriminatorForBackReference(me); if (orderBy.length() > 0) { views.put( viewName, generateDocRefsAsSetWithOrderByView(backRef, fieldName, orderBy, typeDiscriminator)); } else { views.put(viewName, generateDocRefsAsSetView(backRef, fieldName, typeDiscriminator)); } }
@Override String getName() { return member.getName(); }
public String getName() { return member.getName(); }
public String determineMemberDescription(Member member) { return member.getDeclaringClass().getName() + '#' + member.getName(); }
private IAnnotationModel processAnnotation(CtAnnotation<? extends Annotation> annotation) { String simpleName = annotation.getActualAnnotation().annotationType().getSimpleName(); AnnotationModel annotationModel = new AnnotationModel(); annotationModel.setName(simpleName); Map<String, Object> elementValues = annotation.getElementValues(); for (Map.Entry<String, Object> entry : elementValues.entrySet()) { String key = entry.getKey(); if (key == null) { continue; } Object value = entry.getValue(); ArrayList<CtAnnotation<?>> annotationList = toCtAnnotationList(value); if (annotationList != null) { int size = annotationList.size(); IAnnotationModel[] annotationModels = new IAnnotationModel[size]; for (int i = 0; i < size; i++) { CtAnnotation<?> subAnnotation = annotationList.get(i); IAnnotationModel subAnnotationModel = processAnnotation(subAnnotation); annotationModels[i] = subAnnotationModel; } annotationModel.addValue(key, annotationModels); } else if (value instanceof String[]) { annotationModel.addValue(key, value); } else { if (value instanceof CtNewArray<?>) { List<?> elements = ((CtNewArray<?>) value).getElements(); int size = elements.size(); Object[] arr = new Object[size]; for (int i = 0; i < size; i++) { Object elem = elements.get(i); if (elem instanceof CtCodeElement) { PartialEvaluator eval = factory.Eval().createPartialEvaluator(); arr[i] = eval.evaluate(null, (CtCodeElement) elem); } else { arr[i] = elem; } } value = arr; } if (value instanceof CtCodeElement) { PartialEvaluator eval = factory.Eval().createPartialEvaluator(); value = eval.evaluate(null, (CtCodeElement) value); } if (value instanceof CtLiteral<?>) { value = ((CtLiteral<?>) value).getValue().toString(); } else if (value instanceof CtFieldReference<?>) { Member member = ((CtFieldReference<?>) value).getActualField(); // if field references a static final String, use string's value if (member instanceof Field) { Field field = (Field) member; int mod = field.getModifiers(); if (Modifier.isStatic(mod) && Modifier.isFinal(mod)) { field.setAccessible(true); try { value = field.get(null).toString(); } catch (Throwable t) { value = member.getName(); // NOOP tolerate any exception/error, and fall back to using name of field below } } // fall back to using name of field reference else { value = member.getName(); } } } else if (value.getClass().isArray()) { int length = Array.getLength(value); String[] arr = new String[length]; for (int i = 0; i < length; i++) { Object elem = Array.get(value, i); String sVal = elem.toString(); if (elem instanceof CtLiteral<?>) { sVal = ((CtLiteral<?>) elem).getValue().toString(); } else if (elem instanceof CtFieldReference<?>) { sVal = ((CtFieldReference<?>) elem).getActualField().getName(); } arr[i] = sVal; } value = arr; } else { value = value.toString(); } if (value == null) { value = "null"; } annotationModel.addValue(key, value); } } return annotationModel; }
public static String getPropertyName(Member member) { if (member instanceof Method) { return getPropertyName((Method) member); } return member.getName(); }