Integrate Action with BindingContract
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / JavaFileTemplate.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.binding.java.api.generator;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verify;
12 import static java.util.Objects.requireNonNull;
13 import static org.opendaylight.mdsal.binding.generator.BindingGeneratorUtil.encodeAngleBrackets;
14 import static org.opendaylight.mdsal.binding.generator.BindingGeneratorUtil.replaceAllIllegalChars;
15
16 import com.google.common.base.CharMatcher;
17 import com.google.common.base.MoreObjects;
18 import com.google.common.collect.ImmutableSortedSet;
19 import java.lang.reflect.Method;
20 import java.util.AbstractMap;
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.HashMap;
26 import java.util.LinkedHashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.NoSuchElementException;
30 import java.util.Objects;
31 import java.util.Optional;
32 import java.util.Set;
33 import java.util.regex.Pattern;
34 import java.util.stream.Collectors;
35 import javax.annotation.processing.Generated;
36 import org.eclipse.jdt.annotation.NonNull;
37 import org.eclipse.jdt.annotation.Nullable;
38 import org.eclipse.xtext.xbase.lib.StringExtensions;
39 import org.opendaylight.mdsal.binding.model.api.AnnotationType;
40 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
41 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
42 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
43 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
44 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
45 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
46 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
47 import org.opendaylight.mdsal.binding.model.api.Restrictions;
48 import org.opendaylight.mdsal.binding.model.api.Type;
49 import org.opendaylight.mdsal.binding.model.api.YangSourceDefinition.Multiple;
50 import org.opendaylight.mdsal.binding.model.api.YangSourceDefinition.Single;
51 import org.opendaylight.mdsal.binding.model.ri.BindingTypes;
52 import org.opendaylight.mdsal.binding.model.ri.Types;
53 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
54 import org.opendaylight.yangtools.yang.binding.Augmentable;
55 import org.opendaylight.yangtools.yang.binding.CodeHelpers;
56 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
57 import org.opendaylight.yangtools.yang.model.api.DocumentedNode;
58 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
59 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
60 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
61 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
62 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
63 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
64 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
65 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
66 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
67 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
68 import org.opendaylight.yangtools.yang.model.export.DeclaredStatementFormatter;
69
70 /**
71  * Base Java file template. Contains a non-null type and imports which the generated code refers to.
72  */
73 class JavaFileTemplate {
74     /**
75      * {@code java.lang.Class} as a JavaTypeName.
76      */
77     static final @NonNull JavaTypeName CLASS = JavaTypeName.create(Class.class);
78     /**
79      * {@code java.lang.Deprecated} as a JavaTypeName.
80      */
81     static final @NonNull JavaTypeName DEPRECATED = JavaTypeName.create(Deprecated.class);
82     /**
83      * {@code java.lang.IllegalArgumentException} as a JavaTypeName.
84      */
85     static final @NonNull JavaTypeName IAE = JavaTypeName.create(IllegalArgumentException.class);
86     /**
87      * {@code java.lang.NullPointerException} as a JavaTypeName.
88      */
89     static final @NonNull JavaTypeName NPE = JavaTypeName.create(NullPointerException.class);
90     /**
91      * {@code java.lang.NoSuchElementException} as a JavaTypeName.
92      */
93     static final @NonNull JavaTypeName NSEE = JavaTypeName.create(NoSuchElementException.class);
94     /**
95      * {@code java.lang.Override} as a JavaTypeName.
96      */
97     static final @NonNull JavaTypeName OVERRIDE = JavaTypeName.create(Override.class);
98     /**
99      * {@code java.lang.void} as a JavaTypeName.
100      */
101     static final @NonNull JavaTypeName SUPPRESS_WARNINGS = JavaTypeName.create(SuppressWarnings.class);
102     /**
103      * {@code java.lang.SuppressWarnings} as a JavaTypeName.
104      */
105     static final @NonNull JavaTypeName VOID = JavaTypeName.create(void.class);
106
107     /**
108      * {@code java.util.Arrays} as a JavaTypeName.
109      */
110     static final @NonNull JavaTypeName JU_ARRAYS = JavaTypeName.create(Arrays.class);
111     /**
112      * {@code java.util.HashMap} as a JavaTypeName.
113      */
114     static final @NonNull JavaTypeName JU_HASHMAP = JavaTypeName.create(HashMap.class);
115     /**
116      * {@code java.util.List} as a JavaTypeName.
117      */
118     static final @NonNull JavaTypeName JU_LIST = JavaTypeName.create(List.class);
119     /**
120      * {@code java.util.Map} as a JavaTypeName.
121      */
122     static final @NonNull JavaTypeName JU_MAP = JavaTypeName.create(Map.class);
123     /**
124      * {@code java.util.Objects} as a JavaTypeName.
125      */
126     static final @NonNull JavaTypeName JU_OBJECTS = JavaTypeName.create(Objects.class);
127     /**
128      * {@code java.util.regex.Pattern} as a JavaTypeName.
129      */
130     static final @NonNull JavaTypeName JUR_PATTERN = JavaTypeName.create(Pattern.class);
131
132     /**
133      * {@code javax.annotation.processing.Generated} as a JavaTypeName.
134      */
135     static final @NonNull JavaTypeName GENERATED = JavaTypeName.create(Generated.class);
136
137     /**
138      * {@code org.eclipse.jdt.annotation.NonNull} as a JavaTypeName.
139      */
140     static final @NonNull JavaTypeName NONNULL = JavaTypeName.create("org.eclipse.jdt.annotation", "NonNull");
141
142     /**
143      * {@code org.eclipse.jdt.annotation.NonNullByDefault} as a JavaTypeName.
144      */
145     static final @NonNull JavaTypeName NONNULL_BY_DEFAULT =
146         JavaTypeName.create("org.eclipse.jdt.annotation", "NonNullByDefault");
147
148     /**
149      * {@code org.eclipse.jdt.annotation.Nullable} as a JavaTypeName.
150      */
151     static final @NonNull JavaTypeName NULLABLE = JavaTypeName.create("org.eclipse.jdt.annotation", "Nullable");
152
153     /**
154      * {@code org.opendaylight.yangtools.yang.binding.CodeHelpers} as a JavaTypeName.
155      */
156     static final @NonNull JavaTypeName CODEHELPERS = JavaTypeName.create(CodeHelpers.class);
157
158     /**
159      * {@code com.google.common.base.MoreObjects} as a JavaTypeName.
160      */
161     static final @NonNull JavaTypeName MOREOBJECTS = JavaTypeName.create(MoreObjects.class);
162
163     private static final Comparator<MethodSignature> METHOD_COMPARATOR = new AlphabeticallyTypeMemberComparator<>();
164     private static final CharMatcher AMP_MATCHER = CharMatcher.is('&');
165     private static final Pattern TAIL_COMMENT_PATTERN = Pattern.compile("*/", Pattern.LITERAL);
166     private static final DeclaredStatementFormatter YANG_FORMATTER = DeclaredStatementFormatter.builder()
167         .addIgnoredStatement(YangStmtMapping.CONTACT)
168         .addIgnoredStatement(YangStmtMapping.DESCRIPTION)
169         .addIgnoredStatement(YangStmtMapping.REFERENCE)
170         .addIgnoredStatement(YangStmtMapping.ORGANIZATION)
171         .build();
172     private static final int GETTER_PREFIX_LENGTH = BindingMapping.GETTER_PREFIX.length();
173     private static final Type AUGMENTATION_RET_TYPE;
174
175     static {
176         final Method m;
177         try {
178             m = Augmentable.class.getDeclaredMethod(BindingMapping.AUGMENTABLE_AUGMENTATION_NAME, Class.class);
179         } catch (NoSuchMethodException e) {
180             throw new ExceptionInInitializerError(e);
181         }
182
183         AUGMENTATION_RET_TYPE = Type.of(JavaTypeName.create(m.getReturnType()));
184     }
185
186     private final AbstractJavaGeneratedType javaType;
187     private final GeneratedType type;
188
189     JavaFileTemplate(final @NonNull GeneratedType type) {
190         this(new TopLevelJavaGeneratedType(type), type);
191     }
192
193     JavaFileTemplate(final AbstractJavaGeneratedType javaType, final GeneratedType type) {
194         this.javaType = requireNonNull(javaType);
195         this.type = requireNonNull(type);
196     }
197
198     final AbstractJavaGeneratedType javaType() {
199         return javaType;
200     }
201
202     final GeneratedType type() {
203         return type;
204     }
205
206     final String generateImportBlock() {
207         verify(javaType instanceof TopLevelJavaGeneratedType);
208         return ((TopLevelJavaGeneratedType) javaType).imports().map(name -> "import " + name + ";\n")
209                 .collect(Collectors.joining());
210     }
211
212     final @NonNull String importedJavadocName(final @NonNull Type intype) {
213         return importedName(intype instanceof ParameterizedType parameterized ? parameterized.getRawType() : intype);
214     }
215
216     final @NonNull String importedName(final @NonNull Type intype) {
217         return javaType.getReferenceString(intype);
218     }
219
220     final @NonNull String importedName(final @NonNull Type intype, final @NonNull String annotation) {
221         return javaType.getReferenceString(intype, annotation);
222     }
223
224     final @NonNull String importedName(final Class<?> cls) {
225         return importedName(Types.typeForClass(cls));
226     }
227
228     final @NonNull String importedName(final @NonNull JavaTypeName intype) {
229         return javaType.getReferenceString(intype);
230     }
231
232     final @NonNull String importedNonNull(final @NonNull Type intype) {
233         return importedName(intype, importedName(NONNULL));
234     }
235
236     final @NonNull String importedNullable(final @NonNull Type intype) {
237         return importedName(intype, importedName(NULLABLE));
238     }
239
240     final @NonNull String fullyQualifiedNonNull(final @NonNull Type intype) {
241         return fullyQualifiedName(intype, importedName(NONNULL));
242     }
243
244     final @NonNull String fullyQualifiedName(final @NonNull Type intype, final @NonNull String annotation) {
245         return javaType.getFullyQualifiedReference(intype, annotation);
246     }
247
248     // Exposed for BuilderTemplate
249     boolean isLocalInnerClass(final JavaTypeName name) {
250         final Optional<JavaTypeName> optEnc = name.immediatelyEnclosingClass();
251         return optEnc.isPresent() && type.getIdentifier().equals(optEnc.get());
252     }
253
254     final CharSequence generateInnerClass(final GeneratedType innerClass) {
255         if (!(innerClass instanceof GeneratedTransferObject gto)) {
256             return "";
257         }
258
259         final NestedJavaGeneratedType innerJavaType = javaType.getEnclosedType(innerClass.getIdentifier());
260         return gto.isUnionType() ? new UnionTemplate(innerJavaType, gto).generateAsInnerClass()
261                 : new ClassTemplate(innerJavaType, gto).generateAsInnerClass();
262     }
263
264     /**
265      * Return imported name of java.util class, whose hashCode/equals methods we want to invoke on the property. Returns
266      * {@link Arrays} if the property is an array, {@link Objects} otherwise.
267      *
268      * @param property Generated property
269      * @return Imported class name
270      */
271     final String importedUtilClass(final GeneratedProperty property) {
272         return importedName(property.getReturnType().getName().indexOf('[') != -1 ? JU_ARRAYS : JU_OBJECTS);
273     }
274
275     final String generatedAnnotation() {
276         return "@" + importedName(GENERATED) + "(\"mdsal-binding-generator\")";
277     }
278
279     /**
280      * Run type analysis, which results in identification of the augmentable type, as well as all methods available
281      * to the type, expressed as properties.
282      */
283     static Map.Entry<Type, Set<BuilderGeneratedProperty>> analyzeTypeHierarchy(final GeneratedType type) {
284         final Set<MethodSignature> methods = new LinkedHashSet<>();
285         final Type augmentType = createMethods(type, methods);
286         final Set<MethodSignature> sortedMethods = ImmutableSortedSet.orderedBy(METHOD_COMPARATOR).addAll(methods)
287                 .build();
288
289         return new AbstractMap.SimpleImmutableEntry<>(augmentType, propertiesFromMethods(sortedMethods));
290     }
291
292     static final Restrictions restrictionsForSetter(final Type actualType) {
293         return actualType instanceof GeneratedType ? null : getRestrictions(actualType);
294     }
295
296     static final Restrictions getRestrictions(final Type type) {
297         if (type instanceof ConcreteType) {
298             return ((ConcreteType) type).getRestrictions();
299         }
300         if (type instanceof GeneratedTransferObject) {
301             return ((GeneratedTransferObject) type).getRestrictions();
302         }
303         return null;
304     }
305
306     /**
307      * Generate a call to {@link Object#clone()} if target field represents an array. Returns an empty string otherwise.
308      *
309      * @param property Generated property
310      * @return The string used to clone the property, or an empty string
311      */
312     static final String cloneCall(final GeneratedProperty property) {
313         return property.getReturnType().getName().endsWith("[]") ? ".clone()" : "";
314     }
315
316     /**
317      * Returns set of method signature instances which contains all the methods of the <code>genType</code>
318      * and all the methods of the implemented interfaces.
319      *
320      * @returns set of method signature instances
321      */
322     private static ParameterizedType createMethods(final GeneratedType type, final Set<MethodSignature> methods) {
323         methods.addAll(type.getMethodDefinitions());
324         return collectImplementedMethods(type, methods, type.getImplements());
325     }
326
327     /**
328      * Adds to the <code>methods</code> set all the methods of the <code>implementedIfcs</code>
329      * and recursively their implemented interfaces.
330      *
331      * @param methods set of method signatures
332      * @param implementedIfcs list of implemented interfaces
333      */
334     private static ParameterizedType collectImplementedMethods(final GeneratedType type,
335             final Set<MethodSignature> methods, final List<Type> implementedIfcs) {
336         if (implementedIfcs == null || implementedIfcs.isEmpty()) {
337             return null;
338         }
339
340         ParameterizedType augmentType = null;
341         for (Type implementedIfc : implementedIfcs) {
342             if (implementedIfc instanceof GeneratedType ifc && !(implementedIfc instanceof GeneratedTransferObject)) {
343                 addImplMethods(methods, ifc);
344
345                 final ParameterizedType t = collectImplementedMethods(type, methods, ifc.getImplements());
346                 if (t != null && augmentType == null) {
347                     augmentType = t;
348                 }
349             } else if (Augmentable.class.getName().equals(implementedIfc.getFullyQualifiedName())) {
350                 augmentType = Types.parameterizedTypeFor(AUGMENTATION_RET_TYPE, Type.of(type.getIdentifier()));
351             }
352         }
353
354         return augmentType;
355     }
356
357     private static void addImplMethods(final Set<MethodSignature> methods, final GeneratedType implType) {
358         for (final MethodSignature implMethod : implType.getMethodDefinitions()) {
359             if (hasOverrideAnnotation(implMethod)) {
360                 methods.add(implMethod);
361             } else {
362                 final String implMethodName = implMethod.getName();
363                 if (BindingMapping.isGetterMethodName(implMethodName)
364                         && getterByName(methods, implMethodName).isEmpty()) {
365
366                     methods.add(implMethod);
367                 }
368             }
369         }
370     }
371
372     protected static Optional<MethodSignature> getterByName(final Iterable<MethodSignature> methods,
373             final String implMethodName) {
374         for (MethodSignature method : methods) {
375             final String methodName = method.getName();
376             if (BindingMapping.isGetterMethodName(methodName) && isSameProperty(method.getName(), implMethodName)) {
377                 return Optional.of(method);
378             }
379         }
380         return Optional.empty();
381     }
382
383     protected static String propertyNameFromGetter(final MethodSignature getter) {
384         return propertyNameFromGetter(getter.getName());
385     }
386
387     protected static String propertyNameFromGetter(final String getterName) {
388         final String prefix;
389         if (BindingMapping.isGetterMethodName(getterName)) {
390             prefix = BindingMapping.GETTER_PREFIX;
391         } else if (BindingMapping.isNonnullMethodName(getterName)) {
392             prefix = BindingMapping.NONNULL_PREFIX;
393         } else if (BindingMapping.isRequireMethodName(getterName)) {
394             prefix = BindingMapping.REQUIRE_PREFIX;
395         } else {
396             throw new IllegalArgumentException(getterName + " is not a getter");
397         }
398         return StringExtensions.toFirstLower(getterName.substring(prefix.length()));
399     }
400
401     /**
402      * Check whether specified method has an attached annotation which corresponds to {@code @Override}.
403      *
404      * @param method Method to examine
405      * @return True if there is an override annotation
406      */
407     static boolean hasOverrideAnnotation(final MethodSignature method) {
408         for (final AnnotationType annotation : method.getAnnotations()) {
409             if (OVERRIDE.equals(annotation.getIdentifier())) {
410                 return true;
411             }
412         }
413         return false;
414     }
415
416     final void appendSnippet(final StringBuilder sb, final GeneratedType genType) {
417         genType.getYangSourceDefinition().ifPresent(def -> {
418             sb.append('\n');
419
420             if (def instanceof Single single) {
421                 final DocumentedNode node = single.getNode();
422
423                 sb.append("<p>\n")
424                     .append("This class represents the following YANG schema fragment defined in module <b>")
425                     .append(def.getModule().argument().getLocalName()).append("</b>\n")
426                     .append("<pre>\n");
427                 appendYangSnippet(sb, def.getModule(), ((EffectiveStatement<?, ?>) node).getDeclared());
428                 sb.append("</pre>");
429
430                 if (node instanceof SchemaNode schema) {
431 //                    sb.append("The schema path to identify an instance is\n");
432 //                    appendPath(sb.append("<i>"), def.getModule(), schema.getPath().getPathFromRoot());
433 //                    sb.append("</i>\n");
434
435                     if (hasBuilderClass(schema)) {
436                         final String builderName = genType.getName() + BindingMapping.BUILDER_SUFFIX;
437
438                         sb.append("\n<p>To create instances of this class use {@link ").append(builderName)
439                         .append("}.\n")
440                         .append("@see ").append(builderName).append('\n');
441                         if (node instanceof ListSchemaNode) {
442                             final var keyDef = ((ListSchemaNode) node).getKeyDefinition();
443                             if (!keyDef.isEmpty()) {
444                                 sb.append("@see ").append(genType.getName()).append(BindingMapping.KEY_SUFFIX);
445                             }
446                             sb.append('\n');
447                         }
448                     }
449                 } else if (node instanceof AugmentEffectiveStatement) {
450                     // Find target Augmentation<Foo> and reference Foo
451                     final var augType = findAugmentationArgument(genType);
452                     if (augType != null) {
453                         sb.append("\n\n")
454                         .append("@see ").append(importedName(augType));
455                     }
456                 }
457                 if (node instanceof TypedefEffectiveStatement && genType instanceof GeneratedTransferObject genTO) {
458                     final var augType = genTO.getSuperType();
459                     if (augType != null) {
460                         sb.append("\n\n")
461                         .append("@see ").append(augType.getName());
462                     }
463                 }
464             } else if (def instanceof Multiple multiple) {
465                 sb.append("<pre>\n");
466                 for (SchemaNode node : multiple.getNodes()) {
467                     appendYangSnippet(sb, def.getModule(), ((EffectiveStatement<?, ?>) node).getDeclared());
468                 }
469                 sb.append("</pre>\n");
470             }
471         });
472     }
473
474     private static @Nullable Type findAugmentationArgument(final GeneratedType genType) {
475         for (var implType : genType.getImplements()) {
476             if (implType instanceof ParameterizedType parameterized) {
477                 final var augmentType = BindingTypes.extractAugmentable(parameterized);
478                 if (augmentType != null) {
479                     return augmentType;
480                 }
481             }
482         }
483         return null;
484     }
485
486     static String encodeJavadocSymbols(final String description) {
487         // FIXME: Use String.isBlank()?
488         return description == null || description.isEmpty() ? description
489             : TAIL_COMMENT_PATTERN.matcher(AMP_MATCHER.replaceFrom(description, "&amp;")).replaceAll("&#42;&#47;");
490     }
491
492     private static void appendYangSnippet(final StringBuilder sb, final ModuleEffectiveStatement module,
493         final DeclaredStatement<?> stmt) {
494         for (String str : YANG_FORMATTER.toYangTextSnippet(module, stmt)) {
495             sb.append(replaceAllIllegalChars(encodeAngleBrackets(encodeJavadocSymbols(str))));
496         }
497     }
498
499 //    private static void appendPath(final StringBuilder sb, final ModuleEffectiveStatement module,
500 //            final List<QName> path) {
501 //        if (!path.isEmpty()) {
502 //            // FIXME: this is module name, while when we switch, we end up using QName.toString() -- which is weird
503 //            sb.append(module.argument().getLocalName());
504 //            XMLNamespace currentNamespace = path.get(0).getNamespace();
505 //
506 //            for (QName pathElement : path) {
507 //                final XMLNamespace elementNamespace = pathElement.getNamespace();
508 //                if (!elementNamespace.equals(currentNamespace)) {
509 //                    sb.append(pathElement);
510 //                    currentNamespace = elementNamespace;
511 //                } else {
512 //                    sb.append(pathElement.getLocalName());
513 //                }
514 //            }
515 //        }
516 //    }
517
518     private static boolean hasBuilderClass(final SchemaNode schemaNode) {
519         return schemaNode instanceof ContainerSchemaNode || schemaNode instanceof ListSchemaNode
520                 || schemaNode instanceof RpcDefinition || schemaNode instanceof NotificationDefinition;
521     }
522
523     private static boolean isSameProperty(final String getterName1, final String getterName2) {
524         return propertyNameFromGetter(getterName1).equals(propertyNameFromGetter(getterName2));
525     }
526
527     /**
528      * Creates set of generated property instances from getter <code>methods</code>.
529      *
530      * @param methods set of method signature instances which should be transformed to list of properties
531      * @return set of generated property instances which represents the getter <code>methods</code>
532      */
533     private static Set<BuilderGeneratedProperty> propertiesFromMethods(final Collection<MethodSignature> methods) {
534         if (methods == null || methods.isEmpty()) {
535             return Collections.emptySet();
536         }
537         final Set<BuilderGeneratedProperty> result = new LinkedHashSet<>();
538         for (MethodSignature m : methods) {
539             final BuilderGeneratedProperty createdField = propertyFromGetter(m);
540             if (createdField != null) {
541                 result.add(createdField);
542             }
543         }
544         return result;
545     }
546
547     /**
548      * Creates generated property instance from the getter <code>method</code> name and return type.
549      *
550      * @param method method signature from which is the method name and return type obtained
551      * @return generated property instance for the getter <code>method</code>
552      * @throws IllegalArgumentException <ul>
553      *                                    <li>if the {@code method} equals {@code null}</li>
554      *                                    <li>if the name of the {@code method} equals {@code null}</li>
555      *                                    <li>if the name of the {@code method} is empty</li>
556      *                                    <li>if the return type of the {@code method} equals {@code null}</li>
557      *                                  </ul>
558      */
559     private static BuilderGeneratedProperty propertyFromGetter(final MethodSignature method) {
560         checkArgument(method != null);
561         checkArgument(method.getReturnType() != null);
562         checkArgument(method.getName() != null);
563         checkArgument(!method.getName().isEmpty());
564         if (method.isDefault()) {
565             return null;
566         }
567         if (!BindingMapping.isGetterMethodName(method.getName())) {
568             return null;
569         }
570
571         final String fieldName = StringExtensions.toFirstLower(method.getName().substring(GETTER_PREFIX_LENGTH));
572         return new BuilderGeneratedProperty(fieldName, method);
573     }
574 }