1 package net.sourceforge.pmd.lang.vm.ast;
2
3 import org.apache.commons.lang3.ArrayUtils;
4 import org.apache.commons.lang3.StringUtils;
5
6 /*
7 * Licensed to the Apache Software Foundation (ASF) under one
8 * or more contributor license agreements. See the NOTICE file
9 * distributed with this work for additional information
10 * regarding copyright ownership. The ASF licenses this file
11 * to you under the Apache License, Version 2.0 (the
12 * "License"); you may not use this file except in compliance
13 * with the License. You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing,
18 * software distributed under the License is distributed on an
19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20 * KIND, either express or implied. See the License for the
21 * specific language governing permissions and limitations
22 * under the License.
23 */
24
25
26 /**
27 * ASTMethod.java
28 *
29 * Method support for references : $foo.method()
30 *
31 * NOTE :
32 *
33 * introspection is now done at render time.
34 *
35 * Please look at the Parser.jjt file which is what controls the generation of this class.
36 *
37 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
38 * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
39 * @version $Id: ASTMethod.java 720228 2008-11-24 16:58:33Z nbubna $
40 */
41 public class ASTMethod extends AbstractVmNode {
42 private final String methodName = "";
43
44 /**
45 * @param id
46 */
47 public ASTMethod(final int id) {
48 super(id);
49 }
50
51 /**
52 * @param p
53 * @param id
54 */
55 public ASTMethod(final VmParser p, final int id) {
56 super(p, id);
57 }
58
59 /**
60 * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.node.VmParserVisitor,
61 * java.lang.Object)
62 */
63 @Override
64 public Object jjtAccept(final VmParserVisitor visitor, final Object data) {
65 return visitor.visit(this, data);
66 }
67
68 /**
69 * Internal class used as key for method cache. Combines ASTMethod fields with array of parameter classes. Has
70 * public access (and complete constructor) for unit test purposes.
71 *
72 * @since 1.5
73 */
74 public static class MethodCacheKey {
75 private final String methodName;
76
77 private final Class[] params;
78
79 public MethodCacheKey(final String methodName, final Class[] params) {
80 /**
81 * Should never be initialized with nulls, but to be safe we refuse to accept them.
82 */
83 this.methodName = (methodName != null) ? methodName : StringUtils.EMPTY;
84 this.params = (params != null) ? params : ArrayUtils.EMPTY_CLASS_ARRAY;
85 }
86
87 /**
88 * @see java.lang.Object#equals(java.lang.Object)
89 */
90 @Override
91 public boolean equals(final Object o) {
92 /**
93 * note we skip the null test for methodName and params due to the earlier test in the constructor
94 */
95 if (o instanceof MethodCacheKey) {
96 final MethodCacheKey other = (MethodCacheKey) o;
97 if (params.length == other.params.length && methodName.equals(other.methodName)) {
98 for (int i = 0; i < params.length; ++i) {
99 if (params[i] == null) {
100 if (params[i] != other.params[i]) {
101 return false;
102 }
103 }
104 else if (!params[i].equals(other.params[i])) {
105 return false;
106 }
107 }
108 return true;
109 }
110 }
111 return false;
112 }
113
114 /**
115 * @see java.lang.Object#hashCode()
116 */
117 @Override
118 public int hashCode() {
119 int result = 17;
120
121 /**
122 * note we skip the null test for methodName and params due to the earlier test in the constructor
123 */
124 for (int i = 0; i < params.length; ++i) {
125 final Class param = params[i];
126 if (param != null) {
127 result = result * 37 + param.hashCode();
128 }
129 }
130
131 result = result * 37 + methodName.hashCode();
132
133 return result;
134 }
135 }
136
137 /**
138 * @return Returns the methodName.
139 * @since 1.5
140 */
141 public String getMethodName() {
142 return methodName;
143 }
144
145 }