/** * Tries to find parametrized type in interface of a class. * * @param classType * A class having parametrized interface. * @param parametrizedType * Interface where parametrized type is present. * @param position * Position in a list of parametrized types in interface. * @return Generics type in interface implemented by a class type. * @throws NotFoundException */ public static JClassType getGenericsFromInterfaceType(JClassType classType, JClassType parametrizedType, int position) throws NotFoundException { JClassType type = getGenericsFromInterfaceHierarchy(classType, parametrizedType, position); if (type != null) return type; if (classType.getSuperclass() != null) { return getGenericsFromInterfaceType(classType.getSuperclass(), parametrizedType, position); } throw new NotFoundException("Unable to find generics in type " + classType + ", where parametrized type is " + parametrizedType + " on position " + position); }
@Override public String doGenerate(TreeLogger logger, GeneratorContext context, String typeClass) throws UnableToCompleteException { this.logger = logger; this.context = context; typeOracle = context.getTypeOracle(); type = typeOracle.findType(typeClass); JClassType beanWrapperType = typeOracle.findType(BEAN_WRAPPER_FQN); JClassType[] directImplementors = beanWrapperType.getSubtypes(); List<JClassType> beanList = new ArrayList<JClassType>(); for (JClassType wrapper : directImplementors) { try { beanList.add(RebindUtils.getGenericsFromInterfaceType(wrapper, beanWrapperType, 0)); } catch (NotFoundException e) { logger.log(Type.WARN, "Skipping wrapper = " + wrapper + " because cannot get generics argument"); } } String packageName = type.getPackage().getName(); String genTypeName = type.getSimpleSourceName() + "Generated"; String fqn = packageName + "." + genTypeName; SourceWriter sourceWriter = getSourceWriter(packageName, genTypeName, type); if (sourceWriter == null) { return fqn; } BeanPropertyDescriptorCreator creator = new BeanPropertyDescriptorCreator(); creator.write(logger, sourceWriter, beanList); sourceWriter.commit(logger); return fqn; }
@Override protected boolean initialize() throws UnableToCompleteException { try { bindingBeanClassType = RebindUtils.getGenericsFromInterfaceType(classType, typeOracle .findType(IBeanBindingHolder.class.getName()), 0); } catch (NotFoundException e) { logger.log(Type.ERROR, "Unable to extract generic type class from interface"); throw new UnableToCompleteException(); } // initialize binding context for all related creators BindingCreatorFactory.setBindingContext(bindingBeanClassType, packageName, namingStrategy); BindingFieldsBase bindingFieldsBaseAnnotation = classType.getAnnotation(BindingFieldsBase.class); if (bindingFieldsBaseAnnotation == null) { return false; } validationEnabled = isValidationEnabled(bindingFieldsBaseAnnotation); setDefaultLoaderCreator(); try { BindingCreatorFactory.fillSupportedTypes(context.getTypeOracle()); } catch (GeneratorException e) { logger.log(Type.ERROR, e.getMessage()); throw new UnableToCompleteException(); } return true; }