Wed, 20 Aug 2014 10:25:28 +0200
8044638: Tidy up Nashorn codebase for code standards
8055199: Tidy up Nashorn codebase for code standards (August 2014)
Reviewed-by: lagergren, sundar
attila@90 | 1 | /* |
attila@90 | 2 | * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
attila@90 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
attila@90 | 4 | * |
attila@90 | 5 | * This code is free software; you can redistribute it and/or modify it |
attila@90 | 6 | * under the terms of the GNU General Public License version 2 only, as |
attila@90 | 7 | * published by the Free Software Foundation. Oracle designates this |
attila@90 | 8 | * particular file as subject to the "Classpath" exception as provided |
attila@90 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
attila@90 | 10 | * |
attila@90 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
attila@90 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
attila@90 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
attila@90 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
attila@90 | 15 | * accompanied this code). |
attila@90 | 16 | * |
attila@90 | 17 | * You should have received a copy of the GNU General Public License version |
attila@90 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
attila@90 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
attila@90 | 20 | * |
attila@90 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
attila@90 | 22 | * or visit www.oracle.com if you need additional information or have any |
attila@90 | 23 | * questions. |
attila@90 | 24 | */ |
attila@90 | 25 | |
attila@90 | 26 | /* |
attila@90 | 27 | * This file is available under and governed by the GNU General Public |
attila@90 | 28 | * License version 2 only, as published by the Free Software Foundation. |
attila@90 | 29 | * However, the following notice accompanied the original version of this |
attila@90 | 30 | * file, and Oracle licenses the original version of this file under the BSD |
attila@90 | 31 | * license: |
attila@90 | 32 | */ |
attila@90 | 33 | /* |
attila@90 | 34 | Copyright 2009-2013 Attila Szegedi |
attila@90 | 35 | |
attila@90 | 36 | Licensed under both the Apache License, Version 2.0 (the "Apache License") |
attila@90 | 37 | and the BSD License (the "BSD License"), with licensee being free to |
attila@90 | 38 | choose either of the two at their discretion. |
attila@90 | 39 | |
attila@90 | 40 | You may not use this file except in compliance with either the Apache |
attila@90 | 41 | License or the BSD License. |
attila@90 | 42 | |
attila@90 | 43 | If you choose to use this file in compliance with the Apache License, the |
attila@90 | 44 | following notice applies to you: |
attila@90 | 45 | |
attila@90 | 46 | You may obtain a copy of the Apache License at |
attila@90 | 47 | |
attila@90 | 48 | http://www.apache.org/licenses/LICENSE-2.0 |
attila@90 | 49 | |
attila@90 | 50 | Unless required by applicable law or agreed to in writing, software |
attila@90 | 51 | distributed under the License is distributed on an "AS IS" BASIS, |
attila@90 | 52 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
attila@90 | 53 | implied. See the License for the specific language governing |
attila@90 | 54 | permissions and limitations under the License. |
attila@90 | 55 | |
attila@90 | 56 | If you choose to use this file in compliance with the BSD License, the |
attila@90 | 57 | following notice applies to you: |
attila@90 | 58 | |
attila@90 | 59 | Redistribution and use in source and binary forms, with or without |
attila@90 | 60 | modification, are permitted provided that the following conditions are |
attila@90 | 61 | met: |
attila@90 | 62 | * Redistributions of source code must retain the above copyright |
attila@90 | 63 | notice, this list of conditions and the following disclaimer. |
attila@90 | 64 | * Redistributions in binary form must reproduce the above copyright |
attila@90 | 65 | notice, this list of conditions and the following disclaimer in the |
attila@90 | 66 | documentation and/or other materials provided with the distribution. |
attila@90 | 67 | * Neither the name of the copyright holder nor the names of |
attila@90 | 68 | contributors may be used to endorse or promote products derived from |
attila@90 | 69 | this software without specific prior written permission. |
attila@90 | 70 | |
attila@90 | 71 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
attila@90 | 72 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
attila@90 | 73 | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
attila@90 | 74 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER |
attila@90 | 75 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
attila@90 | 76 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
attila@90 | 77 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
attila@90 | 78 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
attila@90 | 79 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
attila@90 | 80 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
attila@90 | 81 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
attila@90 | 82 | */ |
attila@90 | 83 | |
attila@90 | 84 | package jdk.internal.dynalink.support; |
attila@90 | 85 | |
attila@90 | 86 | import java.util.ArrayList; |
attila@90 | 87 | import java.util.Collection; |
attila@90 | 88 | import java.util.Collections; |
attila@90 | 89 | import java.util.HashMap; |
attila@90 | 90 | import java.util.HashSet; |
attila@90 | 91 | import java.util.IdentityHashMap; |
attila@90 | 92 | import java.util.Iterator; |
attila@90 | 93 | import java.util.List; |
attila@90 | 94 | import java.util.Map; |
attila@90 | 95 | import java.util.Set; |
attila@90 | 96 | |
attila@90 | 97 | /** |
attila@90 | 98 | * Various static utility methods for testing type relationships. |
attila@90 | 99 | * |
attila@90 | 100 | * @author Attila Szegedi |
attila@90 | 101 | */ |
attila@90 | 102 | public class TypeUtilities { |
attila@90 | 103 | static final Class<Object> OBJECT_CLASS = Object.class; |
attila@90 | 104 | |
attila@90 | 105 | private TypeUtilities() { |
attila@90 | 106 | } |
attila@90 | 107 | |
attila@90 | 108 | /** |
attila@90 | 109 | * Given two types represented by c1 and c2, returns a type that is their most specific common superclass or |
attila@90 | 110 | * superinterface. |
attila@90 | 111 | * |
attila@90 | 112 | * @param c1 one type |
attila@90 | 113 | * @param c2 another type |
attila@90 | 114 | * @return their most common superclass or superinterface. If they have several unrelated superinterfaces as their |
attila@90 | 115 | * most specific common type, or the types themselves are completely unrelated interfaces, {@link java.lang.Object} |
attila@90 | 116 | * is returned. |
attila@90 | 117 | */ |
attila@962 | 118 | public static Class<?> getMostSpecificCommonType(final Class<?> c1, final Class<?> c2) { |
attila@90 | 119 | if(c1 == c2) { |
attila@90 | 120 | return c1; |
attila@90 | 121 | } |
attila@90 | 122 | Class<?> c3 = c2; |
attila@90 | 123 | if(c3.isPrimitive()) { |
attila@90 | 124 | if(c3 == Byte.TYPE) |
attila@90 | 125 | c3 = Byte.class; |
attila@90 | 126 | else if(c3 == Short.TYPE) |
attila@90 | 127 | c3 = Short.class; |
attila@90 | 128 | else if(c3 == Character.TYPE) |
attila@90 | 129 | c3 = Character.class; |
attila@90 | 130 | else if(c3 == Integer.TYPE) |
attila@90 | 131 | c3 = Integer.class; |
attila@90 | 132 | else if(c3 == Float.TYPE) |
attila@90 | 133 | c3 = Float.class; |
attila@90 | 134 | else if(c3 == Long.TYPE) |
attila@90 | 135 | c3 = Long.class; |
attila@90 | 136 | else if(c3 == Double.TYPE) |
attila@90 | 137 | c3 = Double.class; |
attila@90 | 138 | } |
attila@962 | 139 | final Set<Class<?>> a1 = getAssignables(c1, c3); |
attila@962 | 140 | final Set<Class<?>> a2 = getAssignables(c3, c1); |
attila@90 | 141 | a1.retainAll(a2); |
attila@90 | 142 | if(a1.isEmpty()) { |
attila@90 | 143 | // Can happen when at least one of the arguments is an interface, |
attila@90 | 144 | // as they don't have Object at the root of their hierarchy. |
attila@90 | 145 | return Object.class; |
attila@90 | 146 | } |
attila@90 | 147 | // Gather maximally specific elements. Yes, there can be more than one |
attila@90 | 148 | // thank to interfaces. I.e., if you call this method for String.class |
attila@90 | 149 | // and Number.class, you'll have Comparable, Serializable, and Object |
attila@90 | 150 | // as maximal elements. |
attila@962 | 151 | final List<Class<?>> max = new ArrayList<>(); |
attila@962 | 152 | outer: for(final Class<?> clazz: a1) { |
attila@962 | 153 | for(final Iterator<Class<?>> maxiter = max.iterator(); maxiter.hasNext();) { |
attila@962 | 154 | final Class<?> maxClazz = maxiter.next(); |
attila@90 | 155 | if(isSubtype(maxClazz, clazz)) { |
attila@90 | 156 | // It can't be maximal, if there's already a more specific |
attila@90 | 157 | // maximal than it. |
attila@90 | 158 | continue outer; |
attila@90 | 159 | } |
attila@90 | 160 | if(isSubtype(clazz, maxClazz)) { |
attila@90 | 161 | // If it's more specific than a currently maximal element, |
attila@90 | 162 | // that currently maximal is no longer a maximal. |
attila@90 | 163 | maxiter.remove(); |
attila@90 | 164 | } |
attila@90 | 165 | } |
attila@90 | 166 | // If we get here, no current maximal is more specific than the |
attila@90 | 167 | // current class, so it is considered maximal as well |
attila@90 | 168 | max.add(clazz); |
attila@90 | 169 | } |
attila@90 | 170 | if(max.size() > 1) { |
attila@90 | 171 | return OBJECT_CLASS; |
attila@90 | 172 | } |
attila@90 | 173 | return max.get(0); |
attila@90 | 174 | } |
attila@90 | 175 | |
attila@962 | 176 | private static Set<Class<?>> getAssignables(final Class<?> c1, final Class<?> c2) { |
attila@962 | 177 | final Set<Class<?>> s = new HashSet<>(); |
attila@90 | 178 | collectAssignables(c1, c2, s); |
attila@90 | 179 | return s; |
attila@90 | 180 | } |
attila@90 | 181 | |
attila@962 | 182 | private static void collectAssignables(final Class<?> c1, final Class<?> c2, final Set<Class<?>> s) { |
attila@90 | 183 | if(c1.isAssignableFrom(c2)) { |
attila@90 | 184 | s.add(c1); |
attila@90 | 185 | } |
attila@962 | 186 | final Class<?> sc = c1.getSuperclass(); |
attila@90 | 187 | if(sc != null) { |
attila@90 | 188 | collectAssignables(sc, c2, s); |
attila@90 | 189 | } |
attila@962 | 190 | final Class<?>[] itf = c1.getInterfaces(); |
attila@90 | 191 | for(int i = 0; i < itf.length; ++i) { |
attila@90 | 192 | collectAssignables(itf[i], c2, s); |
attila@90 | 193 | } |
attila@90 | 194 | } |
attila@90 | 195 | |
attila@90 | 196 | private static final Map<Class<?>, Class<?>> WRAPPER_TYPES = createWrapperTypes(); |
attila@90 | 197 | private static final Map<Class<?>, Class<?>> PRIMITIVE_TYPES = invertMap(WRAPPER_TYPES); |
attila@90 | 198 | private static final Map<String, Class<?>> PRIMITIVE_TYPES_BY_NAME = createClassNameMapping(WRAPPER_TYPES.keySet()); |
attila@90 | 199 | |
attila@90 | 200 | private static Map<Class<?>, Class<?>> createWrapperTypes() { |
attila@90 | 201 | final Map<Class<?>, Class<?>> wrapperTypes = new IdentityHashMap<>(8); |
attila@90 | 202 | wrapperTypes.put(Boolean.TYPE, Boolean.class); |
attila@90 | 203 | wrapperTypes.put(Byte.TYPE, Byte.class); |
attila@90 | 204 | wrapperTypes.put(Character.TYPE, Character.class); |
attila@90 | 205 | wrapperTypes.put(Short.TYPE, Short.class); |
attila@90 | 206 | wrapperTypes.put(Integer.TYPE, Integer.class); |
attila@90 | 207 | wrapperTypes.put(Long.TYPE, Long.class); |
attila@90 | 208 | wrapperTypes.put(Float.TYPE, Float.class); |
attila@90 | 209 | wrapperTypes.put(Double.TYPE, Double.class); |
attila@90 | 210 | return Collections.unmodifiableMap(wrapperTypes); |
attila@90 | 211 | } |
attila@90 | 212 | |
attila@962 | 213 | private static Map<String, Class<?>> createClassNameMapping(final Collection<Class<?>> classes) { |
attila@90 | 214 | final Map<String, Class<?>> map = new HashMap<>(); |
attila@962 | 215 | for(final Class<?> clazz: classes) { |
attila@90 | 216 | map.put(clazz.getName(), clazz); |
attila@90 | 217 | } |
attila@90 | 218 | return map; |
attila@90 | 219 | } |
attila@90 | 220 | |
attila@962 | 221 | private static <K, V> Map<V, K> invertMap(final Map<K, V> map) { |
attila@90 | 222 | final Map<V, K> inverted = new IdentityHashMap<>(map.size()); |
attila@962 | 223 | for(final Map.Entry<K, V> entry: map.entrySet()) { |
attila@90 | 224 | inverted.put(entry.getValue(), entry.getKey()); |
attila@90 | 225 | } |
attila@90 | 226 | return Collections.unmodifiableMap(inverted); |
attila@90 | 227 | } |
attila@90 | 228 | |
attila@90 | 229 | /** |
attila@90 | 230 | * Determines whether one type can be converted to another type using a method invocation conversion, as per JLS 5.3 |
attila@90 | 231 | * "Method Invocation Conversion". This is basically all conversions allowed by subtyping (see |
attila@90 | 232 | * {@link #isSubtype(Class, Class)}) as well as boxing conversion (JLS 5.1.7) optionally followed by widening |
attila@90 | 233 | * reference conversion and unboxing conversion (JLS 5.1.8) optionally followed by widening primitive conversion. |
attila@90 | 234 | * |
attila@90 | 235 | * @param callSiteType the parameter type at the call site |
attila@90 | 236 | * @param methodType the parameter type in the method declaration |
attila@90 | 237 | * @return true if callSiteType is method invocation convertible to the methodType. |
attila@90 | 238 | */ |
attila@962 | 239 | public static boolean isMethodInvocationConvertible(final Class<?> callSiteType, final Class<?> methodType) { |
attila@90 | 240 | if(methodType.isAssignableFrom(callSiteType)) { |
attila@90 | 241 | return true; |
attila@90 | 242 | } |
attila@90 | 243 | if(callSiteType.isPrimitive()) { |
attila@90 | 244 | if(methodType.isPrimitive()) { |
attila@90 | 245 | return isProperPrimitiveSubtype(callSiteType, methodType); |
attila@90 | 246 | } |
attila@90 | 247 | // Boxing + widening reference conversion |
attila@90 | 248 | return methodType.isAssignableFrom(WRAPPER_TYPES.get(callSiteType)); |
attila@90 | 249 | } |
attila@90 | 250 | if(methodType.isPrimitive()) { |
attila@90 | 251 | final Class<?> unboxedCallSiteType = PRIMITIVE_TYPES.get(callSiteType); |
attila@90 | 252 | return unboxedCallSiteType != null |
attila@90 | 253 | && (unboxedCallSiteType == methodType || isProperPrimitiveSubtype(unboxedCallSiteType, methodType)); |
attila@90 | 254 | } |
attila@90 | 255 | return false; |
attila@90 | 256 | } |
attila@90 | 257 | |
attila@90 | 258 | /** |
attila@90 | 259 | * Determines whether one type can be potentially converted to another type at runtime. Allows a conversion between |
attila@90 | 260 | * any subtype and supertype in either direction, and also allows a conversion between any two primitive types, as |
attila@90 | 261 | * well as between any primitive type and any reference type that can hold a boxed primitive. |
attila@90 | 262 | * |
attila@90 | 263 | * @param callSiteType the parameter type at the call site |
attila@90 | 264 | * @param methodType the parameter type in the method declaration |
attila@90 | 265 | * @return true if callSiteType is potentially convertible to the methodType. |
attila@90 | 266 | */ |
attila@962 | 267 | public static boolean isPotentiallyConvertible(final Class<?> callSiteType, final Class<?> methodType) { |
attila@90 | 268 | // Widening or narrowing reference conversion |
attila@90 | 269 | if(methodType.isAssignableFrom(callSiteType) || callSiteType.isAssignableFrom(methodType)) { |
attila@90 | 270 | return true; |
attila@90 | 271 | } |
attila@90 | 272 | if(callSiteType.isPrimitive()) { |
attila@90 | 273 | // Allow any conversion among primitives, as well as from any |
attila@90 | 274 | // primitive to any type that can receive a boxed primitive. |
attila@90 | 275 | // TODO: narrow this a bit, i.e. allow, say, boolean to Character? |
attila@90 | 276 | // MethodHandles.convertArguments() allows it, so we might need to |
attila@90 | 277 | // too. |
attila@90 | 278 | return methodType.isPrimitive() || isAssignableFromBoxedPrimitive(methodType); |
attila@90 | 279 | } |
attila@90 | 280 | if(methodType.isPrimitive()) { |
attila@90 | 281 | // Allow conversion from any reference type that can contain a |
attila@90 | 282 | // boxed primitive to any primitive. |
attila@90 | 283 | // TODO: narrow this a bit too? |
attila@90 | 284 | return isAssignableFromBoxedPrimitive(callSiteType); |
attila@90 | 285 | } |
attila@90 | 286 | return false; |
attila@90 | 287 | } |
attila@90 | 288 | |
attila@90 | 289 | /** |
attila@90 | 290 | * Determines whether one type is a subtype of another type, as per JLS 4.10 "Subtyping". Note: this is not strict |
attila@90 | 291 | * or proper subtype, therefore true is also returned for identical types; to be completely precise, it allows |
attila@90 | 292 | * identity conversion (JLS 5.1.1), widening primitive conversion (JLS 5.1.2) and widening reference conversion (JLS |
attila@90 | 293 | * 5.1.5). |
attila@90 | 294 | * |
attila@90 | 295 | * @param subType the supposed subtype |
attila@90 | 296 | * @param superType the supposed supertype of the subtype |
attila@90 | 297 | * @return true if subType can be converted by identity conversion, widening primitive conversion, or widening |
attila@90 | 298 | * reference conversion to superType. |
attila@90 | 299 | */ |
attila@962 | 300 | public static boolean isSubtype(final Class<?> subType, final Class<?> superType) { |
attila@90 | 301 | // Covers both JLS 4.10.2 "Subtyping among Class and Interface Types" |
attila@90 | 302 | // and JLS 4.10.3 "Subtyping among Array Types", as well as primitive |
attila@90 | 303 | // type identity. |
attila@90 | 304 | if(superType.isAssignableFrom(subType)) { |
attila@90 | 305 | return true; |
attila@90 | 306 | } |
attila@90 | 307 | // JLS 4.10.1 "Subtyping among Primitive Types". Note we don't test for |
attila@90 | 308 | // identity, as identical types were taken care of in the |
attila@90 | 309 | // isAssignableFrom test. As per 4.10.1, the supertype relation is as |
attila@90 | 310 | // follows: |
attila@90 | 311 | // double > float |
attila@90 | 312 | // float > long |
attila@90 | 313 | // long > int |
attila@90 | 314 | // int > short |
attila@90 | 315 | // int > char |
attila@90 | 316 | // short > byte |
attila@90 | 317 | if(superType.isPrimitive() && subType.isPrimitive()) { |
attila@90 | 318 | return isProperPrimitiveSubtype(subType, superType); |
attila@90 | 319 | } |
attila@90 | 320 | return false; |
attila@90 | 321 | } |
attila@90 | 322 | |
attila@90 | 323 | /** |
attila@90 | 324 | * Returns true if a supposed primitive subtype is a proper subtype ( meaning, subtype and not identical) of the |
attila@90 | 325 | * supposed primitive supertype |
attila@90 | 326 | * |
attila@90 | 327 | * @param subType the supposed subtype |
attila@90 | 328 | * @param superType the supposed supertype |
attila@90 | 329 | * @return true if subType is a proper (not identical to) primitive subtype of the superType |
attila@90 | 330 | */ |
attila@962 | 331 | private static boolean isProperPrimitiveSubtype(final Class<?> subType, final Class<?> superType) { |
attila@90 | 332 | if(superType == boolean.class || subType == boolean.class) { |
attila@90 | 333 | return false; |
attila@90 | 334 | } |
attila@90 | 335 | if(subType == byte.class) { |
attila@90 | 336 | return superType != char.class; |
attila@90 | 337 | } |
attila@90 | 338 | if(subType == char.class) { |
attila@90 | 339 | return superType != short.class && superType != byte.class; |
attila@90 | 340 | } |
attila@90 | 341 | if(subType == short.class) { |
attila@90 | 342 | return superType != char.class && superType != byte.class; |
attila@90 | 343 | } |
attila@90 | 344 | if(subType == int.class) { |
attila@90 | 345 | return superType == long.class || superType == float.class || superType == double.class; |
attila@90 | 346 | } |
attila@90 | 347 | if(subType == long.class) { |
attila@90 | 348 | return superType == float.class || superType == double.class; |
attila@90 | 349 | } |
attila@90 | 350 | if(subType == float.class) { |
attila@90 | 351 | return superType == double.class; |
attila@90 | 352 | } |
attila@90 | 353 | return false; |
attila@90 | 354 | } |
attila@90 | 355 | |
attila@90 | 356 | private static final Map<Class<?>, Class<?>> WRAPPER_TO_PRIMITIVE_TYPES = createWrapperToPrimitiveTypes(); |
attila@90 | 357 | |
attila@90 | 358 | private static Map<Class<?>, Class<?>> createWrapperToPrimitiveTypes() { |
attila@90 | 359 | final Map<Class<?>, Class<?>> classes = new IdentityHashMap<>(); |
attila@90 | 360 | classes.put(Void.class, Void.TYPE); |
attila@90 | 361 | classes.put(Boolean.class, Boolean.TYPE); |
attila@90 | 362 | classes.put(Byte.class, Byte.TYPE); |
attila@90 | 363 | classes.put(Character.class, Character.TYPE); |
attila@90 | 364 | classes.put(Short.class, Short.TYPE); |
attila@90 | 365 | classes.put(Integer.class, Integer.TYPE); |
attila@90 | 366 | classes.put(Long.class, Long.TYPE); |
attila@90 | 367 | classes.put(Float.class, Float.TYPE); |
attila@90 | 368 | classes.put(Double.class, Double.TYPE); |
attila@90 | 369 | return classes; |
attila@90 | 370 | } |
attila@90 | 371 | |
attila@90 | 372 | private static final Set<Class<?>> PRIMITIVE_WRAPPER_TYPES = createPrimitiveWrapperTypes(); |
attila@90 | 373 | |
attila@90 | 374 | private static Set<Class<?>> createPrimitiveWrapperTypes() { |
attila@90 | 375 | final Map<Class<?>, Class<?>> classes = new IdentityHashMap<>(); |
attila@90 | 376 | addClassHierarchy(classes, Boolean.class); |
attila@90 | 377 | addClassHierarchy(classes, Byte.class); |
attila@90 | 378 | addClassHierarchy(classes, Character.class); |
attila@90 | 379 | addClassHierarchy(classes, Short.class); |
attila@90 | 380 | addClassHierarchy(classes, Integer.class); |
attila@90 | 381 | addClassHierarchy(classes, Long.class); |
attila@90 | 382 | addClassHierarchy(classes, Float.class); |
attila@90 | 383 | addClassHierarchy(classes, Double.class); |
attila@90 | 384 | return classes.keySet(); |
attila@90 | 385 | } |
attila@90 | 386 | |
attila@962 | 387 | private static void addClassHierarchy(final Map<Class<?>, Class<?>> map, final Class<?> clazz) { |
attila@90 | 388 | if(clazz == null) { |
attila@90 | 389 | return; |
attila@90 | 390 | } |
attila@90 | 391 | map.put(clazz, clazz); |
attila@90 | 392 | addClassHierarchy(map, clazz.getSuperclass()); |
attila@962 | 393 | for(final Class<?> itf: clazz.getInterfaces()) { |
attila@90 | 394 | addClassHierarchy(map, itf); |
attila@90 | 395 | } |
attila@90 | 396 | } |
attila@90 | 397 | |
attila@90 | 398 | /** |
attila@90 | 399 | * Returns true if the class can be assigned from any boxed primitive. |
attila@90 | 400 | * |
attila@90 | 401 | * @param clazz the class |
attila@90 | 402 | * @return true if the class can be assigned from any boxed primitive. Basically, it is true if the class is any |
attila@90 | 403 | * primitive wrapper class, or a superclass or superinterface of any primitive wrapper class. |
attila@90 | 404 | */ |
attila@962 | 405 | private static boolean isAssignableFromBoxedPrimitive(final Class<?> clazz) { |
attila@90 | 406 | return PRIMITIVE_WRAPPER_TYPES.contains(clazz); |
attila@90 | 407 | } |
attila@90 | 408 | |
attila@90 | 409 | /** |
attila@90 | 410 | * Given a name of a primitive type (except "void"), returns the class representing it. I.e. when invoked with |
attila@90 | 411 | * "int", returns {@link Integer#TYPE}. |
attila@90 | 412 | * @param name the name of the primitive type |
attila@90 | 413 | * @return the class representing the primitive type, or null if the name does not correspond to a primitive type |
attila@90 | 414 | * or is "void". |
attila@90 | 415 | */ |
attila@962 | 416 | public static Class<?> getPrimitiveTypeByName(final String name) { |
attila@90 | 417 | return PRIMITIVE_TYPES_BY_NAME.get(name); |
attila@90 | 418 | } |
attila@90 | 419 | |
attila@90 | 420 | /** |
attila@90 | 421 | * When passed a class representing a wrapper for a primitive type, returns the class representing the corresponding |
attila@90 | 422 | * primitive type. I.e. calling it with {@code Integer.class} will return {@code Integer.TYPE}. If passed a class |
attila@90 | 423 | * that is not a wrapper for primitive type, returns null. |
attila@90 | 424 | * @param wrapperType the class object representing a wrapper for a primitive type |
attila@90 | 425 | * @return the class object representing the primitive type, or null if the passed class is not a primitive wrapper. |
attila@90 | 426 | */ |
attila@962 | 427 | public static Class<?> getPrimitiveType(final Class<?> wrapperType) { |
attila@90 | 428 | return WRAPPER_TO_PRIMITIVE_TYPES.get(wrapperType); |
attila@90 | 429 | } |
attila@90 | 430 | |
attila@90 | 431 | |
attila@90 | 432 | /** |
attila@90 | 433 | * When passed a class representing a primitive type, returns the class representing the corresponding |
attila@90 | 434 | * wrapper type. I.e. calling it with {@code int.class} will return {@code Integer.class}. If passed a class |
attila@90 | 435 | * that is not a primitive type, returns null. |
attila@90 | 436 | * @param primitiveType the class object representing a primitive type |
attila@90 | 437 | * @return the class object representing the wrapper type, or null if the passed class is not a primitive. |
attila@90 | 438 | */ |
attila@962 | 439 | public static Class<?> getWrapperType(final Class<?> primitiveType) { |
attila@90 | 440 | return WRAPPER_TYPES.get(primitiveType); |
attila@90 | 441 | } |
attila@101 | 442 | } |