/common/common-lang/src/main/java/com/twelvemonkeys/lang/ReflectUtil.java

https://github.com/conceptboard/TwelveMonkeys · Java · 137 lines · 64 code · 8 blank · 65 comment · 61 complexity · ffeab708b5e086bcd5b6b261ecbbd242 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008, Harald Kuhr
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name "TwelveMonkeys" nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  20. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. package com.twelvemonkeys.lang;
  29. /**
  30. * Util class for various reflection-based operations.
  31. * <p/>
  32. * <em>NOTE: This class is not considered part of the public API and may be
  33. * changed without notice</em>
  34. *
  35. * @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
  36. * @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/lang/ReflectUtil.java#1 $
  37. */
  38. public final class ReflectUtil {
  39. /** Don't allow instances */
  40. private ReflectUtil() {}
  41. /**
  42. * Returns the primitive type for the given wrapper type.
  43. *
  44. * @param pType the wrapper type
  45. *
  46. * @return the primitive type
  47. *
  48. * @throws IllegalArgumentException if {@code pType} is not a primitive
  49. * wrapper
  50. */
  51. public static Class unwrapType(Class pType) {
  52. if (pType == Boolean.class) {
  53. return Boolean.TYPE;
  54. }
  55. else if (pType == Byte.class) {
  56. return Byte.TYPE;
  57. }
  58. else if (pType == Character.class) {
  59. return Character.TYPE;
  60. }
  61. else if (pType == Double.class) {
  62. return Double.TYPE;
  63. }
  64. else if (pType == Float.class) {
  65. return Float.TYPE;
  66. }
  67. else if (pType == Integer.class) {
  68. return Integer.TYPE;
  69. }
  70. else if (pType == Long.class) {
  71. return Long.TYPE;
  72. }
  73. else if (pType == Short.class) {
  74. return Short.TYPE;
  75. }
  76. throw new IllegalArgumentException("Not a primitive wrapper: " + pType);
  77. }
  78. /**
  79. * Returns the wrapper type for the given primitive type.
  80. *
  81. * @param pType the primitive tpye
  82. *
  83. * @return the wrapper type
  84. *
  85. * @throws IllegalArgumentException if {@code pType} is not a primitive
  86. * type
  87. */
  88. public static Class wrapType(Class pType) {
  89. if (pType == Boolean.TYPE) {
  90. return Boolean.class;
  91. }
  92. else if (pType == Byte.TYPE) {
  93. return Byte.class;
  94. }
  95. else if (pType == Character.TYPE) {
  96. return Character.class;
  97. }
  98. else if (pType == Double.TYPE) {
  99. return Double.class;
  100. }
  101. else if (pType == Float.TYPE) {
  102. return Float.class;
  103. }
  104. else if (pType == Integer.TYPE) {
  105. return Integer.class;
  106. }
  107. else if (pType == Long.TYPE) {
  108. return Long.class;
  109. }
  110. else if (pType == Short.TYPE) {
  111. return Short.class;
  112. }
  113. throw new IllegalArgumentException("Not a primitive type: " + pType);
  114. }
  115. /**
  116. * Returns {@code true} if the given type is a primitive wrapper.
  117. *
  118. * @param pType
  119. *
  120. * @return {@code true} if the given type is a primitive wrapper, otherwise
  121. * {@code false}
  122. */
  123. public static boolean isPrimitiveWrapper(Class pType) {
  124. return pType == Boolean.class || pType == Byte.class
  125. || pType == Character.class || pType == Double.class
  126. || pType == Float.class || pType == Integer.class
  127. || pType == Long.class || pType == Short.class;
  128. }
  129. }