- new(GeneratedType genType) {
- super(new TopLevelJavaGeneratedType(builderName(genType), genType), genType)
- this.properties = propertiesFromMethods(createMethods)
- keyType = genType.key
- }
-
- def static builderName(GeneratedType genType) {
- val name = genType.identifier
- name.createSibling(name.simpleName + "Builder")
- }
-
- /**
- * Returns set of method signature instances which contains all the methods of the <code>genType</code>
- * and all the methods of the implemented interfaces.
- *
- * @returns set of method signature instances
- */
- def private Set<MethodSignature> createMethods() {
- val Set<MethodSignature> methods = new LinkedHashSet();
- methods.addAll(type.methodDefinitions)
- collectImplementedMethods(methods, type.implements)
- val Set<MethodSignature> sortedMethods = ImmutableSortedSet.orderedBy(METHOD_COMPARATOR).addAll(methods).build()
-
- return sortedMethods
- }
-
- /**
- * Adds to the <code>methods</code> set all the methods of the <code>implementedIfcs</code>
- * and recursively their implemented interfaces.
- *
- * @param methods set of method signatures
- * @param implementedIfcs list of implemented interfaces
- */
- def private void collectImplementedMethods(Set<MethodSignature> methods, List<Type> implementedIfcs) {
- if (implementedIfcs === null || implementedIfcs.empty) {
- return
- }
- for (implementedIfc : implementedIfcs) {
- if ((implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject))) {
- val ifc = implementedIfc as GeneratedType
- methods.addAll(ifc.methodDefinitions)
- collectImplementedMethods(methods, ifc.implements)
- } else if (implementedIfc.fullyQualifiedName == Augmentable.name) {
- for (m : Augmentable.methods) {
- if (m.name == GET_AUGMENTATION_METHOD_NAME) {
- val identifier = JavaTypeName.create(m.returnType)
- val tmpGenTO = new CodegenGeneratedTOBuilder(identifier)
- val refType = new ReferencedTypeImpl(identifier)
- val generic = new ReferencedTypeImpl(type.identifier)
- val parametrizedReturnType = Types.parameterizedTypeFor(refType, generic)
- tmpGenTO.addMethod(m.name).setReturnType(parametrizedReturnType)
- augmentField = tmpGenTO.build.methodDefinitions.first.propertyFromGetter
- }
- }
- }
- }
- }
-
- /**
- * Returns the first element of the list <code>elements</code>.
- *
- * @param list of elements
- */
- def private <E> first(List<E> elements) {
- elements.get(0)
- }
-
- /**
- * Creates set of generated property instances from getter <code>methods</code>.
- *
- * @param set of method signature instances which should be transformed to list of properties
- * @return set of generated property instances which represents the getter <code>methods</code>
- */
- def private propertiesFromMethods(Collection<MethodSignature> methods) {
- if (methods === null || methods.isEmpty()) {
- return Collections.emptySet
- }
- val Set<GeneratedProperty> result = new LinkedHashSet
- for (m : methods) {
- val createdField = m.propertyFromGetter
- if (createdField !== null) {
- result.add(createdField)
- }
- }
- return result
- }
-
- /**
- * Creates generated property instance from the getter <code>method</code> name and return type.
- *
- * @param method method signature from which is the method name and return type obtained
- * @return generated property instance for the getter <code>method</code>
- * @throws IllegalArgumentException<ul>
- * <li>if the <code>method</code> equals <code>null</code></li>
- * <li>if the name of the <code>method</code> equals <code>null</code></li>
- * <li>if the name of the <code>method</code> is empty</li>
- * <li>if the return type of the <code>method</code> equals <code>null</code></li>
- * </ul>
- */
- def private GeneratedProperty propertyFromGetter(MethodSignature method) {
- if (method === null || method.name === null || method.name.empty || method.returnType === null) {
- throw new IllegalArgumentException("Method, method name, method return type reference cannot be NULL or empty!")
- }
- var prefix = "get";
- if (Types.BOOLEAN.equals(method.returnType)) {
- prefix = "is";
- }
- if (method.name.startsWith(prefix)) {
- val fieldName = method.getName().substring(prefix.length()).toFirstLower
- val tmpGenTO = new CodegenGeneratedTOBuilder(JavaTypeName.create("foo", "foo"))
- tmpGenTO.addProperty(fieldName).setReturnType(method.returnType)
- return tmpGenTO.build.properties.first
- }