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 /**
43 * @param id
44 */
45 public ASTMethod(final int id) {
46 super(id);
47 }
48
49 /**
50 * @param p
51 * @param id
52 */
53 public ASTMethod(final VmParser p, final int id) {
54 super(p, id);
55 }
56
57 /**
58 * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.node.VmParserVisitor,
59 * java.lang.Object)
60 */
61 @Override
62 public Object jjtAccept(final VmParserVisitor visitor, final Object data) {
63 return visitor.visit(this, data);
64 }
65
66 /**
67 * Internal class used as key for method cache. Combines ASTMethod fields with array of parameter classes. Has
68 * public access (and complete constructor) for unit test purposes.
69 *
70 * @since 1.5
71 */
72 public static class MethodCacheKey {
73 private final String methodName;
74
75 private final Class[] params;
76
77 public MethodCacheKey(final String methodName, final Class[] params) {
78 /**
79 * Should never be initialized with nulls, but to be safe we refuse to accept them.
80 */
81 this.methodName = (methodName != null) ? methodName : StringUtils.EMPTY;
82 this.params = (params != null) ? params : ArrayUtils.EMPTY_CLASS_ARRAY;
83 }
84
85 /**
86 * @see java.lang.Object#equals(java.lang.Object)
87 */
88 @Override
89 public boolean equals(final Object o) {
90 /**
91 * note we skip the null test for methodName and params due to the earlier test in the constructor
92 */
93 if (o instanceof MethodCacheKey) {
94 final MethodCacheKey other = (MethodCacheKey) o;
95 if (params.length == other.params.length && methodName.equals(other.methodName)) {
96 for (int i = 0; i < params.length; ++i) {
97 if (params[i] == null) {
98 if (params[i] != other.params[i]) {
99 return false;
100 }
101 }
102 else if (!params[i].equals(other.params[i])) {
103 return false;
104 }
105 }
106 return true;
107 }
108 }
109 return false;
110 }
111
112 /**
113 * @see java.lang.Object#hashCode()
114 */
115 @Override
116 public int hashCode() {
117 int result = 17;
118
119 /**
120 * note we skip the null test for methodName and params due to the earlier test in the constructor
121 */
122 for (int i = 0; i < params.length; ++i) {
123 final Class param = params[i];
124 if (param != null) {
125 result = result * 37 + param.hashCode();
126 }
127 }
128
129 result = result * 37 + methodName.hashCode();
130
131 return result;
132 }
133 }
134
135 /**
136 * @return Returns the methodName.
137 * @since 1.5
138 */
139 public String getMethodName() {
140 return "";
141 }
142
143 }