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ö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 memberName The name of a (method or) field. Cannot be null or empty.
57 * @param classXmlName The name given as the {@link XmlType#name()} value of an annotation placed on the Class,
58 * or {@code null} if none is provided.
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 */
62 public MethodLocation(final String packageName,
63 final String className,
64 final String classXmlName,
65 final String memberName,
66 final String memberXmlName,
67 final List<JavaParameter> parameters) {
68
69 super(packageName, className, classXmlName, memberName, memberXmlName);
70
71 // Check sanity
72 Validate.notNull(parameters, "parameters");
73
74 // Stringify the parameter types
75 if (parameters.size() > 0) {
76 final StringBuilder builder = new StringBuilder();
77
78 for (JavaParameter current : parameters) {
79 builder.append(current.getType().getFullyQualifiedName()).append(PARAMETER_SEPARATOR);
80 }
81 this.parameters = "(" + builder.substring(0, builder.lastIndexOf(PARAMETER_SEPARATOR)) + ")";
82 }
83 }
84
85 /**
86 * {@inheritDoc}
87 */
88 @Override
89 public String getPath() {
90 return super.getPath() + parameters;
91 }
92
93 /**
94 * {@inheritDoc}
95 */
96 @Override
97 public String toString() {
98 return super.toString() + parameters;
99 }
100
101 /**
102 * {@inheritDoc}
103 */
104 @Override
105 public int hashCode() {
106 return this.toString().hashCode();
107 }
108
109 /**
110 * @return The parameters, concatenated into a String.
111 */
112 public String getParametersAsString() {
113 return parameters;
114 }
115
116 /**
117 * @return True if this MethodLocation has no parameters.
118 */
119 public boolean hasNoParameters() {
120 return NO_PARAMETERS.equals(parameters);
121 }
122 }