View Javadoc
1   package org.codehaus.mojo.jaxb2.schemageneration.postprocessing.javadoc.location;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.codehaus.mojo.jaxb2.shared.Validate;
23  
24  import javax.xml.bind.annotation.XmlAttribute;
25  import javax.xml.bind.annotation.XmlElement;
26  import javax.xml.bind.annotation.XmlType;
27  
28  /**
29   * Comparable path structure to locate a particular field within compilation unit.
30   *
31   * @author <a href="mailto:lj@jguru.se">Lennart J&ouml;relid</a>, jGuru Europe AB
32   * @since 2.0
33   */
34  public class FieldLocation extends ClassLocation {
35  
36      // Internal state
37      private String memberName;
38      private String memberXmlName;
39  
40      /**
41       * Creates a new FieldLocation with the supplied package, class and member names.
42       *
43       * @param packageName   The name of the package for a class potentially holding JavaDoc. Cannot be {@code null}.
44       * @param className     The (simple) name of a class. Cannot be null or empty.
45       * @param memberName    The name of a (method or) field. Cannot be null or empty.
46       * @param classXmlName  The name given as the {@link XmlType#name()} value of an annotation placed on the Class,
47       *                      or {@code  null} if none is provided.
48       * @param memberXmlName The name given as the {@link XmlElement#name()} or {@link XmlAttribute#name()} value of
49       *                      an annotation placed on this Field, or {@code null} if none is provided.
50       */
51      public FieldLocation(final String packageName,
52              final String className,
53              final String classXmlName,
54              final String memberName,
55              final String memberXmlName) {
56  
57          // Delegate
58          super(packageName, className, classXmlName);
59  
60          // Check sanity
61          Validate.notEmpty(memberName, "memberName");
62  
63          // Assign internal state
64          this.memberName = memberName;
65          this.memberXmlName = memberXmlName;
66      }
67  
68      /**
69       * Retrieves the name of the member (i.e. method or field), potentially holding JavaDoc.
70       *
71       * @return The name of the member (i.e. method or field), potentially holding JavaDoc.
72       * Never null or empty.
73       */
74      public String getMemberName() {
75          return memberXmlName == null ? memberName : memberXmlName;
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public String getAnnotationRenamedTo() {
83          return memberXmlName;
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public String getPath() {
91          return super.getPath() + "#" + getMemberName();
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public String toString() {
99  
100         final String xmlOverriddenFrom = memberXmlName != null && !memberName.equals(memberXmlName)
101                 ? " (from: " + memberName + ")"
102                 : "";
103 
104         return super.toString() + "#" + getMemberName() + xmlOverriddenFrom;
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public int hashCode() {
112         return this.toString().hashCode();
113     }
114 }