public static DialogFieldConfig getDialogFieldFromSuperClasses(CtMethod method) throws NotFoundException, ClassNotFoundException, InvalidComponentClassException { DialogFieldConfig dialogFieldConfig = null; List<CtClass> classes = new ArrayList<CtClass>(); CtClass clazz = method.getDeclaringClass(); classes.add(clazz); while (clazz.getSuperclass() != null) { classes.add(clazz.getSuperclass()); clazz = clazz.getSuperclass(); } Collections.reverse(classes); CtMember interfaceMember = getMemberForAnnotatedInterfaceMethod(method); if (interfaceMember != null) { dialogFieldConfig = new DialogFieldConfig( (DialogField) interfaceMember.getAnnotation(DialogField.class), interfaceMember); } for (CtClass ctclass : classes) { try { CtMethod superClassMethod = ctclass.getDeclaredMethod(method.getName(), method.getParameterTypes()); if (superClassMethod.hasAnnotation(DialogField.class)) { dialogFieldConfig = new DialogFieldConfig( (DialogField) superClassMethod.getAnnotation(DialogField.class), superClassMethod); } else if (superClassMethod.hasAnnotation(DialogFieldOverride.class)) { mergeDialogFields(dialogFieldConfig, superClassMethod); } } catch (NotFoundException e) { } } return dialogFieldConfig; }
private static CtMember getMemberForAnnotatedInterfaceMethod(CtMethod member) throws InvalidComponentClassException, ClassNotFoundException, NotFoundException { CtMethod newMember = null; List<CtClass> interfaces = new ArrayList<CtClass>(); CtClass clazz = member.getDeclaringClass(); while (clazz != null) { interfaces.addAll(Arrays.asList(clazz.getInterfaces())); clazz = clazz.getSuperclass(); } for (CtClass ctclass : interfaces) { try { CtMethod newMethodMember = ctclass.getDeclaredMethod(member.getName(), member.getParameterTypes()); DialogField tempDialogProperty = (DialogField) newMethodMember.getAnnotation(DialogField.class); if (tempDialogProperty != null) { if (newMember == null) { newMember = newMethodMember; } else { throw new InvalidComponentClassException( "Class has multiple interfaces that have the same method signature annotated"); } } } catch (NotFoundException e) { } } if (newMember != null) { member = newMember; } return newMember; }
@SuppressWarnings("unchecked") private <T extends Annotation> T getMethodAnnotation(CtMethod ctMethod, Class<T> annotation) { try { return (T) ctMethod.getAnnotation(annotation); } catch (ClassNotFoundException e) { // should never happen throw new GwtTestPatchException(e); } }
private CtMethod findPatchMethod(CtMethod m) throws Exception { for (CtMethod patchMethod : patchMethods) { PatchMethod annotation = (PatchMethod) patchMethod.getAnnotation(PatchMethod.class); String methodName = (annotation.value().length() > 0) ? annotation.value() : patchMethod.getName(); if (m.getName().equals(methodName) && hasCompatibleSignature(m, patchMethod)) { return patchMethod; } } return null; }
private static void mergeDialogFields(DialogFieldConfig dialogFieldConfig, CtMethod method) throws ClassNotFoundException { if (dialogFieldConfig != null && method.hasAnnotation(DialogFieldOverride.class)) { DialogFieldOverride dialogField = (DialogFieldOverride) method.getAnnotation(DialogFieldOverride.class); if (StringUtils.isNotEmpty(dialogField.fieldLabel())) { dialogFieldConfig.setFieldLabel(dialogField.fieldLabel()); } if (StringUtils.isNotEmpty(dialogField.fieldDescription())) { dialogFieldConfig.setFieldDescription(dialogField.fieldDescription()); } dialogFieldConfig.setRequired(dialogField.required()); dialogFieldConfig.setHideLabel(dialogField.hideLabel()); if (StringUtils.isNotEmpty(dialogField.defaultValue())) { dialogFieldConfig.setDefaultValue(dialogField.defaultValue()); } if (StringUtils.isNotEmpty(dialogField.name())) { dialogFieldConfig.setName(dialogField.name()); } dialogFieldConfig.setTab(dialogField.tab()); dialogFieldConfig.setRanking(dialogField.ranking()); if (dialogField.additionalProperties().length > 0) { List<FieldProperty> properties = new ArrayList<FieldProperty>(); properties.addAll(Arrays.asList(dialogField.additionalProperties())); if (dialogField.mergeAdditionalProperties()) { properties.addAll(Arrays.asList(dialogFieldConfig.getAdditionalProperties())); } dialogFieldConfig.setAdditionalProperties( properties.toArray(new FieldProperty[properties.size()])); } if (dialogField.listeners().length > 0) { List<Listener> listeners = new ArrayList<Listener>(); listeners.addAll(Arrays.asList(dialogField.listeners())); if (dialogField.mergeAdditionalProperties()) { listeners.addAll(Arrays.asList(dialogFieldConfig.getListeners())); } dialogFieldConfig.setListeners(listeners.toArray(new Listener[listeners.size()])); } } }
private static void populateMetricsAndOperations( List<Class<?>> classes, Props props, boolean withNamePrefix) throws Exception { props.setHasOperations(true); props.setHasMetrics(true); for (Class<?> clazz : classes) { MBean mbean = clazz.getAnnotation(MBean.class); String prefix = withNamePrefix ? mbean.objectName() + '.' : ""; CtClass ctClass = classPool.get(clazz.getName()); CtMethod[] ctMethods = ctClass.getMethods(); for (CtMethod ctMethod : ctMethods) { ManagedAttribute managedAttr = (ManagedAttribute) ctMethod.getAnnotation(ManagedAttribute.class); ManagedOperation managedOp = (ManagedOperation) ctMethod.getAnnotation(ManagedOperation.class); Metric rhqMetric = (Metric) ctMethod.getAnnotation(Metric.class); if (rhqMetric != null) { debug("Metric annotation found " + rhqMetric); // Property and description resolution are the reason why annotation scanning is done // here. // These two fields are calculated from either the method name or the Managed* // annotations, // and so, only the infinispan side knows about that. String property = prefix + getPropertyFromBeanConvention(ctMethod); if (!rhqMetric.property().isEmpty()) { property = prefix + rhqMetric.property(); } MetricProps metric = new MetricProps(property); String displayName = withNamePrefix ? "[" + mbean.objectName() + "] " + rhqMetric.displayName() : rhqMetric.displayName(); metric.setDisplayName(displayName); metric.setDisplayType(rhqMetric.displayType()); metric.setDataType(rhqMetric.dataType()); metric.setUnits(rhqMetric.units()); if (managedAttr != null) { debug("Metric has ManagedAttribute annotation " + managedAttr); metric.setDescription(managedAttr.description()); } else if (managedOp != null) { debug("Metric has ManagedOperation annotation " + managedOp); metric.setDescription(managedOp.description()); } else { log.debug( "Metric has no managed annotations, so take the description from the display name."); metric.setDescription(rhqMetric.displayName()); } props.getMetrics().add(metric); } Operation rhqOperation = (Operation) ctMethod.getAnnotation(Operation.class); if (rhqOperation != null) { debug("Operation annotation found " + rhqOperation); String name; if (!rhqOperation.name().isEmpty()) { name = prefix + rhqOperation.name(); } else { name = prefix + ctMethod.getName(); } OperationProps operation = new OperationProps(name); String displayName = withNamePrefix ? "[" + mbean.objectName() + "] " + rhqOperation.displayName() : rhqOperation.displayName(); operation.setDisplayName(displayName); if (managedAttr != null) { debug("Operation has ManagedAttribute annotation " + managedAttr); operation.setDescription(managedAttr.description()); } else if (managedOp != null) { debug("Operation has ManagedOperation annotation " + managedOp); operation.setDescription(managedOp.description()); } else { debug( "Operation has no managed annotations, so take the description from the display name."); operation.setDescription(rhqOperation.displayName()); } Object[][] paramAnnotations = ctMethod.getParameterAnnotations(); int i = 0; for (Object[] paramAnnotationsInEach : paramAnnotations) { boolean hadParameter = false; for (Object annot : paramAnnotationsInEach) { debug("Parameter annotation " + annot); if (annot instanceof Parameter) { Parameter param = (Parameter) annot; SimpleProperty prop = new SimpleProperty(param.name()); prop.setDescription(param.description()); operation.getParams().add(prop); hadParameter = true; } } if (!hadParameter) { operation.getParams().add(new SimpleProperty("p" + i++)); } } CtClass returnType = ctMethod.getReturnType(); if (!returnType.equals(CtClass.voidType)) { if (!returnType.equals(Void.TYPE)) { SimpleProperty prop = new SimpleProperty("operationResult"); operation.setResult(prop); } } props.getOperations().add(operation); } } CtField[] ctFields = ctClass.getDeclaredFields(); for (CtField ctField : ctFields) { debug("Inspecting field " + ctField); Metric rhqMetric = (Metric) ctField.getAnnotation(Metric.class); if (rhqMetric != null) { debug("Field " + ctField + " contains Metric annotation " + rhqMetric); String property; if (!rhqMetric.property().isEmpty()) { property = prefix + rhqMetric.property(); } else { property = prefix + getPropertyFromBeanConvention(ctField); } MetricProps metric = new MetricProps(property); String displayName = withNamePrefix ? "[" + mbean.objectName() + "] " + rhqMetric.displayName() : rhqMetric.displayName(); metric.setDisplayName(displayName); metric.setDisplayType(rhqMetric.displayType()); metric.setDataType(rhqMetric.dataType()); metric.setUnits(rhqMetric.units()); ManagedAttribute managedAttr = (ManagedAttribute) ctField.getAnnotation(ManagedAttribute.class); if (managedAttr != null) { debug("Metric has ManagedAttribute annotation " + managedAttr); metric.setDescription(managedAttr.description()); } else { log.debug( "Metric has no managed annotations, so take the description from the display name."); metric.setDescription(rhqMetric.displayName()); } props.getMetrics().add(metric); } } } }