1 /*
2 * ModeShape (http://www.modeshape.org)
3 * See the COPYRIGHT.txt file distributed with this work for information
4 * regarding copyright ownership. Some portions may be licensed
5 * to Red Hat, Inc. under one or more contributor license agreements.
6 * See the AUTHORS.txt file in the distribution for a full listing of
7 * individual contributors.
8 *
9 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10 * is licensed to you under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
13 *
14 * ModeShape is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this software; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23 */
24 package org.modeshape.sequencer.java.metadata;
25
26 import java.util.ArrayList;
27 import java.util.LinkedList;
28 import java.util.List;
29
30 /**
31 * Represent the {@link MethodMetadata}
32 */
33 public abstract class MethodMetadata {
34
35 private String name;
36
37 private FieldMetadata returnType;
38
39 public abstract boolean isContructor();
40
41 private List<ModifierMetadata> modifiers = new ArrayList<ModifierMetadata>();
42
43 private List<FieldMetadata> parameters = new ArrayList<FieldMetadata>();
44
45 private List<AnnotationMetadata> annotations = new LinkedList<AnnotationMetadata>();
46
47 public List<AnnotationMetadata> getAnnotations() {
48 return annotations;
49 }
50
51 /**
52 * @return name
53 */
54 public String getName() {
55 return name;
56 }
57
58 /**
59 * @param name Sets name to the specified value.
60 */
61 public void setName( String name ) {
62 this.name = name;
63 }
64
65 /**
66 * @return modifiers
67 */
68 public List<ModifierMetadata> getModifiers() {
69 return modifiers;
70 }
71
72 /**
73 * @param modifiers Sets modifiers to the specified value.
74 */
75 public void setModifiers( List<ModifierMetadata> modifiers ) {
76 this.modifiers = modifiers;
77 }
78
79 /**
80 * @param modifierName the name of the modifier to check for
81 * @return true if the type has a modifier of that name, otherwise false
82 */
83 public boolean hasModifierNamed( String modifierName ) {
84 for (ModifierMetadata modifier : modifiers) {
85 if (modifierName.equals(modifier.getName())) {
86 return true;
87 }
88 }
89
90 return false;
91
92 }
93
94 /**
95 * @return parameters
96 */
97 public List<FieldMetadata> getParameters() {
98 return parameters;
99 }
100
101 /**
102 * @param parameters Sets parameters to the specified value.
103 */
104 public void setParameters( List<FieldMetadata> parameters ) {
105 this.parameters = parameters;
106 }
107
108 /**
109 * @return returnType
110 */
111 public FieldMetadata getReturnType() {
112 return returnType;
113 }
114
115 /**
116 * @param returnType Sets returnType to the specified value.
117 */
118 public void setReturnType( FieldMetadata returnType ) {
119 this.returnType = returnType;
120 }
121
122 public String getId() {
123 StringBuilder buff = new StringBuilder();
124 buff.append(getName()).append('(');
125
126 boolean first = true;
127 for (FieldMetadata parameter : parameters) {
128 if (first) {
129 first = false;
130 } else {
131 buff.append(", ");
132 }
133
134 buff.append(shortNameFor(parameter.getName()).replace("[]", " array"));
135 }
136
137 buff.append(')');
138
139 return buff.toString();
140 }
141
142 private String shortNameFor( String type ) {
143 assert type != null;
144
145 int lastDotPos = type.lastIndexOf('.');
146 if (lastDotPos < 0) return type;
147 return type.substring(lastDotPos + 1);
148 }
149
150 }