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