/** * Return the descriptor class for a given type. */ private String getDescriptorClassName(TypeElement type) { return BindableDescriptorUtils.getDescriptorClassName( elements.getBinaryName(type).toString()); }
/** * @return Returns a descriptor instance for a class marked with {@link Bindable}. * @throws IllegalArgumentException If the class is not annotated with {@link Bindable}. * @throws NoClassDefFoundError If the descriptor cannot be found. * @throws RuntimeException If instantiation fails for some reason. */ public static IBindableDescriptor getDescriptor(Class<?> clazz) { try { return getDescriptorClass(clazz).newInstance(); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }
/** * Return the mapped descriptor class name for a given class name (descriptors for nested * classes become top-level classes). */ public static String getDescriptorClassName(String className) { String packageName = getPackageName(className); // Nested classes will get a top level descriptor. String shortClassName; if (packageName.isEmpty()) { shortClassName = className; } else { shortClassName = className.substring(packageName.length() + 1); } int dollarIndex = shortClassName.lastIndexOf("$"); if (dollarIndex >= 0) { shortClassName = shortClassName.substring(dollarIndex + 1); } return packageName + (packageName.isEmpty() ? "" : ".") + shortClassName + "Descriptor"; }
/** * Load metadata of a given bindable class, merged with bindable attributes from * parent classes marked with {@link Bindable} as well. */ public static BindableMetadata forClassWithParents(final Class<?> clazz) { IBindableDescriptor descriptor = BindableDescriptorUtils.getDescriptor(clazz); final BindableMetadata bindable = new BindableMetadata(); bindable.setDescription(descriptor.getDescription()); bindable.setLabel(descriptor.getLabel()); bindable.setTitle(descriptor.getTitle()); bindable.setAttributeMetadata( asAttributeMetadata(descriptor.getAttributes())); return bindable; }
/** * @return Returns the descriptor class for a class marked with {@link Bindable}. * @throws IllegalArgumentException If the class is not annotated with {@link Bindable}. * @throws NoClassDefFoundError If the descriptor cannot be found. */ @SuppressWarnings("unchecked") public static Class<? extends IBindableDescriptor> getDescriptorClass(Class<?> clazz) { if (clazz.getAnnotation(Bindable.class) == null) throw new IllegalArgumentException("Class not marked with @Bindable: " + clazz.getName()); ClassLoader cl = clazz.getClassLoader(); String descriptorClassName = getDescriptorClassName(clazz.getName()); try { return (Class<? extends IBindableDescriptor>) Class.forName(descriptorClassName, true, cl); } catch (ClassNotFoundException e) { throw new NoClassDefFoundError("Descriptor class not found: " + descriptorClassName); } }