| 1 |
/* |
| 2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 |
* |
| 4 |
* Copyright (c) 2010-2011 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
* The contents of this file are subject to the terms of either the GNU |
| 7 |
* General Public License Version 2 only ("GPL") or the Common Development |
| 8 |
* and Distribution License("CDDL") (collectively, the "License"). You |
| 9 |
* may not use this file except in compliance with the License. You can |
| 10 |
* obtain a copy of the License at |
| 11 |
* http://glassfish.java.net/public/CDDL+GPL_1_1.html |
| 12 |
* or packager/legal/LICENSE.txt. See the License for the specific |
| 13 |
* language governing permissions and limitations under the License. |
| 14 |
* |
| 15 |
* When distributing the software, include this License Header Notice in each |
| 16 |
* file and include the License file at packager/legal/LICENSE.txt. |
| 17 |
* |
| 18 |
* GPL Classpath Exception: |
| 19 |
* Oracle designates this particular file as subject to the "Classpath" |
| 20 |
* exception as provided by Oracle in the GPL Version 2 section of the License |
| 21 |
* file that accompanied this code. |
| 22 |
* |
| 23 |
* Modifications: |
| 24 |
* If applicable, add the following below the License Header, with the fields |
| 25 |
* enclosed by brackets [] replaced by your own identifying information: |
| 26 |
* "Portions Copyright [year] [name of copyright owner]" |
| 27 |
* |
| 28 |
* Contributor(s): |
| 29 |
* If you wish your version of this file to be governed by only the CDDL or |
| 30 |
* only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 31 |
* elects to include this software in this distribution under the [CDDL or GPL |
| 32 |
* Version 2] license." If you don't indicate a single choice of license, a |
| 33 |
* recipient has the option to distribute your version of this file under |
| 34 |
* either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 35 |
* its licensees as provided above. However, if you add GPL Version 2 code |
| 36 |
* and therefore, elected the GPL Version 2 license, then the option applies |
| 37 |
* only if the new code is made subject to such option by the copyright |
| 38 |
* holder. |
| 39 |
*/ |
| 40 |
|
| 41 |
package com.sun.jersey.server.impl.cdi; |
| 42 |
|
| 43 |
import java.lang.annotation.Annotation; |
| 44 |
import java.lang.reflect.InvocationTargetException; |
| 45 |
import java.lang.reflect.Method; |
| 46 |
import java.lang.reflect.Type; |
| 47 |
import javax.ws.rs.DefaultValue; |
| 48 |
|
| 49 |
/** |
| 50 |
* Records all information about a JAX-RS-related parameter. |
| 51 |
* |
| 52 |
* @author robc |
| 53 |
*/ |
| 54 |
public class DiscoveredParameter { |
| 55 |
|
| 56 |
private Annotation annotation; |
| 57 |
private Type type; |
| 58 |
private DefaultValue defaultValue; |
| 59 |
private boolean encoded; |
| 60 |
|
| 61 |
public DiscoveredParameter(Annotation annotation, Type type, DefaultValue defaultValue, boolean encoded) { |
| 62 |
this.annotation = annotation; |
| 63 |
this.type = type; |
| 64 |
this.defaultValue = defaultValue; |
| 65 |
this.encoded = encoded; |
| 66 |
} |
| 67 |
|
| 68 |
public Annotation getAnnotation() { |
| 69 |
return annotation; |
| 70 |
} |
| 71 |
|
| 72 |
public Type getType() { |
| 73 |
return type; |
| 74 |
} |
| 75 |
|
| 76 |
public DefaultValue getDefaultValue() { |
| 77 |
return defaultValue; |
| 78 |
} |
| 79 |
|
| 80 |
public boolean isEncoded() { |
| 81 |
return encoded; |
| 82 |
} |
| 83 |
|
| 84 |
public String getValue() { |
| 85 |
try { |
| 86 |
// Annotation[] as, Annotation a, Source source, String sourceName, Type type, Class<?> clazz) |
| 87 |
Method valueMethod = annotation.annotationType().getDeclaredMethod("value"); |
| 88 |
String name = (String)valueMethod.invoke(annotation); |
| 89 |
return name; |
| 90 |
} |
| 91 |
catch (NoSuchMethodException e) { |
| 92 |
return null; |
| 93 |
} |
| 94 |
catch (IllegalAccessException e) { |
| 95 |
// wrap and rethrow for now |
| 96 |
throw new RuntimeException(e); |
| 97 |
} |
| 98 |
catch (InvocationTargetException e) { |
| 99 |
// wrap and rethrow for now |
| 100 |
throw new RuntimeException(e); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public int hashCode() { |
| 106 |
int result = 17; |
| 107 |
result = 31 * result + (annotation == null ? 0 : annotation.hashCode()); |
| 108 |
result = 31 * result + (type == null ? 0 : type.hashCode()); |
| 109 |
result = 31 * result + (defaultValue == null ? 0 : defaultValue.hashCode()); |
| 110 |
result = 31 * result + (encoded ? 7 : 11); |
| 111 |
return result; |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public boolean equals(Object obj) { |
| 116 |
if (this == obj) { |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
if (obj == null) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
if (obj.getClass() != this.getClass()) { |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
DiscoveredParameter that = (DiscoveredParameter) obj; |
| 129 |
|
| 130 |
return (annotation == null ? that.annotation == null : annotation.equals(that.annotation)) && |
| 131 |
(type == null ? that.type == null : type.equals(that.type)) && |
| 132 |
(defaultValue == null ? that.defaultValue == null : defaultValue.equals(that.defaultValue)) && |
| 133 |
(this.encoded == that.encoded); |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public String toString() { |
| 138 |
StringBuilder sb = new StringBuilder(); |
| 139 |
sb.append("DiscoveredParameter("); |
| 140 |
sb.append(annotation); |
| 141 |
sb.append(','); |
| 142 |
sb.append(type); |
| 143 |
sb.append(','); |
| 144 |
sb.append(defaultValue); |
| 145 |
sb.append(','); |
| 146 |
sb.append(encoded); |
| 147 |
sb.append(')'); |
| 148 |
return sb.toString(); |
| 149 |
} |
| 150 |
} |