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 com.thoughtworks.qdox.model.JavaParameter;
23  import org.codehaus.mojo.jaxb2.shared.Validate;
24  
25  import javax.xml.bind.annotation.XmlAttribute;
26  import javax.xml.bind.annotation.XmlElement;
27  import javax.xml.bind.annotation.XmlType;
28  import java.util.List;
29  
30  /**
31   * Comparable path structure to locate a particular method within compilation unit.
32   *
33   * @author <a href="mailto:lj@jguru.se">Lennart J&ouml;relid</a>, jGuru Europe AB
34   * @since 2.0
35   */
36  public class MethodLocation extends FieldLocation {
37  
38      /**
39       * Signature for a method without any parameters.
40       */
41      public static final String NO_PARAMETERS = "()";
42  
43      /**
44       * Separator for a method's parameters.
45       */
46      public static final String PARAMETER_SEPARATOR = ",";
47  
48      // Internal state
49      private String parameters = NO_PARAMETERS;
50  
51      /**
52       * Creates a new MethodLocation with the supplied package, class and member names.
53       *
54       * @param packageName The name of the package for a class potentially holding JavaDoc. Cannot be {@code null}.
55       * @param className   The (simple) name of a class. Cannot be null or empty.
56       * @param classXmlName  The name given as the {@link XmlType#name()} value of an annotation placed on the Class,
57       *                      or {@code  null} if none is provided.
58       * @param memberName  The name of a (method or) field. Cannot be null or empty.
59       * @param memberXmlName The name given as the {@link XmlElement#name()} or {@link XmlAttribute#name()} value of
60       *                      an annotation placed on this Field, or {@code null} if none is provided.
61       * @param parameters  The names of the types which are parameters to this method.
62       */
63      public MethodLocation(final String packageName,
64              final String className,
65              final String classXmlName,
66              final String memberName,
67              final String memberXmlName,
68              final List<JavaParameter> parameters) {
69  
70          super(packageName, className, classXmlName, memberName, memberXmlName);
71  
72          // Check sanity
73          Validate.notNull(parameters, "parameters");
74  
75          // Stringify the parameter types
76          if (parameters.size() > 0) {
77              final StringBuilder builder = new StringBuilder();
78  
79              for (JavaParameter current : parameters) {
80                  builder.append(current.getType().getFullyQualifiedName()).append(PARAMETER_SEPARATOR);
81              }
82              this.parameters = "(" + builder.substring(0, builder.lastIndexOf(PARAMETER_SEPARATOR)) + ")";
83          }
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public String getPath() {
91          return super.getPath() + parameters;
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public String toString() {
99          return super.toString() + parameters;
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public int hashCode() {
107         return this.toString().hashCode();
108     }
109 
110     /**
111      * @return The parameters, concatenated into a String.
112      */
113     public String getParametersAsString() {
114         return parameters;
115     }
116 
117     /**
118      * @return True if this MethodLocation has no parameters.
119      */
120     public boolean hasNoParameters() {
121         return NO_PARAMETERS.equals(parameters);
122     }
123 }