libjtype-java-0.1.3/0000755000175000017500000000000012142346612013021 5ustar tonytonylibjtype-java-0.1.3/src/0000755000175000017500000000000012142346612013610 5ustar tonytonylibjtype-java-0.1.3/src/main/0000755000175000017500000000000012142346612014534 5ustar tonytonylibjtype-java-0.1.3/src/main/resources/0000755000175000017500000000000012142346612016546 5ustar tonytonylibjtype-java-0.1.3/src/main/java/0000755000175000017500000000000012142346612015455 5ustar tonytonylibjtype-java-0.1.3/src/main/java/com/0000755000175000017500000000000012142346612016233 5ustar tonytonylibjtype-java-0.1.3/src/main/java/com/googlecode/0000755000175000017500000000000012142346612020342 5ustar tonytonylibjtype-java-0.1.3/src/main/java/com/googlecode/jtype/0000755000175000017500000000000012142346612021475 5ustar tonytonylibjtype-java-0.1.3/src/main/java/com/googlecode/jtype/Utils.java0000644000175000017500000000404111141671627023444 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; /** * Various internal utility methods. * * @author Mark Hobson * @version $Id: Utils.java 2 2009-02-02 22:28:39Z markhobson $ */ final class Utils { // constructors ----------------------------------------------------------- private Utils() { throw new AssertionError(); } // public methods --------------------------------------------------------- public static T checkNotNull(T object, String name) { if (object == null) { throw new NullPointerException(name + " cannot be null"); } return object; } public static void checkTrue(boolean condition, String message) { if (!condition) { throw new IllegalArgumentException(message); } } public static void checkTrue(boolean condition, String message, Object value) { if (!condition) { throw new IllegalArgumentException(message + value); } } public static void checkFalse(boolean condition, String message) { if (condition) { throw new IllegalArgumentException(message); } } public static void checkFalse(boolean condition, String message, Object value) { if (condition) { throw new IllegalArgumentException(message + value); } } public static int nullHashCode(Object object) { return (object != null) ? object.hashCode() : 0; } public static boolean nullEquals(Object object1, Object object2) { return (object1 == null) ? (object2 == null) : object1.equals(object2); } } libjtype-java-0.1.3/src/main/java/com/googlecode/jtype/ClassSerializer.java0000644000175000017500000000211411141671627025442 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; /** * Function that maps classes to a string representation. * * @author Mark Hobson * @version $Id: ClassSerializer.java 2 2009-02-02 22:28:39Z markhobson $ */ public interface ClassSerializer { /** * Gets a string representation of the specified class. * * @param klass * the class to obtain a string representation of * @return the string representation of the specified class */ String toString(Class klass); } libjtype-java-0.1.3/src/main/java/com/googlecode/jtype/ClassSerializers.java0000644000175000017500000000311311245215237025621 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; /** * Factory for creating {@code ClassSerializer}s. * * @author Mark Hobson * @version $Id: ClassSerializers.java 38 2009-08-26 11:21:03Z markhobson $ * @see ClassSerializer */ public final class ClassSerializers { // constants -------------------------------------------------------------- public static final ClassSerializer QUALIFIED = new ClassSerializer() { public String toString(Class klass) { return klass.getName(); } }; public static final ClassSerializer UNQUALIFIED = new ClassSerializer() { public String toString(Class klass) { return ClassUtils.getUnqualifiedClassName(klass); } }; public static final ClassSerializer SIMPLE = new ClassSerializer() { public String toString(Class klass) { return ClassUtils.getSimpleClassName(klass); } }; // constructors ----------------------------------------------------------- private ClassSerializers() { throw new AssertionError(); } } libjtype-java-0.1.3/src/main/java/com/googlecode/jtype/AbstractTypeVisitor.java0000644000175000017500000000504111352504161026322 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import java.lang.reflect.GenericArrayType; import java.lang.reflect.GenericDeclaration; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.lang.reflect.WildcardType; /** * * * @author Mark Hobson * @version $Id: AbstractTypeVisitor.java 73 2010-03-24 21:47:29Z markhobson $ */ abstract class AbstractTypeVisitor implements TypeVisitor { // TypeVisitor methods ---------------------------------------------------- /** * {@inheritDoc} */ public void visit(Class type) { // no-op } /** * {@inheritDoc} */ public boolean beginVisit(TypeVariable type) { return true; } /** * {@inheritDoc} */ public void visitTypeVariableBound(Type bound, int index) { visit(bound); } /** * {@inheritDoc} */ public void endVisit(TypeVariable type) { // no-op } /** * {@inheritDoc} */ public void visit(GenericArrayType type) { visit(type.getGenericComponentType()); } /** * {@inheritDoc} */ public boolean beginVisit(ParameterizedType type) { visit(type.getRawType()); return true; } /** * {@inheritDoc} */ public void visitActualTypeArgument(Type type, int index) { visit(type); } /** * {@inheritDoc} */ public void endVisit(ParameterizedType type) { // no-op } /** * {@inheritDoc} */ public boolean beginVisit(WildcardType type) { return true; } /** * {@inheritDoc} */ public void visitUpperBound(Type bound, int index) { visit(bound); } /** * {@inheritDoc} */ public void visitLowerBound(Type bound, int index) { visit(bound); } /** * {@inheritDoc} */ public void endVisit(WildcardType type) { // no-op } // protected methods ------------------------------------------------------ protected void visit(Type type) { TypeUtils.accept(type, this); } } libjtype-java-0.1.3/src/main/java/com/googlecode/jtype/SerializingTypeVisitor.java0000644000175000017500000000741311663225457027061 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import static com.googlecode.jtype.Utils.checkNotNull; import java.lang.reflect.GenericArrayType; import java.lang.reflect.GenericDeclaration; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.lang.reflect.WildcardType; /** * * * @author Mark Hobson * @version $Id: SerializingTypeVisitor.java 110 2011-11-23 17:19:43Z markhobson $ */ class SerializingTypeVisitor extends AbstractTypeVisitor { // fields ----------------------------------------------------------------- private final ClassSerializer serializer; private final StringBuilder builder; // constructors ----------------------------------------------------------- public SerializingTypeVisitor(ClassSerializer serializer) { checkNotNull(serializer, "serializer"); this.serializer = serializer; builder = new StringBuilder(); } // TypeVisitor methods ---------------------------------------------------- /** * {@inheritDoc} */ @Override public void visit(Class type) { if (type.isArray()) { visit(type.getComponentType()); builder.append("[]"); } else { builder.append(serializer.toString(type)); } } /** * {@inheritDoc} */ @Override public boolean beginVisit(TypeVariable type) { builder.append(type.getName()); return true; } /** * {@inheritDoc} */ @Override public void visitTypeVariableBound(Type bound, int index) { if (!(bound == Object.class && index == 0)) { builder.append((index == 0) ? " extends " : " & "); visit(bound); } } /** * {@inheritDoc} */ @Override public void visit(GenericArrayType type) { visit(type.getGenericComponentType()); builder.append("[]"); } /** * {@inheritDoc} */ @Override public boolean beginVisit(ParameterizedType type) { Type ownerType = type.getOwnerType(); if (ownerType != null) { visit(ownerType); builder.append("."); } visit(type.getRawType()); if (type.getActualTypeArguments().length > 0) { builder.append("<"); } return true; } /** * {@inheritDoc} */ @Override public void visitActualTypeArgument(Type type, int index) { if (index > 0) { builder.append(", "); } visit(type); } /** * {@inheritDoc} */ @Override public void endVisit(ParameterizedType type) { if (type.getActualTypeArguments().length > 0) { builder.append(">"); } } /** * {@inheritDoc} */ @Override public boolean beginVisit(WildcardType type) { builder.append("?"); return true; } /** * {@inheritDoc} */ @Override public void visitUpperBound(Type bound, int index) { if (!(bound == Object.class && index == 0)) { builder.append((index == 0) ? " extends " : " & "); visit(bound); } } /** * {@inheritDoc} */ @Override public void visitLowerBound(Type bound, int index) { builder.append((index == 0) ? " super " : " & "); visit(bound); } // Object methods --------------------------------------------------------- /** * {@inheritDoc} */ @Override public String toString() { return builder.toString(); } } libjtype-java-0.1.3/src/main/java/com/googlecode/jtype/Generic.java0000644000175000017500000001556211663225457023737 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import static com.googlecode.jtype.Utils.checkFalse; import static com.googlecode.jtype.Utils.checkNotNull; import static com.googlecode.jtype.Utils.checkTrue; import java.io.Serializable; import java.lang.reflect.GenericArrayType; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.lang.reflect.WildcardType; import java.util.Collections; import java.util.HashMap; import java.util.Map; /** * Provides a generic type literal. *

* This class captures the actual type argument used when subclassed. This allows it to be referenced as a type * parameter at compile time and also makes it available at run time. It is intended to be used as follows: *

* {@code Generic> listStringType = new Generic>() }{}; *

* This allows generic type literals to be used in a simple manner as standard class literals. For example, consider the * following generic method signature: *

* {@code void add(T element, Class type)} *

* A problem arises when {@code } is a generic type, such as {@code List}, since {@code List.class} * produces a compile time error. Use of this class can mitigate this problem: *

* {@code void add(T element, Generic type)} *

* Which can then be invoked as follows: *

* {@code add(new ArrayList(), new Generic>() }{}); * * @author Mark Hobson * @version $Id: Generic.java 110 2011-11-23 17:19:43Z markhobson $ * @param the type that this generic type literal represents * @see Generics * @see Neal Gafter's blog: Super Type Tokens */ public abstract class Generic implements Serializable { // classes ---------------------------------------------------------------- private static final class DefaultGeneric extends Generic { public DefaultGeneric(Type type) { super(type); } } /** * Simple read-only cache for common generics. Implemented as an inner class for lazy instantiation. */ private static class GenericCache { private static final Map> GENERICS_BY_TYPE = createCache(); public static Generic get(Type type) { return GENERICS_BY_TYPE.get(type); } } // constants -------------------------------------------------------------- private static final long serialVersionUID = 1L; // fields ----------------------------------------------------------------- /** * The type that this generic type literal represents. * * @serial */ private final Type type; // constructors ----------------------------------------------------------- protected Generic() { Type type = getActualTypeArgument(); validateType(type); this.type = type; } Generic(Type type) { validateType(type); this.type = type; } // public methods --------------------------------------------------------- public Type getType() { return type; } @SuppressWarnings("unchecked") public Class getRawType() { return (Class) TypeUtils.getErasedReferenceType(type); } public String toUnqualifiedString() { return TypeUtils.toUnqualifiedString(type); } public static Generic get(Class klass) { // guaranteed by definition @SuppressWarnings("unchecked") Generic generic = (Generic) get((Type) klass); return generic; } public static Generic get(Type type) { Generic generic = GenericCache.get(type); if (generic == null) { generic = create(type); } return generic; } @SuppressWarnings("unchecked") public static Generic get(Class rawType, Type... actualTypeArguments) { if (actualTypeArguments == null || actualTypeArguments.length == 0) { return get(rawType); } ParameterizedType paramType = Types.parameterizedType(rawType, actualTypeArguments); return (Generic) get(paramType); } public static Generic valueOf(String typeName) { return get(Types.valueOf(typeName)); } // Object methods --------------------------------------------------------- /** * {@inheritDoc} */ @Override public int hashCode() { return type.hashCode(); } /** * {@inheritDoc} */ @Override public boolean equals(Object object) { if (!(object instanceof Generic)) { return false; } Generic generic = (Generic) object; return type.equals(generic.getType()); } /** * {@inheritDoc} */ @Override public String toString() { return TypeUtils.toString(type); } // private methods -------------------------------------------------------- private static Map> createCache() { Map> genericsByType = new HashMap>(); putCacheEntry(genericsByType, Object.class); putCacheEntry(genericsByType, Boolean.class); putCacheEntry(genericsByType, Byte.class); putCacheEntry(genericsByType, Character.class); putCacheEntry(genericsByType, Double.class); putCacheEntry(genericsByType, Float.class); putCacheEntry(genericsByType, Integer.class); putCacheEntry(genericsByType, Long.class); putCacheEntry(genericsByType, Short.class); putCacheEntry(genericsByType, String.class); return Collections.unmodifiableMap(genericsByType); } private static void putCacheEntry(Map> genericsByType, Type type) { genericsByType.put(type, create(type)); } private static Generic create(Type type) { return new DefaultGeneric(type); } private static void validateType(Type type) { checkNotNull(type, "type"); checkFalse(type instanceof TypeVariable, "Type variables are not supported: ", type); checkFalse(type instanceof WildcardType, "Wildcard types are not supported: ", type); checkTrue(type instanceof Class || type instanceof ParameterizedType || type instanceof GenericArrayType, "Unsupported type: ", type); } private Type getActualTypeArgument() { if (getClass().getSuperclass() != Generic.class) { throw new IllegalStateException("Generic must only be subclassed once"); } Type superclass = getClass().getGenericSuperclass(); return ((ParameterizedType) superclass).getActualTypeArguments()[0]; } } libjtype-java-0.1.3/src/main/java/com/googlecode/jtype/DefaultWildcardType.java0000644000175000017500000000633511663225457026261 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import static com.googlecode.jtype.Utils.checkFalse; import java.io.Serializable; import java.lang.reflect.Type; import java.lang.reflect.WildcardType; import java.util.Arrays; /** * Default implementation of a wildcard type. * * @author Mark Hobson * @version $Id: DefaultWildcardType.java 110 2011-11-23 17:19:43Z markhobson $ * @see WildcardType */ class DefaultWildcardType implements WildcardType, Serializable { // constants -------------------------------------------------------------- private static final Type[] DEFAULT_UPPER_BOUNDS = new Type[] {Object.class}; private static final Type[] DEFAULT_LOWER_BOUNDS = new Type[0]; private static final long serialVersionUID = 1L; // fields ----------------------------------------------------------------- /** * The upper bound(s) of this type variable. * * @serial */ private final Type[] upperBounds; /** * The lower bound(s) of this type variable. * * @serial */ private final Type[] lowerBounds; // constructors ----------------------------------------------------------- public DefaultWildcardType(Type[] upperBounds, Type[] lowerBounds) { if (upperBounds == null || upperBounds.length == 0) { upperBounds = DEFAULT_UPPER_BOUNDS; } if (lowerBounds == null) { lowerBounds = DEFAULT_LOWER_BOUNDS; } this.upperBounds = upperBounds.clone(); this.lowerBounds = lowerBounds.clone(); boolean hasUpperBounds = !Arrays.equals(this.upperBounds, DEFAULT_UPPER_BOUNDS); boolean hasLowerBounds = !Arrays.equals(this.lowerBounds, DEFAULT_LOWER_BOUNDS); checkFalse(hasUpperBounds && hasLowerBounds, "Wildcard type cannot have both upper and lower bounds"); } // WildcardType methods --------------------------------------------------- /** * {@inheritDoc} */ public Type[] getUpperBounds() { return upperBounds.clone(); } /** * {@inheritDoc} */ public Type[] getLowerBounds() { return lowerBounds.clone(); } // Object methods --------------------------------------------------------- /** * {@inheritDoc} */ @Override public int hashCode() { return Arrays.hashCode(lowerBounds) ^ Arrays.hashCode(upperBounds); } /** * {@inheritDoc} */ @Override public boolean equals(Object object) { if (!(object instanceof WildcardType)) { return false; } WildcardType wildcardType = (WildcardType) object; return Arrays.equals(lowerBounds, wildcardType.getLowerBounds()) && Arrays.equals(upperBounds, wildcardType.getUpperBounds()); } /** * {@inheritDoc} */ @Override public String toString() { return TypeUtils.toString(this); } } libjtype-java-0.1.3/src/main/java/com/googlecode/jtype/Generics.java0000644000175000017500000001434011465002475024104 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import java.util.Collection; import java.util.Comparator; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Map; import java.util.Queue; import java.util.Set; import java.util.SortedMap; import java.util.SortedSet; /** * Factory for creating common generics. * * @author Mark Hobson * @version $Id: Generics.java 86 2010-11-05 13:23:09Z markhobson $ * @see Generic */ @SuppressWarnings("unchecked") public final class Generics { // constants -------------------------------------------------------------- private static final Generic> COMPARABLE = (Generic>) Generic.get(Comparable.class, Types.unboundedWildcardType()); private static final Generic> COMPARATOR = (Generic>) Generic.get(Comparator.class, Types.unboundedWildcardType()); private static final Generic> ENUMERATION = (Generic>) Generic.get(Enumeration.class, Types.unboundedWildcardType()); private static final Generic> ITERABLE = (Generic>) Generic.get(Iterable.class, Types.unboundedWildcardType()); private static final Generic> ITERATOR = (Generic>) Generic.get(Iterator.class, Types.unboundedWildcardType()); private static final Generic> LIST_ITERATOR = (Generic>) Generic.get( ListIterator.class, Types.unboundedWildcardType()); private static final Generic> COLLECTION = (Generic>) Generic.get(Collection.class, Types.unboundedWildcardType()); private static final Generic> SET = (Generic>) Generic.get(Set.class, Types.unboundedWildcardType()); private static final Generic> SORTED_SET = (Generic>) Generic.get(SortedSet.class, Types.unboundedWildcardType()); private static final Generic> LIST = (Generic>) Generic.get(List.class, Types.unboundedWildcardType()); private static final Generic> MAP = (Generic>) Generic.get(Map.class, Types.unboundedWildcardType(), Types.unboundedWildcardType()); private static final Generic> SORTED_MAP = (Generic>) Generic.get(SortedMap.class, Types.unboundedWildcardType(), Types.unboundedWildcardType()); private static final Generic> QUEUE = (Generic>) Generic.get(Queue.class, Types.unboundedWildcardType()); // constructors ----------------------------------------------------------- private Generics() { throw new AssertionError(); } // public methods --------------------------------------------------------- public static Generic> comparable() { return COMPARABLE; } public static Generic> comparable(Class type) { return (Generic>) Generic.get(Comparable.class, type); } public static Generic> comparator() { return COMPARATOR; } public static Generic> comparator(Class type) { return (Generic>) Generic.get(Comparator.class, type); } public static Generic> enumeration() { return ENUMERATION; } public static Generic> enumeration(Class elementClass) { return (Generic>) Generic.get(Enumeration.class, elementClass); } public static Generic> iterable() { return ITERABLE; } public static Generic> iterable(Class elementClass) { return (Generic>) Generic.get(Iterable.class, elementClass); } public static Generic> iterator() { return ITERATOR; } public static Generic> iterator(Class elementClass) { return (Generic>) Generic.get(Iterator.class, elementClass); } public static Generic> listIterator() { return LIST_ITERATOR; } public static Generic> listIterator(Class elementClass) { return (Generic>) Generic.get(ListIterator.class, elementClass); } public static Generic> collection() { return COLLECTION; } public static Generic> collection(Class elementClass) { return (Generic>) Generic.get(Collection.class, elementClass); } public static Generic> set() { return SET; } public static Generic> set(Class elementClass) { return (Generic>) Generic.get(Set.class, elementClass); } public static Generic> sortedSet() { return SORTED_SET; } public static Generic> sortedSet(Class elementClass) { return (Generic>) Generic.get(SortedSet.class, elementClass); } public static Generic> list() { return LIST; } public static Generic> list(Class elementClass) { return (Generic>) Generic.get(List.class, elementClass); } public static Generic> map() { return MAP; } public static Generic> map(Class keyClass, Class valueClass) { return (Generic>) Generic.get(Map.class, keyClass, valueClass); } public static Generic> sortedMap() { return SORTED_MAP; } public static Generic> sortedMap(Class keyClass, Class valueClass) { return (Generic>) Generic.get(SortedMap.class, keyClass, valueClass); } public static Generic> queue() { return QUEUE; } public static Generic> queue(Class elementClass) { return (Generic>) Generic.get(Queue.class, elementClass); } } libjtype-java-0.1.3/src/main/java/com/googlecode/jtype/TypeVisitor.java0000644000175000017500000000324311663226156024652 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import java.lang.reflect.GenericArrayType; import java.lang.reflect.GenericDeclaration; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.lang.reflect.WildcardType; /** * * * @author Mark Hobson * @version $Id: TypeVisitor.java 111 2011-11-23 17:25:02Z markhobson $ */ public interface TypeVisitor { void visit(Class type); // TODO: rename to visit boolean beginVisit(TypeVariable type); void visitTypeVariableBound(Type bound, int index); void endVisit(TypeVariable type); void visit(GenericArrayType type); // TODO: rename to visit boolean beginVisit(ParameterizedType type); void visitActualTypeArgument(Type type, int index); void endVisit(ParameterizedType type); // TODO: rename to visit boolean beginVisit(WildcardType type); void visitUpperBound(Type bound, int index); void visitLowerBound(Type bound, int index); void endVisit(WildcardType type); } libjtype-java-0.1.3/src/main/java/com/googlecode/jtype/ClassUtils.java0000644000175000017500000001032611463521342024430 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import java.lang.reflect.Array; import java.util.Collections; import java.util.HashMap; import java.util.Map; /** * Provides utility methods for working with classes. * * @author Mark Hobson * @version $Id: ClassUtils.java 82 2010-11-01 11:22:10Z markhobson $ * @see Class */ final class ClassUtils { // constants -------------------------------------------------------------- private static final Map PRIMITIVE_DESCRIPTORS_BY_CLASS_NAME = createPrimitiveDescriptorsByClassName(); // constructors ----------------------------------------------------------- private ClassUtils() { throw new AssertionError(); } // public methods --------------------------------------------------------- public static String getUnqualifiedClassName(Class klass) { return getUnqualifiedClassName(klass.getName()); } public static String getUnqualifiedClassName(String className) { int dot = className.lastIndexOf('.'); return (dot == -1) ? className : className.substring(dot + 1); } public static String getSimpleClassName(Class klass) { return getSimpleClassName(klass.getName()); } public static String getSimpleClassName(String className) { int index = className.lastIndexOf('$'); if (index == -1) { index = className.lastIndexOf('.'); } return (index == -1) ? className : className.substring(index + 1); } public static Class getArrayType(Class componentType) { return Array.newInstance(componentType, 0).getClass(); } public static Class valueOf(String className) { if (isPrimitiveClassName(className)) { return valueOfPrimitive(className); } ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); try { return Class.forName(className, true, classLoader); } catch (ClassNotFoundException exception) { return null; } } // private methods -------------------------------------------------------- private static Map createPrimitiveDescriptorsByClassName() { Map primitiveDescriptorsByClassName = new HashMap(); // from JVM specification 4.3.2 // see http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#84645 primitiveDescriptorsByClassName.put("byte", "B"); primitiveDescriptorsByClassName.put("char", "C"); primitiveDescriptorsByClassName.put("double", "D"); primitiveDescriptorsByClassName.put("float", "F"); primitiveDescriptorsByClassName.put("int", "I"); primitiveDescriptorsByClassName.put("long", "J"); primitiveDescriptorsByClassName.put("short", "S"); primitiveDescriptorsByClassName.put("boolean", "Z"); return Collections.unmodifiableMap(primitiveDescriptorsByClassName); } private static Class valueOfPrimitive(String className) { // cannot load primitives directly so load primitive array type and use component type instead String descriptor = getPrimitiveDescriptor(className); String arrayDescriptor = getArrayDescriptor(descriptor); Class arrayType = valueOf(arrayDescriptor); return arrayType.getComponentType(); } private static boolean isPrimitiveClassName(String className) { return PRIMITIVE_DESCRIPTORS_BY_CLASS_NAME.containsKey(className); } private static String getPrimitiveDescriptor(String className) { if (!isPrimitiveClassName(className)) { throw new IllegalArgumentException("className is not a primitive class name: " + className); } return PRIMITIVE_DESCRIPTORS_BY_CLASS_NAME.get(className); } private static String getArrayDescriptor(String componentDescriptor) { return "[" + componentDescriptor; } } libjtype-java-0.1.3/src/main/java/com/googlecode/jtype/DefaultTypeVariable.java0000644000175000017500000001135411663225457026252 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import static com.googlecode.jtype.Utils.checkNotNull; import static com.googlecode.jtype.Utils.checkTrue; import java.io.Serializable; import java.lang.reflect.GenericDeclaration; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.util.Arrays; /** * Default implementation of a type variable. * * @author Mark Hobson * @version $Id: DefaultTypeVariable.java 110 2011-11-23 17:19:43Z markhobson $ * @param * the type of generic declaration that declared the type variable * @see TypeVariable */ class DefaultTypeVariable implements TypeVariable, Serializable { // constants -------------------------------------------------------------- private static final Type[] DEFAULT_BOUNDS = new Type[] {Object.class}; private static final long serialVersionUID = 1L; // fields ----------------------------------------------------------------- /** * The generic declaration that declared this type variable. * * @serial */ private final D declaration; /** * The name of this type variable, as it occurs in the source code. * * @serial */ private final String name; /** * The upper bound(s) of this type variable. * * @serial */ private final Type[] bounds; // constructors ----------------------------------------------------------- public DefaultTypeVariable(D declaration, String name, Type[] bounds) { if (bounds == null || bounds.length == 0) { bounds = DEFAULT_BOUNDS; } // initial bound must be either a class type, an interface type or a type variable checkTrue(isValidFirstBound(bounds[0]), "First bound must be either a class type, an interface type or a type " + "variable", bounds[0]); // subsequent bounds must be an interface type for (int i = 1; i < bounds.length; i++) { checkTrue(isValidSecondaryBound(bounds[i]), "Secondary bounds must be an interface type: ", bounds[i]); } // TODO: the erasures of all constituent types of a bound must be pairwise different // TODO: type variable may not be a subtype of two interface types which are different parameterizations of the // same generic interface this.declaration = checkNotNull(declaration, "declaration"); this.name = checkNotNull(name, "name"); this.bounds = bounds.clone(); } // TypeVariable methods --------------------------------------------------- /** * {@inheritDoc} */ public D getGenericDeclaration() { return declaration; } /** * {@inheritDoc} */ public String getName() { return name; } /** * {@inheritDoc} */ public Type[] getBounds() { return bounds.clone(); } // Object methods --------------------------------------------------------- /** * {@inheritDoc} */ @Override public int hashCode() { int hashCode = declaration.hashCode(); hashCode = (hashCode * 37) + name.hashCode(); hashCode = (hashCode * 37) + Arrays.hashCode(bounds); return hashCode; } /** * {@inheritDoc} */ @Override public boolean equals(Object object) { if (!(object instanceof TypeVariable)) { return false; } TypeVariable typeVariable = (TypeVariable) object; return declaration.equals(typeVariable.getGenericDeclaration()) && name.equals(typeVariable.getName()) && Arrays.equals(bounds, typeVariable.getBounds()); } /** * {@inheritDoc} */ @Override public String toString() { return TypeUtils.toString(this); } // private methods -------------------------------------------------------- private static boolean isValidFirstBound(Type bound) { return (bound instanceof Class && !((Class) bound).isArray()) || (bound instanceof ParameterizedType) || (bound instanceof TypeVariable); } private static boolean isValidSecondaryBound(Type bound) { if (bound instanceof Class) { return ((Class) bound).isInterface(); } if (bound instanceof ParameterizedType) { Type rawType = ((ParameterizedType) bound).getRawType(); return (rawType instanceof Class) && ((Class) rawType).isInterface(); } return false; } } libjtype-java-0.1.3/src/main/java/com/googlecode/jtype/TypeUtils.java0000644000175000017500000004663611663755377024344 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import static com.googlecode.jtype.Utils.checkNotNull; import static com.googlecode.jtype.Utils.checkTrue; import java.io.Serializable; import java.lang.reflect.GenericArrayType; import java.lang.reflect.MalformedParameterizedTypeException; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.lang.reflect.WildcardType; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; /** * Provides utility methods for working with types. * * @author Mark Hobson * @version $Id: TypeUtils.java 113 2011-11-25 18:14:23Z markhobson@gmail.com $ */ public final class TypeUtils { // constants -------------------------------------------------------------- private static final Map, Set>> SUBTYPES_BY_PRIMITIVE; static { Map, Set>> subtypesByPrimitive = new HashMap, Set>>(); putPrimitiveSubtypes(subtypesByPrimitive, Void.TYPE); putPrimitiveSubtypes(subtypesByPrimitive, Boolean.TYPE); putPrimitiveSubtypes(subtypesByPrimitive, Byte.TYPE); putPrimitiveSubtypes(subtypesByPrimitive, Character.TYPE); putPrimitiveSubtypes(subtypesByPrimitive, Short.TYPE, Byte.TYPE); putPrimitiveSubtypes(subtypesByPrimitive, Integer.TYPE, Character.TYPE, Short.TYPE); putPrimitiveSubtypes(subtypesByPrimitive, Long.TYPE, Integer.TYPE); putPrimitiveSubtypes(subtypesByPrimitive, Float.TYPE, Long.TYPE); putPrimitiveSubtypes(subtypesByPrimitive, Double.TYPE, Float.TYPE); SUBTYPES_BY_PRIMITIVE = Collections.unmodifiableMap(subtypesByPrimitive); } // constructors ----------------------------------------------------------- private TypeUtils() { throw new AssertionError(); } // public methods --------------------------------------------------------- public static void accept(Type type, TypeVisitor visitor) { checkNotNull(type, "type"); checkNotNull(visitor, "visitor"); if (type instanceof Class) { visitor.visit((Class) type); } else if (type instanceof TypeVariable) { TypeVariable typeVariable = (TypeVariable) type; if (visitor.beginVisit(typeVariable)) { // visit bounds Type[] bounds = typeVariable.getBounds(); for (int i = 0; i < bounds.length; i++) { visitor.visitTypeVariableBound(bounds[i], i); } } visitor.endVisit(typeVariable); } else if (type instanceof GenericArrayType) { visitor.visit((GenericArrayType) type); } else if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; if (visitor.beginVisit(parameterizedType)) { // visit actual type arguments Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); for (int i = 0; i < actualTypeArguments.length; i++) { visitor.visitActualTypeArgument(actualTypeArguments[i], i); } } visitor.endVisit(parameterizedType); } else if (type instanceof WildcardType) { WildcardType wildcardType = (WildcardType) type; if (visitor.beginVisit(wildcardType)) { // visit upper bounds Type[] upperBounds = wildcardType.getUpperBounds(); for (int i = 0; i < upperBounds.length; i++) { visitor.visitUpperBound(upperBounds[i], i); } // visit lower bounds Type[] lowerBounds = wildcardType.getLowerBounds(); for (int i = 0; i < lowerBounds.length; i++) { visitor.visitLowerBound(lowerBounds[i], i); } } visitor.endVisit(wildcardType); } else { throw new IllegalArgumentException("Unknown type: " + type); } } public static boolean isAssignable(Type supertype, Type type) { checkNotNull(supertype, "supertype"); checkNotNull(type, "type"); if (supertype.equals(type)) { return true; } if (supertype instanceof Class) { if (type instanceof Class) { return isClassAssignable((Class) supertype, (Class) type); } if (type instanceof ParameterizedType) { return isAssignable(supertype, ((ParameterizedType) type).getRawType()); } if (type instanceof TypeVariable) { return isTypeVariableAssignable(supertype, (TypeVariable) type); } if (type instanceof GenericArrayType) { if (((Class) supertype).isArray()) { return isAssignable(getComponentType(supertype), getComponentType(type)); } return isArraySupertype((Class) supertype); } if (type instanceof WildcardType) { return isClassAssignableToWildcardType((Class) supertype, (WildcardType) type); } return false; } if (supertype instanceof ParameterizedType) { if (type instanceof Class) { return isSuperAssignable(supertype, type); } if (type instanceof ParameterizedType) { return isParameterizedTypeAssignable((ParameterizedType) supertype, (ParameterizedType) type); } return false; } if (type instanceof TypeVariable) { return isTypeVariableAssignable(supertype, (TypeVariable) type); } if (supertype instanceof GenericArrayType) { if (isArray(type)) { return isAssignable(getComponentType(supertype), getComponentType(type)); } return false; } if (supertype instanceof WildcardType) { return isWildcardTypeAssignable((WildcardType) supertype, type); } return false; } public static boolean isInstance(Type type, Object object) { return getErasedReferenceType(type).isInstance(object); } /** * Gets the erased type of the specified type. * * @param type * the type to perform erasure on * @return the erased type, never a parameterized type nor a type variable * @see 4.6 Type Erasure */ public static Type getErasedType(Type type) { // the erasure of a parameterized type G is |G| if (type instanceof ParameterizedType) { Type rawType = ((ParameterizedType) type).getRawType(); return getErasedType(rawType); } // TODO: the erasure of a nested type T.C is |T|.C // the erasure of an array type T[] is |T|[] if (isArray(type)) { Type componentType = getComponentType(type); Type erasedComponentType = getErasedType(componentType); return getArrayType(erasedComponentType); } // the erasure of a type variable is the erasure of its leftmost bound if (type instanceof TypeVariable) { Type[] bounds = ((TypeVariable) type).getBounds(); return getErasedType(bounds[0]); } // the erasure of every other type is the type itself return type; } public static Class getErasedReferenceType(Type type) { checkTrue(isReferenceType(type), "type is not a reference type: ", type); return (Class) getErasedType(type); } /** * @deprecated Use {@link #getErasedReferenceType(Type)} instead. */ @Deprecated public static Class getRawType(Type type) { return getErasedReferenceType(type); } public static boolean isArray(Type type) { return (type instanceof Class && ((Class) type).isArray()) || (type instanceof GenericArrayType); } public static boolean isPrimitive(Type type) { return (type instanceof Class) && ((Class) type).isPrimitive(); } public static Type getComponentType(Type type) { if (type instanceof Class) { Class klass = (Class) type; return klass.isArray() ? klass.getComponentType() : null; } if (type instanceof GenericArrayType) { return ((GenericArrayType) type).getGenericComponentType(); } return null; } public static Type getArrayType(Type componentType) { checkNotNull(componentType, "componentType"); if (componentType instanceof Class) { return ClassUtils.getArrayType((Class) componentType); } return Types.genericArrayType(componentType); } public static boolean isParameterizedType(Type type, Class rawType) { checkNotNull(type, "type"); checkNotNull(rawType, "rawType"); ParameterizedType parameterizedType; try { parameterizedType = Types.unboundedParameterizedType(rawType); } catch (MalformedParameterizedTypeException exception) { return false; } return isAssignable(parameterizedType, type); } /** * @deprecated Use {@link #isParameterizedType(Type, Class)} instead. */ @Deprecated public static boolean isSimpleParameterizedType(Type type, Class rawType) { if (!isParameterizedType(type, rawType)) { return false; } Type[] typeArgs = ((ParameterizedType) type).getActualTypeArguments(); return (typeArgs.length == 1); } /** * @deprecated Use {@link #getActualTypeArgument(Type, int)} instead. */ @Deprecated public static Type getActualTypeArgument(Type type) { return getActualTypeArgument(type, 0); } public static Type getActualTypeArgument(Type type, int argIndex) { checkNotNull(type, "type"); checkTrue(type instanceof ParameterizedType, "type must be a ParameterizedType: ", type); ParameterizedType paramType = (ParameterizedType) type; Type[] typeArgs = paramType.getActualTypeArguments(); checkTrue(argIndex >= 0, "argIndex must be non-negative: ", argIndex); checkTrue(argIndex < typeArgs.length, "argIndex must be less than the number of type parameters: ", argIndex); return typeArgs[argIndex]; } public static Type getResolvedSuperclass(Type type) { checkNotNull(type, "type"); Class rawType = getErasedReferenceType(type); Type supertype = rawType.getGenericSuperclass(); if (supertype == null) { return null; } return resolveTypeVariables(supertype, type); } public static Type[] getResolvedInterfaces(Type type) { checkNotNull(type, "type"); Class rawType = getErasedReferenceType(type); Type[] interfaces = rawType.getGenericInterfaces(); Type[] resolvedInterfaces = new Type[interfaces.length]; for (int i = 0; i < interfaces.length; i++) { resolvedInterfaces[i] = resolveTypeVariables(interfaces[i], type); } return resolvedInterfaces; } public static Type getResolvedSupertype(Class type, Class rawSupertype) { Type resolvedSupertype = getResolvedSupertype((Type) type, rawSupertype); if (resolvedSupertype == null) { throw new IllegalStateException("type must extend or implement supertype: " + type.getName()); } return resolvedSupertype; } public static String toString(Type type) { return toString(type, ClassSerializers.QUALIFIED); } public static String toString(Type type, ClassSerializer serializer) { if (type == null) { return String.valueOf(type); } SerializingTypeVisitor visitor = new SerializingTypeVisitor(serializer); accept(type, visitor); return visitor.toString(); } public static String toUnqualifiedString(Type type) { return toString(type, ClassSerializers.UNQUALIFIED); } public static String toSimpleString(Type type) { return toString(type, ClassSerializers.SIMPLE); } // private methods -------------------------------------------------------- private static void putPrimitiveSubtypes(Map, Set>> subtypesByPrimitive, Class primitiveType, Class... directSubtypes) { Set> subtypes = new HashSet>(); for (Class directSubtype : directSubtypes) { subtypes.add(directSubtype); subtypes.addAll(subtypesByPrimitive.get(directSubtype)); } subtypesByPrimitive.put(primitiveType, Collections.unmodifiableSet(subtypes)); } private static boolean isClassAssignable(Class supertype, Class type) { // Class.isAssignableFrom does not perform primitive widening if (supertype.isPrimitive() && type.isPrimitive()) { return SUBTYPES_BY_PRIMITIVE.get(supertype).contains(type); } return supertype.isAssignableFrom(type); } private static boolean isClassAssignableToWildcardType(Class supertype, WildcardType type) { for (Type upperBound : type.getUpperBounds()) { if (!isAssignable(supertype, upperBound)) { return false; } } return true; } private static boolean isParameterizedTypeAssignable(ParameterizedType supertype, ParameterizedType type) { Type rawSupertype = supertype.getRawType(); Type rawType = type.getRawType(); if (!rawSupertype.equals(rawType)) { // short circuit when class raw types are unassignable if (rawSupertype instanceof Class && rawType instanceof Class && !(((Class) rawSupertype).isAssignableFrom((Class) rawType))) { return false; } return isSuperAssignable(supertype, type); } Type[] supertypeArgs = supertype.getActualTypeArguments(); Type[] typeArgs = type.getActualTypeArguments(); if (supertypeArgs.length != typeArgs.length) { return false; } for (int i = 0; i < supertypeArgs.length; i++) { Type supertypeArg = supertypeArgs[i]; Type typeArg = typeArgs[i]; if (supertypeArg instanceof WildcardType) { if (!isWildcardTypeAssignable((WildcardType) supertypeArg, typeArg)) { return false; } } else if (!supertypeArg.equals(typeArg)) { return false; } } return true; } private static boolean isTypeVariableAssignable(Type supertype, TypeVariable type) { for (Type bound : type.getBounds()) { if (isAssignable(supertype, bound)) { return true; } } return false; } private static boolean isWildcardTypeAssignable(WildcardType supertype, Type type) { for (Type upperBound : supertype.getUpperBounds()) { if (!isAssignable(upperBound, type)) { return false; } } for (Type lowerBound : supertype.getLowerBounds()) { if (!isAssignable(type, lowerBound)) { return false; } } return true; } private static boolean isSuperAssignable(Type supertype, Type type) { Type superclass = getResolvedSuperclass(type); if (superclass != null && isAssignable(supertype, superclass)) { return true; } for (Type interphace : getResolvedInterfaces(type)) { if (isAssignable(supertype, interphace)) { return true; } } return false; } /** * Gets whether the specified type is a reference type. *

* More specifically, this method returns {@code true} if the specified type is one of the following: *

    *
  • a class type
  • *
  • an interface type
  • *
  • an array type
  • *
  • a parameterized type
  • *
  • a type variable
  • *
  • the null type
  • *
* * @param type * the type to check * @return {@code true} if the specified type is a reference type * @see 4.3 Reference Types and Values */ private static boolean isReferenceType(Type type) { return type == null || type instanceof Class || type instanceof ParameterizedType || type instanceof TypeVariable || type instanceof GenericArrayType; } private static boolean isArraySupertype(Class type) { return Object.class.equals(type) || Cloneable.class.equals(type) || Serializable.class.equals(type); } private static Type resolveTypeVariables(Type type, Type subtype) { // TODO: need to support other types in future, e.g. T[], etc. if (!(type instanceof ParameterizedType)) { return type; } Map actualTypeArgumentsByParameter = getActualTypeArgumentsByParameter(type, subtype); Class rawType = getErasedReferenceType(type); return parameterizeClass(rawType, actualTypeArgumentsByParameter); } private static Map getActualTypeArgumentsByParameter(Type... types) { // TODO: return Map>, Type> somehow Map actualTypeArgumentsByParameter = new LinkedHashMap(); for (Type type : types) { actualTypeArgumentsByParameter.putAll(getActualTypeArgumentsByParameterInternal(type)); } return normalize(actualTypeArgumentsByParameter); } private static Map getActualTypeArgumentsByParameterInternal(Type type) { // TODO: look deeply within non-parameterized types when visitors implemented if (!(type instanceof ParameterizedType)) { return Collections.emptyMap(); } TypeVariable[] typeParameters = getErasedReferenceType(type).getTypeParameters(); Type[] typeArguments = ((ParameterizedType) type).getActualTypeArguments(); if (typeParameters.length != typeArguments.length) { throw new MalformedParameterizedTypeException(); } Map actualTypeArgumentsByParameter = new LinkedHashMap(); for (int i = 0; i < typeParameters.length; i++) { actualTypeArgumentsByParameter.put(typeParameters[i], typeArguments[i]); } return actualTypeArgumentsByParameter; } private static ParameterizedType parameterizeClass(Class type, Map actualTypeArgumentsByParameter) { return parameterizeClassCapture(type, actualTypeArgumentsByParameter); } private static ParameterizedType parameterizeClassCapture(Class type, Map actualTypeArgumentsByParameter) { // TODO: actualTypeArgumentsByParameter should be Map>, Type> TypeVariable>[] typeParameters = type.getTypeParameters(); Type[] actualTypeArguments = new Type[typeParameters.length]; for (int i = 0; i < typeParameters.length; i++) { TypeVariable> typeParameter = typeParameters[i]; Type actualTypeArgument = actualTypeArgumentsByParameter.get(typeParameter); if (actualTypeArgument == null) { throw new IllegalArgumentException("Missing actual type argument for type parameter: " + typeParameter); } actualTypeArguments[i] = actualTypeArgument; } return Types.parameterizedType(getErasedReferenceType(type), actualTypeArguments); } private static Map normalize(Map map) { // TODO: will this cause an infinite loop with recursive bounds? for (Entry entry : map.entrySet()) { K key = entry.getKey(); V value = entry.getValue(); while (map.containsKey(value)) { value = map.get(value); } map.put(key, value); } return map; } private static Type getResolvedSupertype(Type type, Class rawSupertype) { Class rawCurrentType = TypeUtils.getErasedReferenceType(type); if (rawSupertype.equals(rawCurrentType)) { return type; } // try interfaces for (Type interfaceType : TypeUtils.getResolvedInterfaces(type)) { Type resolvedType = getResolvedSupertype(interfaceType, rawSupertype); if (resolvedType != null) { return resolvedType; } } // try superclass Type supertype = TypeUtils.getResolvedSuperclass(type); if (supertype == null) { return null; } return getResolvedSupertype(supertype, rawSupertype); } } libjtype-java-0.1.3/src/main/java/com/googlecode/jtype/Types.java0000644000175000017500000002302711663225457023462 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import static com.googlecode.jtype.Utils.checkNotNull; import java.lang.reflect.GenericArrayType; import java.lang.reflect.GenericDeclaration; import java.lang.reflect.MalformedParameterizedTypeException; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.lang.reflect.WildcardType; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Factory for creating types. * * @author Mark Hobson * @version $Id: Types.java 110 2011-11-23 17:19:43Z markhobson $ * @see Type */ public final class Types { // constants -------------------------------------------------------------- private static final WildcardType UNBOUNDED_WILDCARD_TYPE = wildcardType(null, null); private static final Pattern ARRAY_PATTERN = Pattern.compile("\\[\\s*\\]$"); private static final Pattern UPPER_BOUND_PATTERN = Pattern.compile("^\\?\\s+extends\\s+"); private static final Pattern LOWER_BOUND_PATTERN = Pattern.compile("^\\?\\s+super\\s+"); // constructors ----------------------------------------------------------- private Types() { throw new AssertionError(); } // public methods --------------------------------------------------------- /** * Creates a type variable for the specified declaration, name and bounds. * * @param * the type of generic declaration that declared the type variable * @param declaration * the generic declaration that declared the type variable * @param name * the name of the type variable * @param bounds * the upper bounds of the type variable * @return the type variable */ public static TypeVariable typeVariable(D declaration, String name, Type... bounds) { return new DefaultTypeVariable(declaration, name, bounds); } /** * Creates a generic array type for the specified component type. * * @param componentType * the component type * @return the generic array type */ public static GenericArrayType genericArrayType(Type componentType) { return new DefaultGenericArrayType(componentType); } /** * Creates a parameterized type for the specified raw type and actual type arguments. * * @param rawType * the raw type * @param actualTypeArguments * the actual type arguments * @return the parameterized type * @throws MalformedParameterizedTypeException * if the raw type is not a parameterized type or the number of actual type arguments differs from those * defined on the raw type */ public static ParameterizedType parameterizedType(Class rawType, Type... actualTypeArguments) { return new DefaultParameterizedType(null, rawType, actualTypeArguments); } /** * Creates a parameterized type for the specified raw type with unbounded wildcard actual type arguments. * * @param rawType * the raw type * @return the parameterized type * @throws MalformedParameterizedTypeException * if the raw type is not a parameterized type */ public static ParameterizedType unboundedParameterizedType(Class rawType) { checkNotNull(rawType, "rawType"); int typeParameterCount = rawType.getTypeParameters().length; Type[] actualTypeArguments = new Type[typeParameterCount]; Arrays.fill(actualTypeArguments, unboundedWildcardType()); return parameterizedType(rawType, actualTypeArguments); } /** * Creates an unbounded wildcard type. * * @return the wildcard type */ public static WildcardType unboundedWildcardType() { return UNBOUNDED_WILDCARD_TYPE; } /** * Creates a wildcard type with the specified upper bound. * * @param upperBound * the upper bound type * @return the wildcard type */ public static WildcardType upperBoundedWildcardType(Type upperBound) { checkNotNull(upperBound, "upperBound"); return wildcardType(new Type[] {upperBound}, null); } /** * Creates a wildcard type with the specified lower bound. * * @param lowerBound * the lower bound type * @return the wildcard type */ public static WildcardType lowerBoundedWildcardType(Type lowerBound) { checkNotNull(lowerBound, "lowerBound"); return wildcardType(null, new Type[] {lowerBound}); } /** * Returns a type that corresponds to the specified string. * * @param typeName * the string to be parsed * @return the type */ public static Type valueOf(String typeName) { return valueOf(typeName, (Set) null); } /** * Returns a type that corresponds to the specified string using the specified import context. * * @param typeName * the string to be parsed * @param imports * the fully qualified class names to use when an unqualified class name is encountered * @return the type * @throws IllegalArgumentException * if the import context contains duplicate entries for an unqualified class name */ public static Type valueOf(String typeName, Set imports) { checkNotNull(typeName, "typeName"); Map importMap = createImportMap(imports); return valueOf(typeName, importMap); } // private methods -------------------------------------------------------- private static WildcardType wildcardType(Type[] upperBounds, Type[] lowerBounds) { return new DefaultWildcardType(upperBounds, lowerBounds); } private static Map createImportMap(Set imports) { if (imports == null) { return Collections.emptyMap(); } Map importMap = new HashMap(); for (String className : imports) { String simpleClassName = ClassUtils.getSimpleClassName(className); if (importMap.containsKey(simpleClassName)) { throw new IllegalArgumentException("Duplicate imports: " + importMap.get(simpleClassName) + " and " + className); } importMap.put(simpleClassName, className); } return importMap; } private static Type valueOf(String typeName, Map imports) { typeName = typeName.trim(); // handle arrays Matcher arrayMatcher = ARRAY_PATTERN.matcher(typeName); if (arrayMatcher.find()) { String componentName = typeName.substring(0, arrayMatcher.start()); Type componentType = valueOf(componentName, imports); return TypeUtils.getArrayType(componentType); } // handle wildcards if (typeName.startsWith("?")) { return parseWildcardType(typeName, imports); } // handle classes int argStart = typeName.indexOf('<'); if (argStart == -1) { return parseClass(typeName, imports); } // handle parameterized types int argEnd = typeName.lastIndexOf('>'); if (argEnd == -1) { throw new IllegalArgumentException("Mismatched type argument delimiters: " + typeName); } String rawTypeName = typeName.substring(0, argStart).trim(); Class rawType = parseClass(rawTypeName, imports); String[] actualTypeArgumentNames = typeName.substring(argStart + 1, argEnd).split(","); Type[] actualTypeArguments = new Type[actualTypeArgumentNames.length]; for (int i = 0; i < actualTypeArgumentNames.length; i++) { actualTypeArguments[i] = valueOf(actualTypeArgumentNames[i], imports); } return parameterizedType(rawType, actualTypeArguments); } private static Class parseClass(String className, Map imports) { Class klass = ClassUtils.valueOf(className); if (klass != null) { return klass; } if (!className.contains(".") && imports.containsKey(className)) { String qualifiedClassName = imports.get(className); klass = ClassUtils.valueOf(qualifiedClassName); if (klass != null) { return klass; } } throw new IllegalArgumentException("Class not found: " + className); } private static WildcardType parseWildcardType(String typeName, Map imports) { Type[] upperBounds; Type[] lowerBounds; Matcher upperBoundMatcher; Matcher lowerBoundMatcher; if ("?".equals(typeName)) { upperBounds = null; lowerBounds = null; } else if ((upperBoundMatcher = UPPER_BOUND_PATTERN.matcher(typeName)).find()) { String upperBoundName = typeName.substring(upperBoundMatcher.end()); Type upperBound = valueOf(upperBoundName, imports); upperBounds = new Type[] {upperBound}; lowerBounds = null; } else if ((lowerBoundMatcher = LOWER_BOUND_PATTERN.matcher(typeName)).find()) { String lowerBoundName = typeName.substring(lowerBoundMatcher.end()); Type lowerBound = valueOf(lowerBoundName, imports); upperBounds = null; lowerBounds = new Type[] {lowerBound}; } else { throw new IllegalArgumentException("Invalid wildcard type: " + typeName); } return wildcardType(upperBounds, lowerBounds); } } libjtype-java-0.1.3/src/main/java/com/googlecode/jtype/DefaultParameterizedType.java0000644000175000017500000000752411663225457027325 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import static com.googlecode.jtype.Utils.checkNotNull; import static com.googlecode.jtype.Utils.nullEquals; import static com.googlecode.jtype.Utils.nullHashCode; import java.io.Serializable; import java.lang.reflect.MalformedParameterizedTypeException; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.util.Arrays; /** * Default implementation of a parameterized type. * * @author Mark Hobson * @version $Id: DefaultParameterizedType.java 110 2011-11-23 17:19:43Z markhobson $ * @see ParameterizedType */ class DefaultParameterizedType implements ParameterizedType, Serializable { // constants -------------------------------------------------------------- private static final long serialVersionUID = 1L; // fields ----------------------------------------------------------------- /** * The type that this type is a member of. * * @serial */ private final Type ownerType; /** * The class or interface that declared this type. * * @serial */ private final Type rawType; /** * The types representing the actual type arguments to this type. * * @serial */ private final Type[] actualTypeArguments; // constructors ----------------------------------------------------------- public DefaultParameterizedType(Type ownerType, Class rawType, Type[] actualTypeArguments) { this.rawType = checkNotNull(rawType, "rawType"); if (actualTypeArguments == null) { actualTypeArguments = new Type[0]; } TypeVariable[] typeParameters = rawType.getTypeParameters(); // disallow unparameterized raw types if (typeParameters.length == 0) { throw new MalformedParameterizedTypeException(); } if (typeParameters.length != actualTypeArguments.length) { throw new MalformedParameterizedTypeException(); } if (ownerType == null) { ownerType = rawType.getDeclaringClass(); } this.ownerType = ownerType; this.actualTypeArguments = actualTypeArguments.clone(); } // ParameterizedType methods ---------------------------------------------- /** * {@inheritDoc} */ public Type getOwnerType() { return ownerType; } /** * {@inheritDoc} */ public Type getRawType() { return rawType; } /** * {@inheritDoc} */ public Type[] getActualTypeArguments() { return actualTypeArguments.clone(); } // Object methods --------------------------------------------------------- /** * {@inheritDoc} */ @Override public int hashCode() { int hashCode = nullHashCode(ownerType); hashCode = (37 * hashCode) + rawType.hashCode(); hashCode = (37 * hashCode) + Arrays.hashCode(actualTypeArguments); return hashCode; } /** * {@inheritDoc} */ @Override public boolean equals(Object object) { if (!(object instanceof ParameterizedType)) { return false; } ParameterizedType type = (ParameterizedType) object; if (type == this) { return true; } return nullEquals(ownerType, type.getOwnerType()) && rawType.equals(type.getRawType()) && Arrays.equals(actualTypeArguments, type.getActualTypeArguments()); } /** * {@inheritDoc} */ @Override public String toString() { return TypeUtils.toString(this); } } libjtype-java-0.1.3/src/main/java/com/googlecode/jtype/DefaultGenericArrayType.java0000644000175000017500000000445211663225457027101 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import static com.googlecode.jtype.Utils.checkNotNull; import java.io.Serializable; import java.lang.reflect.GenericArrayType; import java.lang.reflect.Type; /** * Default implementation of a generic array type. * * @author Mark Hobson * @version $Id: DefaultGenericArrayType.java 110 2011-11-23 17:19:43Z markhobson $ * @see GenericArrayType */ class DefaultGenericArrayType implements GenericArrayType, Serializable { // constants -------------------------------------------------------------- private static final long serialVersionUID = 1L; // fields ----------------------------------------------------------------- /** * The component type of this array. * * @serial */ private final Type componentType; // constructors ----------------------------------------------------------- public DefaultGenericArrayType(Type componentType) { this.componentType = checkNotNull(componentType, "componentType"); } // GenericArrayType methods ----------------------------------------------- /** * {@inheritDoc} */ public Type getGenericComponentType() { return componentType; } // Object methods --------------------------------------------------------- /** * {@inheritDoc} */ @Override public int hashCode() { return componentType.hashCode(); } /** * {@inheritDoc} */ @Override public boolean equals(Object object) { if (!(object instanceof GenericArrayType)) { return false; } GenericArrayType type = (GenericArrayType) object; return componentType.equals(type.getGenericComponentType()); } /** * {@inheritDoc} */ @Override public String toString() { return TypeUtils.toString(this); } } libjtype-java-0.1.3/src/test/0000755000175000017500000000000012142346612014567 5ustar tonytonylibjtype-java-0.1.3/src/test/resources/0000755000175000017500000000000012142346612016601 5ustar tonytonylibjtype-java-0.1.3/src/test/java/0000755000175000017500000000000012142346612015510 5ustar tonytonylibjtype-java-0.1.3/src/test/java/com/0000755000175000017500000000000012142346612016266 5ustar tonytonylibjtype-java-0.1.3/src/test/java/com/googlecode/0000755000175000017500000000000012142346612020375 5ustar tonytonylibjtype-java-0.1.3/src/test/java/com/googlecode/jtype/0000755000175000017500000000000012142346612021530 5ustar tonytonylibjtype-java-0.1.3/src/test/java/com/googlecode/jtype/GenericTest.java0000644000175000017500000002214711663755704024632 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import static com.googlecode.jtype.test.SerializableAssert.assertSerializable; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import java.io.IOException; import java.io.Serializable; import java.lang.reflect.GenericArrayType; import java.lang.reflect.GenericDeclaration; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.List; import java.util.Map; import org.junit.Before; import org.junit.Test; /** * Tests {@code Generic}. * * @author Mark Hobson * @version $Id: GenericTest.java 115 2011-11-25 18:17:40Z markhobson@gmail.com $ * @see Generic */ public class GenericTest implements Serializable { // test is serializable with transient fields for serializableWhenSubclassed // classes ---------------------------------------------------------------- private static class GenericSubclass extends Generic { // simple subclass } // fields ----------------------------------------------------------------- private transient GenericDeclaration declaration; // public methods --------------------------------------------------------- @Before public void setUp() throws NoSuchMethodException { declaration = getClass().getConstructor(); } // test methods ----------------------------------------------------------- @Test(expected = IllegalStateException.class) public void constructorWhenSubclassedTwice() { try { new GenericSubclass>() {/**/}; } catch (IllegalStateException exception) { assertEquals("Message", "Generic must only be subclassed once", exception.getMessage()); throw exception; } } @Test public void constructorWhenSubclassedWithParameterType() { assertEquals(Types.parameterizedType(List.class, String.class), new Generic>() {/**/}.getType()); } @Test(expected = IllegalArgumentException.class) public void constructorWhenSubclassedWithTypeVariable() { try { new Generic() {/**/}; } catch (IllegalArgumentException exception) { assertEquals("Message", "Type variables are not supported: T", exception.getMessage()); throw exception; } } @Test public void constructorWhenSubclassedWithGenericArrayType() { assertEquals(Types.genericArrayType(Types.parameterizedType(List.class, String.class)), new Generic[]>() {/**/}.getType()); } @Test public void getWithClass() { assertEquals(String.class, Generic.get(String.class).getType()); } @Test public void getWithClassIsSerializable() throws IOException, ClassNotFoundException { assertSerializable(Generic.get(String.class)); } @Test public void getWithRawTypeAndActualTypeArguments() { ParameterizedType parameterizedType = Types.parameterizedType(List.class, String.class); assertEquals(parameterizedType, Generic.get(List.class, String.class).getType()); } @Test public void getWithRawTypeAndNullActualTypeArguments() { assertEquals(String.class, Generic.get(String.class, (Type[]) null).getType()); } @Test public void getWithRawTypeAndEmptyActualTypeArguments() { assertEquals(String.class, Generic.get(String.class, new Type[0]).getType()); } @Test public void getWithParameterizedType() { ParameterizedType parameterizedType = Types.parameterizedType(List.class, String.class); assertEquals(parameterizedType, Generic.get(parameterizedType).getType()); } @Test(expected = IllegalArgumentException.class) public void getWithWildcardType() { try { Generic.get(Types.unboundedWildcardType()); } catch (IllegalArgumentException exception) { assertEquals("Message", "Wildcard types are not supported: ?", exception.getMessage()); throw exception; } } @Test(expected = IllegalArgumentException.class) public void getWithTypeVariable() { try { Generic.get(Types.typeVariable(declaration, "T")); } catch (IllegalArgumentException exception) { assertEquals("Message", "Type variables are not supported: T", exception.getMessage()); throw exception; } } @Test public void getWithGenericArrayType() { GenericArrayType genericArrayType = Types.genericArrayType(Types.parameterizedType(List.class, String.class)); assertEquals(genericArrayType, Generic.get(genericArrayType).getType()); } @Test public void getWithObjectClassIsCached() { assertSame(Generic.get(Object.class), Generic.get(Object.class)); } @Test public void getWithBooleanClassIsCached() { assertSame(Generic.get(Boolean.class), Generic.get(Boolean.class)); } @Test public void getWithByteClassIsCached() { assertSame(Generic.get(Byte.class), Generic.get(Byte.class)); } @Test public void getWithCharacterClassIsCached() { assertSame(Generic.get(Character.class), Generic.get(Character.class)); } @Test public void getWithDoubleClassIsCached() { assertSame(Generic.get(Double.class), Generic.get(Double.class)); } @Test public void getWithFloatClassIsCached() { assertSame(Generic.get(Float.class), Generic.get(Float.class)); } @Test public void getWithIntegerClassIsCached() { assertSame(Generic.get(Integer.class), Generic.get(Integer.class)); } @Test public void getWithLongClassIsCached() { assertSame(Generic.get(Long.class), Generic.get(Long.class)); } @Test public void getWithShortClassIsCached() { assertSame(Generic.get(Short.class), Generic.get(Short.class)); } @Test public void getWithStringClassIsCached() { assertSame(Generic.get(String.class), Generic.get(String.class)); } @Test public void valueOfWithClass() { assertEquals(String.class, Generic.valueOf("java.lang.String").getType()); } @Test public void valueOfWithSingleParameterizedType() { assertEquals(Types.parameterizedType(List.class, String.class), Generic.valueOf("java.util.List").getType()); } @Test public void valueOfWithDoubleParameterizedType() { assertEquals(Types.parameterizedType(Map.class, String.class, Integer.class), Generic.valueOf("java.util.Map").getType()); } @Test public void valueOfWithUnboundedWildcardType() { assertEquals(Types.parameterizedType(List.class, Types.unboundedWildcardType()), Generic.valueOf("java.util.List").getType()); } @Test public void hashCodeWithClass() { Generic generic1 = Generic.get(String.class); Generic generic2 = Generic.get(String.class); assertEquals(generic1.hashCode(), generic2.hashCode()); } @Test public void hashCodeWithType() { Generic> generic1 = new Generic>() {/**/}; Generic> generic2 = new Generic>() {/**/}; assertEquals(generic1.hashCode(), generic2.hashCode()); } @Test public void equalsWithClassWhenEqual() { Generic generic1 = Generic.get(String.class); Generic generic2 = Generic.get(String.class); assertTrue(generic1.equals(generic2)); } @Test public void equalsWithClassWhenUnequal() { Generic generic1 = Generic.get(String.class); Generic generic2 = Generic.get(Integer.class); assertFalse(generic1.equals(generic2)); } @Test public void equalsWithGenericWhenEqual() { Generic> generic1 = new Generic>() {/**/}; Generic> generic2 = new Generic>() {/**/}; assertTrue(generic1.equals(generic2)); } @Test public void equalsWithGenericWhenUnequal() { Generic> generic1 = new Generic>() {/**/}; Generic> generic2 = new Generic>() {/**/}; assertFalse(generic1.equals(generic2)); } @Test public void equalsWithDifferentClass() { Generic generic = Generic.get(String.class); assertFalse(generic.equals("not a generic")); } @Test public void toStringWithClass() { assertEquals("java.lang.String", Generic.get(String.class).toString()); } @Test public void toStringWithType() { assertEquals("java.util.List", new Generic>() {/**/}.toString()); } @Test public void toUnqualifiedStringWithClass() { assertEquals("String", Generic.get(String.class).toUnqualifiedString()); } @Test public void toUnqualifiedStringWithType() { assertEquals("List", new Generic>() {/**/}.toUnqualifiedString()); } @Test public void serializableWhenSubclassed() throws IOException, ClassNotFoundException { assertSerializable(new Generic() {/**/}); } } libjtype-java-0.1.3/src/test/java/com/googlecode/jtype/GenericsTest.java0000644000175000017500000001067611465002475025007 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import static org.junit.Assert.assertEquals; import java.util.Collection; import java.util.Comparator; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Map; import java.util.Queue; import java.util.Set; import java.util.SortedMap; import java.util.SortedSet; import org.junit.Test; /** * Tests {@code Generics}. * * @author Mark Hobson * @version $Id: GenericsTest.java 86 2010-11-05 13:23:09Z markhobson $ * @see Generics */ public class GenericsTest { // tests ------------------------------------------------------------------ @Test public void comparable() { assertEquals(new Generic>() {/**/}, Generics.comparable()); } @Test public void comparableWithClass() { assertEquals(new Generic>() {/**/}, Generics.comparable(String.class)); } @Test public void comparator() { assertEquals(new Generic>() {/**/}, Generics.comparator()); } @Test public void comparatorWithClass() { assertEquals(new Generic>() {/**/}, Generics.comparator(String.class)); } @Test public void enumeration() { assertEquals(new Generic>() {/**/}, Generics.enumeration()); } @Test public void enumerationWithClass() { assertEquals(new Generic>() {/**/}, Generics.enumeration(String.class)); } @Test public void iterable() { assertEquals(new Generic>() {/**/}, Generics.iterable()); } @Test public void iterableWithClass() { assertEquals(new Generic>() {/**/}, Generics.iterable(String.class)); } @Test public void iterator() { assertEquals(new Generic>() {/**/}, Generics.iterator()); } @Test public void iteratorWithClass() { assertEquals(new Generic>() {/**/}, Generics.iterator(String.class)); } @Test public void listIterator() { assertEquals(new Generic>() {/**/}, Generics.listIterator()); } @Test public void listIteratorWithClass() { assertEquals(new Generic>() {/**/}, Generics.listIterator(String.class)); } @Test public void collection() { assertEquals(new Generic>() {/**/}, Generics.collection()); } @Test public void collectionWithClass() { assertEquals(new Generic>() {/**/}, Generics.collection(String.class)); } @Test public void set() { assertEquals(new Generic>() {/**/}, Generics.set()); } @Test public void setWithClass() { assertEquals(new Generic>() {/**/}, Generics.set(String.class)); } @Test public void sortedSet() { assertEquals(new Generic>() {/**/}, Generics.sortedSet()); } @Test public void sortedSetWithClass() { assertEquals(new Generic>() {/**/}, Generics.sortedSet(String.class)); } @Test public void list() { assertEquals(new Generic>() {/**/}, Generics.list()); } @Test public void listWithClass() { assertEquals(new Generic>() {/**/}, Generics.list(String.class)); } @Test public void map() { assertEquals(new Generic>() {/**/}, Generics.map()); } @Test public void mapWithClasses() { assertEquals(new Generic>() {/**/}, Generics.map(String.class, Integer.class)); } @Test public void sortedMap() { assertEquals(new Generic>() {/**/}, Generics.sortedMap()); } @Test public void sortedMapWithClasses() { assertEquals(new Generic>() {/**/}, Generics.sortedMap(String.class, Integer.class)); } @Test public void queue() { assertEquals(new Generic>() {/**/}, Generics.queue()); } @Test public void queueWithClass() { assertEquals(new Generic>() {/**/}, Generics.queue(String.class)); } } libjtype-java-0.1.3/src/test/java/com/googlecode/jtype/DefaultWildcardTypeTest.java0000644000175000017500000001272211663755704027154 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import static com.googlecode.jtype.test.SerializableAssert.assertSerializable; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import java.io.IOException; import java.lang.reflect.Type; import java.lang.reflect.WildcardType; import org.junit.Test; /** * Tests {@code DefaultWildcardType}. * * @author Mark Hobson * @version $Id: DefaultWildcardTypeTest.java 115 2011-11-25 18:17:40Z markhobson@gmail.com $ * @see DefaultWildcardType */ public class DefaultWildcardTypeTest { // tests ------------------------------------------------------------------ @Test public void constructorWithNullUpperBounds() { WildcardType wildcardType = new DefaultWildcardType(null, new Type[] {Integer.class}); assertArrayEquals(new Type[] {Object.class}, wildcardType.getUpperBounds()); assertArrayEquals(new Type[] {Integer.class}, wildcardType.getLowerBounds()); } @Test public void constructorWithEmptyUpperBounds() { WildcardType wildcardType = new DefaultWildcardType(new Type[0], new Type[] {Integer.class}); assertArrayEquals(new Type[] {Object.class}, wildcardType.getUpperBounds()); assertArrayEquals(new Type[] {Integer.class}, wildcardType.getLowerBounds()); } @Test public void constructorWithNullLowerBounds() { WildcardType wildcardType = new DefaultWildcardType(new Type[] {Number.class}, null); assertArrayEquals(new Type[] {Number.class}, wildcardType.getUpperBounds()); assertArrayEquals(new Type[0], wildcardType.getLowerBounds()); } @Test(expected = IllegalArgumentException.class) public void constructorWithUpperAndLowerBounds() { try { new DefaultWildcardType(new Type[] {Number.class}, new Type[] {Integer.class}); } catch (IllegalArgumentException exception) { assertEquals("Wildcard type cannot have both upper and lower bounds", exception.getMessage()); throw exception; } } @Test public void hashCodeTest() { WildcardType wildcardType1 = new DefaultWildcardType(null, new Type[] {Integer.class}); WildcardType wildcardType2 = new DefaultWildcardType(null, new Type[] {Integer.class}); assertEquals(wildcardType1.hashCode(), wildcardType2.hashCode()); } @Test public void equalsWhenEqual() { WildcardType wildcardType1 = new DefaultWildcardType(null, new Type[] {Integer.class}); WildcardType wildcardType2 = new DefaultWildcardType(null, new Type[] {Integer.class}); assertEquals(wildcardType1, wildcardType2); } @Test public void equalsWithDifferentClass() { WildcardType wildcardType = new DefaultWildcardType(null, new Type[] {Integer.class}); assertFalse(wildcardType.equals(new Object())); } @Test public void equalsWithDifferentBounds() { WildcardType wildcardType1 = new DefaultWildcardType(new Type[] {Number.class}, null); WildcardType wildcardType2 = new DefaultWildcardType(null, new Type[] {Integer.class}); assertFalse(wildcardType1.equals(wildcardType2)); } @Test public void equalsWithDifferentUpperBounds() { WildcardType wildcardType1 = new DefaultWildcardType(new Type[] {Number.class}, null); WildcardType wildcardType2 = new DefaultWildcardType(new Type[] {Integer.class}, null); assertFalse(wildcardType1.equals(wildcardType2)); } @Test public void equalsWithDifferentLowerBounds() { WildcardType wildcardType1 = new DefaultWildcardType(null, new Type[] {Number.class}); WildcardType wildcardType2 = new DefaultWildcardType(null, new Type[] {Integer.class}); assertFalse(wildcardType1.equals(wildcardType2)); } @Test public void toStringWithNoBounds() { WildcardType wildcardType = new DefaultWildcardType(null, null); assertEquals("?", wildcardType.toString()); } @Test public void toStringWithSingleUpperBound() { WildcardType wildcardType = new DefaultWildcardType(new Type[] {Number.class}, null); assertEquals("? extends java.lang.Number", wildcardType.toString()); } @Test public void toStringWithMultipleUpperBounds() { WildcardType wildcardType = new DefaultWildcardType(new Type[] {Number.class, Comparable.class}, null); assertEquals("? extends java.lang.Number & java.lang.Comparable", wildcardType.toString()); } @Test public void toStringWithSingleLowerBound() { WildcardType wildcardType = new DefaultWildcardType(null, new Type[] {Integer.class}); assertEquals("? super java.lang.Integer", wildcardType.toString()); } @Test public void toStringWithMultipleLowerBound() { WildcardType wildcardType = new DefaultWildcardType(null, new Type[] {Integer.class, Number.class}); assertEquals("? super java.lang.Integer & java.lang.Number", wildcardType.toString()); } @Test public void serializable() throws IOException, ClassNotFoundException { WildcardType type = new DefaultWildcardType(null, new Type[] {Integer.class}); assertSerializable(type); } } libjtype-java-0.1.3/src/test/java/com/googlecode/jtype/TypeUtilsGetResolvedSupertypeTest.java0000644000175000017500000000656011663755704031306 0ustar tonytony/* * Copyright 2011 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import static com.googlecode.jtype.Types.parameterizedType; import static com.googlecode.jtype.Types.typeVariable; import static org.junit.Assert.assertEquals; import java.lang.reflect.Type; import java.util.Set; import com.googlecode.jtype.test.AbstractTypeTest; import org.junit.Test; /** * Tests {@code TypeUtils.getResolvedSupertype}. * * @author Mark Hobson * @version $Id: TypeUtilsGetResolvedSupertypeTest.java 115 2011-11-25 18:17:40Z markhobson@gmail.com $ * @see TypeUtils#getResolvedSupertype(Class, Class) */ public class TypeUtilsGetResolvedSupertypeTest extends AbstractTypeTest { // types ------------------------------------------------------------------ private static class DummyClass { // simple subtype } private static interface IFake { // simple subtype } private static class Fake implements IFake { // simple subtype } private static class ParameterizedTypeFake implements IFake> { // simple subtype } private static class TypeVariableFake implements IFake { // simple subtype } private static class SubFake extends Fake { // simple subtype } private static interface IFake2 extends IFake { // simple subtype } private static class Fake2 implements IFake2 { // simple subtype } // AbstractTypeTest methods ----------------------------------------------- /** * {@inheritDoc} */ @Override protected void addImports(Set> imports) { imports.add(DummyClass.class); imports.add(IFake.class); } // tests ------------------------------------------------------------------ @Test public void getResolvedSupertypeWithClassImplementation() { Type actual = TypeUtils.getResolvedSupertype(Fake.class, IFake.class); assertEquals(type("IFake"), actual); } @Test public void getResolvedSupertypeWithParameterizedTypeImplementation() { Type actual = TypeUtils.getResolvedSupertype(ParameterizedTypeFake.class, IFake.class); assertEquals(type("IFake>"), actual); } @Test public void getResolvedSupertypeWithTypeVariableImplementation() { Type actual = TypeUtils.getResolvedSupertype(TypeVariableFake.class, IFake.class); Type expected = parameterizedType(IFake.class, typeVariable(TypeVariableFake.class, "T")); assertEquals(expected, actual); } @Test public void getResolvedSupertypeWithSuperclassClassImplementation() { Type actual = TypeUtils.getResolvedSupertype(SubFake.class, IFake.class); assertEquals(type("IFake"), actual); } @Test public void getResolvedSupertypeWithSuperinterfaceClassImplementation() { Type actual = TypeUtils.getResolvedSupertype(Fake2.class, IFake.class); assertEquals(type("IFake"), actual); } } libjtype-java-0.1.3/src/test/java/com/googlecode/jtype/DefaultParameterizedTypeTest.java0000644000175000017500000001445111663755704030220 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import static com.googlecode.jtype.test.SerializableAssert.assertSerializable; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import java.io.IOException; import java.lang.reflect.MalformedParameterizedTypeException; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.List; import java.util.Map; import java.util.Set; import org.junit.Test; /** * Tests {@code DefaultParameterizedType}. * * @author Mark Hobson * @version $Id: DefaultParameterizedTypeTest.java 115 2011-11-25 18:17:40Z markhobson@gmail.com $ * @see DefaultParameterizedType */ public class DefaultParameterizedTypeTest { // tests ------------------------------------------------------------------ @Test public void constructor() { ParameterizedType type = new DefaultParameterizedType(Map.class, Map.Entry.class, new Type[] {String.class, Integer.class}); assertEquals(Map.class, type.getOwnerType()); assertEquals(Map.Entry.class, type.getRawType()); assertArrayEquals(new Type[] {String.class, Integer.class}, type.getActualTypeArguments()); } @Test public void constructorWithNullOwnerType() { ParameterizedType type = new DefaultParameterizedType(null, List.class, new Type[] {String.class}); assertNull(type.getOwnerType()); assertEquals(List.class, type.getRawType()); assertArrayEquals(new Type[] {String.class}, type.getActualTypeArguments()); } @Test(expected = NullPointerException.class) public void constructorWithNullRawType() { try { new DefaultParameterizedType(null, null, new Type[] {String.class}); } catch (NullPointerException exception) { assertEquals("rawType cannot be null", exception.getMessage()); throw exception; } } @Test(expected = MalformedParameterizedTypeException.class) public void constructorWithUnparameterizedRawType() { new DefaultParameterizedType(null, String.class, null); } @Test(expected = MalformedParameterizedTypeException.class) public void constructorWithMismatchedActualTypeArguments() { new DefaultParameterizedType(null, List.class, null); } @Test public void hashCodeTest() { ParameterizedType type1 = new DefaultParameterizedType(Map.class, Map.Entry.class, new Type[] {String.class, Integer.class}); ParameterizedType type2 = new DefaultParameterizedType(Map.class, Map.Entry.class, new Type[] {String.class, Integer.class}); assertEquals(type1.hashCode(), type2.hashCode()); } @Test public void hashCodeWithNullOwnerType() { ParameterizedType type1 = new DefaultParameterizedType(null, List.class, new Type[] {String.class}); ParameterizedType type2 = new DefaultParameterizedType(null, List.class, new Type[] {String.class}); assertEquals(type1.hashCode(), type2.hashCode()); } @Test public void equalsWhenEqual() { ParameterizedType type1 = new DefaultParameterizedType(Map.class, Map.Entry.class, new Type[] {String.class, Integer.class}); ParameterizedType type2 = new DefaultParameterizedType(Map.class, Map.Entry.class, new Type[] {String.class, Integer.class}); assertEquals(type1, type2); } @Test public void equalsWithDifferentClass() { ParameterizedType type = new DefaultParameterizedType(null, List.class, new Type[] {String.class}); assertFalse(type.equals(new Object())); } @Test public void equalsWithNullOwnerType() { ParameterizedType type1 = new DefaultParameterizedType(null, List.class, new Type[] {String.class}); ParameterizedType type2 = new DefaultParameterizedType(null, List.class, new Type[] {String.class}); assertEquals(type1, type2); } @Test public void equalsWithUnequalOwnerType() { ParameterizedType type1 = new DefaultParameterizedType(Map.class, Map.Entry.class, new Type[] {String.class, Integer.class}); ParameterizedType type2 = new DefaultParameterizedType(List.class, Map.Entry.class, new Type[] {String.class, Integer.class}); assertFalse(type1.equals(type2)); } @Test public void equalsWithUnequalRawType() { ParameterizedType type1 = new DefaultParameterizedType(null, List.class, new Type[] {String.class}); ParameterizedType type2 = new DefaultParameterizedType(null, Set.class, new Type[] {String.class}); assertFalse(type1.equals(type2)); } @Test public void equalsWithUnequalActualTypeArguments() { ParameterizedType type1 = new DefaultParameterizedType(null, List.class, new Type[] {String.class}); ParameterizedType type2 = new DefaultParameterizedType(null, List.class, new Type[] {Integer.class}); assertFalse(type1.equals(type2)); } @Test public void toStringWithOwnerType() { ParameterizedType type = new DefaultParameterizedType(Map.class, Map.Entry.class, new Type[] {String.class, Integer.class}); assertEquals("java.util.Map.java.util.Map$Entry", type.toString()); } @Test public void toStringWithRawTypeAndActualTypeArgument() { ParameterizedType type = new DefaultParameterizedType(null, List.class, new Type[] {String.class}); assertEquals("java.util.List", type.toString()); } @Test public void toStringWithRawTypeAndActualTypeArguments() { ParameterizedType type = new DefaultParameterizedType(null, Map.class, new Type[] {String.class, Integer.class}); assertEquals("java.util.Map", type.toString()); } @Test public void serializable() throws IOException, ClassNotFoundException { ParameterizedType type = new DefaultParameterizedType(Map.class, Map.Entry.class, new Type[] {String.class, Integer.class}); assertSerializable(type); } } libjtype-java-0.1.3/src/test/java/com/googlecode/jtype/DefaultTypeVariableTest.java0000644000175000017500000002234711663755704027154 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import static com.googlecode.jtype.test.SerializableAssert.assertSerializable; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import java.io.IOException; import java.io.Serializable; import java.lang.reflect.Constructor; import java.lang.reflect.GenericDeclaration; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.util.ArrayList; import java.util.List; import org.junit.Before; import org.junit.Test; /** * Tests {@code DefaultTypeVariable}. * * @author Mark Hobson * @version $Id: DefaultTypeVariableTest.java 115 2011-11-25 18:17:40Z markhobson@gmail.com $ * @see DefaultTypeVariable */ public class DefaultTypeVariableTest { // fields ----------------------------------------------------------------- private Constructor constructor; // public methods --------------------------------------------------------- @Before public void setUp() throws NoSuchMethodException { constructor = getClass().getConstructor(); } // tests ------------------------------------------------------------------ @Test(expected = NullPointerException.class) public void constructorWithNullDeclaration() { try { new DefaultTypeVariable>(null, "T", new Type[] {Number.class}); } catch (NullPointerException exception) { assertEquals("declaration cannot be null", exception.getMessage()); throw exception; } } @Test(expected = NullPointerException.class) public void constructorWithNullName() { try { new DefaultTypeVariable>(constructor, null, new Type[] {Number.class}); } catch (NullPointerException exception) { assertEquals("name cannot be null", exception.getMessage()); throw exception; } } @Test public void constructorWithClassFirstBound() { assertConstructor(constructor, "T", Number.class); } @Test(expected = IllegalArgumentException.class) public void constructorWithArrayClassFirstBound() { assertConstructor(constructor, "T", Number[].class); } @Test public void constructorWithInterfaceFirstBound() { assertConstructor(constructor, "T", Serializable.class); } @Test public void constructorWithTypeVariableFirstBound() { assertConstructor(constructor, "T", Types.typeVariable(constructor, "U")); } @Test public void constructorWithInterfaceParameterizedTypeFirstBound() { assertConstructor(constructor, "T", Types.parameterizedType(List.class, Number.class)); } @Test public void constructorWithClassParameterizedTypeFirstBound() { assertConstructor(constructor, "T", Types.parameterizedType(ArrayList.class, Number.class)); } @Test(expected = IllegalArgumentException.class) public void constructorWithWildcardTypeFirstBound() { assertConstructor(constructor, "T", Types.unboundedWildcardType()); } @Test(expected = IllegalArgumentException.class) public void constructorWithClassSecondBound() { assertConstructor(constructor, "T", Object.class, Number.class); } @Test(expected = IllegalArgumentException.class) public void constructorWithArrayClassSecondBound() { assertConstructor(constructor, "T", Object.class, Number[].class); } @Test public void constructorWithInterfaceSecondBound() { assertConstructor(constructor, "T", Object.class, Serializable.class); } @Test(expected = IllegalArgumentException.class) public void constructorWithTypeVariableSecondBound() { assertConstructor(constructor, "T", Object.class, Types.typeVariable(constructor, "U")); } @Test public void constructorWithInterfaceParameterizedTypeSecondBound() { assertConstructor(constructor, "T", Object.class, Types.parameterizedType(List.class, Number.class)); } @Test(expected = IllegalArgumentException.class) public void constructorWithClassParameterizedTypeSecondBound() { assertConstructor(constructor, "T", Object.class, Types.parameterizedType(ArrayList.class, Number.class)); } @Test(expected = IllegalArgumentException.class) public void constructorWithWildcardTypeSecondBound() { assertConstructor(constructor, "T", Object.class, Types.unboundedWildcardType()); } @Test public void constructorWithNullBounds() { TypeVariable> typeVariable = new DefaultTypeVariable>(constructor, "T", null); assertEquals(constructor, typeVariable.getGenericDeclaration()); assertEquals("T", typeVariable.getName()); assertArrayEquals(new Type[] {Object.class}, typeVariable.getBounds()); } @Test public void constructorWithEmptyBounds() { TypeVariable> typeVariable = new DefaultTypeVariable>(constructor, "T", new Type[0]); assertEquals(constructor, typeVariable.getGenericDeclaration()); assertEquals("T", typeVariable.getName()); assertArrayEquals(new Type[] {Object.class}, typeVariable.getBounds()); } @Test public void hashCodeTest() { TypeVariable> typeVariable1 = new DefaultTypeVariable>(constructor, "T", new Type[] {Number.class, Comparable.class}); TypeVariable> typeVariable2 = new DefaultTypeVariable>(constructor, "T", new Type[] {Number.class, Comparable.class}); assertEquals(typeVariable1.hashCode(), typeVariable2.hashCode()); } @Test public void equalsWhenEqual() { TypeVariable> typeVariable1 = new DefaultTypeVariable>(constructor, "T", new Type[] {Number.class, Comparable.class}); TypeVariable> typeVariable2 = new DefaultTypeVariable>(constructor, "T", new Type[] {Number.class, Comparable.class}); assertEquals(typeVariable1, typeVariable2); } @Test public void equalsWithDifferentClass() { TypeVariable> typeVariable = new DefaultTypeVariable>(constructor, "T", null); assertFalse(typeVariable.equals(new Object())); } @Test public void equalsWithDifferentDeclarations() throws NoSuchMethodException { TypeVariable> typeVariable1 = new DefaultTypeVariable>(constructor, "T", new Type[] {Number.class}); Method method = getClass().getDeclaredMethod("equalsWithDifferentDeclarations"); TypeVariable typeVariable2 = new DefaultTypeVariable(method, "T", new Type[] {Number.class}); assertFalse(typeVariable1.equals(typeVariable2)); } @Test public void equalsWithDifferentNames() { TypeVariable> typeVariable1 = new DefaultTypeVariable>(constructor, "T", new Type[] {Number.class}); TypeVariable> typeVariable2 = new DefaultTypeVariable>(constructor, "U", new Type[] {Number.class}); assertFalse(typeVariable1.equals(typeVariable2)); } @Test public void equalsWithDifferentBounds() { TypeVariable> typeVariable1 = new DefaultTypeVariable>(constructor, "T", new Type[] {Number.class}); TypeVariable> typeVariable2 = new DefaultTypeVariable>(constructor, "T", new Type[] {Integer.class}); assertFalse(typeVariable1.equals(typeVariable2)); } @Test public void toStringWithNoBounds() { TypeVariable> typeVariable = new DefaultTypeVariable>(constructor, "T", null); assertEquals("T", typeVariable.toString()); } @Test public void toStringWithSingleBound() { TypeVariable> typeVariable = new DefaultTypeVariable>(constructor, "T", new Type[] {Number.class}); assertEquals("T extends java.lang.Number", typeVariable.toString()); } @Test public void toStringWithMultipleBounds() { TypeVariable> typeVariable = new DefaultTypeVariable>(constructor, "T", new Type[] {Number.class, Comparable.class}); assertEquals("T extends java.lang.Number & java.lang.Comparable", typeVariable.toString()); } @Test public void serializable() throws IOException, ClassNotFoundException { TypeVariable> type = new DefaultTypeVariable>(getClass(), "T", new Type[] {Number.class, Comparable.class}); assertSerializable(type); } // private methods -------------------------------------------------------- private static void assertConstructor(D declaration, String name, Type... bounds) { TypeVariable typeVariable = new DefaultTypeVariable(declaration, name, bounds); assertEquals("Generic declaration", declaration, typeVariable.getGenericDeclaration()); assertEquals("Name", name, typeVariable.getName()); assertArrayEquals("Bounds", bounds, typeVariable.getBounds()); } } libjtype-java-0.1.3/src/test/java/com/googlecode/jtype/DefaultGenericArrayTypeTest.java0000644000175000017500000000616511663755704030002 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import static com.googlecode.jtype.test.SerializableAssert.assertSerializable; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import java.io.IOException; import java.lang.reflect.GenericArrayType; import org.junit.Test; /** * Tests {@code DefaultGenericArrayType}. * * @author Mark Hobson * @version $Id: DefaultGenericArrayTypeTest.java 115 2011-11-25 18:17:40Z markhobson@gmail.com $ * @see DefaultGenericArrayType */ public class DefaultGenericArrayTypeTest { // tests ------------------------------------------------------------------ @Test(expected = NullPointerException.class) public void constructorWithNullComponentType() { try { new DefaultGenericArrayType(null); } catch (NullPointerException exception) { assertEquals("componentType cannot be null", exception.getMessage()); throw exception; } } @Test public void constructorWithComponentType() { GenericArrayType genericArrayType = new DefaultGenericArrayType(Integer.class); assertEquals(Integer.class, genericArrayType.getGenericComponentType()); } @Test public void hashCodeTest() { GenericArrayType genericArrayType1 = new DefaultGenericArrayType(Integer.class); GenericArrayType genericArrayType2 = new DefaultGenericArrayType(Integer.class); assertEquals(genericArrayType1.hashCode(), genericArrayType2.hashCode()); } @Test public void equalsWhenEqual() { GenericArrayType genericArrayType1 = new DefaultGenericArrayType(Integer.class); GenericArrayType genericArrayType2 = new DefaultGenericArrayType(Integer.class); assertEquals(genericArrayType1, genericArrayType2); } @Test public void equalsWithDifferentClass() { GenericArrayType genericArrayType = new DefaultGenericArrayType(Integer.class); assertFalse(genericArrayType.equals(new Object())); } @Test public void equalsWithDifferentComponentTypes() { GenericArrayType genericArrayType1 = new DefaultGenericArrayType(Integer.class); GenericArrayType genericArrayType2 = new DefaultGenericArrayType(Number.class); assertFalse(genericArrayType1.equals(genericArrayType2)); } @Test public void toStringTest() { GenericArrayType genericArrayType = new DefaultGenericArrayType(Integer.class); assertEquals("java.lang.Integer[]", genericArrayType.toString()); } @Test public void serializable() throws IOException, ClassNotFoundException { GenericArrayType type = new DefaultGenericArrayType(Integer.class); assertSerializable(type); } } libjtype-java-0.1.3/src/test/java/com/googlecode/jtype/TypeUtilsTest.java0000644000175000017500000013032711663755524025220 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.io.Serializable; import java.lang.reflect.GenericDeclaration; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.lang.reflect.WildcardType; import java.util.AbstractList; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.RandomAccess; import java.util.Set; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; /** * Tests {@code TypeUtils}. * * @author Mark Hobson * @version $Id: TypeUtilsTest.java 114 2011-11-25 18:15:48Z markhobson@gmail.com $ * @see TypeUtils */ public class TypeUtilsTest { // TODO: split into smaller test cases // TODO: use local types rather than JDK classes in tests // classes ---------------------------------------------------------------- private static class IntegerArrayList extends ArrayList { // simple subclass to fix generics } private static class IntegerKeyHashMap extends HashMap { // simple subclass to fix generics } private static class StringsByIntegerHashMap extends IntegerKeyHashMap { // simple subclass to fix generics } // fields ----------------------------------------------------------------- private Set valueOfImports; private GenericDeclaration declaration; // public methods --------------------------------------------------------- @Before public void setUp() throws NoSuchMethodException { valueOfImports = Collections.unmodifiableSet(new HashSet(Arrays.asList( Object.class.getName(), Number.class.getName(), Integer.class.getName(), Collection.class.getName(), Set.class.getName(), List.class.getName(), Map.class.getName(), HashSet.class.getName(), LinkedHashSet.class.getName(), AbstractList.class.getName(), ArrayList.class.getName() ))); declaration = getClass().getConstructor(); } // isAssignable tests ----------------------------------------------------- // JLS 4.10.1 Subtyping among Primitive Types @Test public void isAssignableWithPrimitiveDouble() { assertAssignable(Double.TYPE, Double.TYPE); assertAsymmetricallyAssignable(Double.TYPE, Float.TYPE); assertAsymmetricallyAssignable(Double.TYPE, Long.TYPE); assertAsymmetricallyAssignable(Double.TYPE, Integer.TYPE); assertAsymmetricallyAssignable(Double.TYPE, Character.TYPE); assertAsymmetricallyAssignable(Double.TYPE, Short.TYPE); assertAsymmetricallyAssignable(Double.TYPE, Byte.TYPE); } @Test public void isAssignableWithPrimitiveFloat() { assertAssignable(Float.TYPE, Float.TYPE); assertAsymmetricallyAssignable(Float.TYPE, Long.TYPE); assertAsymmetricallyAssignable(Float.TYPE, Integer.TYPE); assertAsymmetricallyAssignable(Float.TYPE, Character.TYPE); assertAsymmetricallyAssignable(Float.TYPE, Short.TYPE); assertAsymmetricallyAssignable(Float.TYPE, Byte.TYPE); } @Test public void isAssignableWithPrimitiveLong() { assertAssignable(Long.TYPE, Long.TYPE); assertAsymmetricallyAssignable(Long.TYPE, Integer.TYPE); assertAsymmetricallyAssignable(Long.TYPE, Character.TYPE); assertAsymmetricallyAssignable(Long.TYPE, Short.TYPE); assertAsymmetricallyAssignable(Long.TYPE, Byte.TYPE); } @Test public void isAssignableWithPrimitiveInt() { assertAssignable(Integer.TYPE, Integer.TYPE); assertAsymmetricallyAssignable(Integer.TYPE, Character.TYPE); assertAsymmetricallyAssignable(Integer.TYPE, Short.TYPE); assertAsymmetricallyAssignable(Integer.TYPE, Byte.TYPE); } @Test public void isAssignableWithPrimitiveShort() { assertAssignable(Short.TYPE, Short.TYPE); assertAsymmetricallyAssignable(Short.TYPE, Byte.TYPE); } // JLS 4.10.2 Subtyping among Class and Interface Types /** * The direct superclasses of C. */ @Test public void isAssignableWithDirectSuperclassFromParameterizedType() { assertAssignable(AbstractList.class, valueOf("ArrayList")); } /** * The direct superinterfaces of C. */ @Test public void isAssignableWithDirectSuperinterfaceFromParameterizedType() { assertAssignable(Collection.class, valueOf("List")); } /** * The type Object, if C is an interface type with no direct superinterfaces. */ @Test public void isAssignableWithObjectFromInterface() { assertAssignable(Object.class, Iterable.class); } /** * The raw type C. */ @Test public void isAssignableWithRawTypeFromParameterizedType() { assertAssignable(List.class, valueOf("List")); } // TODO: finish 4.10.2 // JLS 4.10.3 Subtyping among Array Types /** * If S and T are both reference types, then S[] >1 T[] iff S >1 T. */ @Test public void isAssignableWithArrayClassFromDirectSubtypeArrayClass() { assertAsymmetricallyAssignable(Number[].class, Integer[].class); } @Test public void isAssignableWithArrayClassFromIndirectSubtypeArrayClass() { assertAsymmetricallyAssignable(Object[].class, Integer[].class); } @Test public void isAssignableWithArrayClassFromGenericArrayType() { assertAssignable(Integer[].class, Types.genericArrayType(Integer.class)); } @Test public void isAssignableWithArrayClassFromDirectSubtypeGenericArrayType() { assertAsymmetricallyAssignable(Number[].class, Types.genericArrayType(Integer.class)); } @Test public void isAssignableWithArrayClassFromIndirectSubtypeGenericArrayType() { assertAsymmetricallyAssignable(Object[].class, Types.genericArrayType(Integer.class)); } @Test public void isAssignableWithGenericArrayTypeFromDirectSubtypeGenericArrayType() { assertAsymmetricallyAssignable(Types.genericArrayType(Number.class), Types.genericArrayType(Integer.class)); } @Test public void isAssignableWithGenericArrayTypeFromIndirectSubtypeGenericArrayType() { assertAsymmetricallyAssignable(Types.genericArrayType(Object.class), Types.genericArrayType(Integer.class)); } @Test public void isAssignableWithGenericArrayTypeFromArrayClass() { assertAssignable(Types.genericArrayType(Integer.class), Integer[].class); } @Test public void isAssignableWithGenericArrayTypeFromDirectSubtypeArrayClass() { assertAsymmetricallyAssignable(Types.genericArrayType(Number.class), Integer[].class); } @Test public void isAssignableWithGenericArrayTypeFromIndirectSubtypeArrayClass() { assertAsymmetricallyAssignable(Types.genericArrayType(Object.class), Integer[].class); } /** * Object >1 Object[]. */ @Test public void isAssignableWithObjectFromObjectArrayClass() { assertAsymmetricallyAssignable(Object.class, Object[].class); } @Test public void isAssignableWithObjectFromArrayClass() { assertAsymmetricallyAssignable(Object.class, Integer[].class); } @Test public void isAssignableWithObjectFromObjectGenericArrayType() { assertAsymmetricallyAssignable(Object.class, Types.genericArrayType(Object.class)); } @Test public void isAssignableWithObjectFromGenericArrayType() { assertAsymmetricallyAssignable(Object.class, Types.genericArrayType(Integer.class)); } /** * Cloneable >1 Object[]. */ @Test public void isAssignableWithCloneableFromObjectArrayClass() { assertAsymmetricallyAssignable(Cloneable.class, Object[].class); } @Test public void isAssignableWithCloneableFromArrayClass() { assertAsymmetricallyAssignable(Cloneable.class, Integer[].class); } @Test public void isAssignableWithCloneableFromObjectGenericArrayType() { assertAsymmetricallyAssignable(Cloneable.class, Types.genericArrayType(Object.class)); } @Test public void isAssignableWithCloneableFromGenericArrayType() { assertAsymmetricallyAssignable(Cloneable.class, Types.genericArrayType(Integer.class)); } /** * java.io.Serializable >1 Object[]. */ @Test public void isAssignableWithSerializableFromObjectArrayClass() { assertAsymmetricallyAssignable(Serializable.class, Object[].class); } @Test public void isAssignableWithSerializableFromArrayClass() { assertAsymmetricallyAssignable(Serializable.class, Integer[].class); } @Test public void isAssignableWithSerializableFromObjectGenericArrayType() { assertAsymmetricallyAssignable(Serializable.class, Types.genericArrayType(Object.class)); } @Test public void isAssignableWithSerializableFromGenericArrayType() { assertAsymmetricallyAssignable(Serializable.class, Types.genericArrayType(Integer.class)); } /** * If p is a primitive type, then Object >1 p[]. */ @Test public void isAssignableWithObjectFromPrimitiveArray() { assertAsymmetricallyAssignable(Object.class, int[].class); } /** * If p is a primitive type, then Cloneable >1 p[]. */ @Test public void isAssignableWithCloneableFromPrimitiveArray() { assertAsymmetricallyAssignable(Cloneable.class, int[].class); } /** * If p is a primitive type, then java.io.Serializable >1 p[]. */ @Test public void isAssignableWithSerializableFromPrimitiveArray() { assertAsymmetricallyAssignable(Serializable.class, int[].class); } @Test(expected = NullPointerException.class) public void isAssignableWithNullSupertype() { assertAssignable(null, Integer.class); } @Test(expected = NullPointerException.class) public void isAssignableWithNullType() { assertAssignable(Integer.class, null); } /** * Tests that classes are assignable to their direct superclasses. * * {@literal Number <: Integer} */ @Test public void isAssignableWithClassFromDirectSubclass() { assertAsymmetricallyAssignable(Number.class, Integer.class); } /** * Tests that classes are assignable to their indirect superclasses. * * {@literal Object <: Integer} */ @Test public void isAssignableWithClassFromIndirectSubclass() { assertAsymmetricallyAssignable(Object.class, Integer.class); } /** * Tests that parameterized types are assignable to their raw types. * * {@literal List <: List} */ @Test public void isAssignableWithClassFromParameterizedType() { assertAsymmetricallyAssignable(List.class, valueOf("List")); } /** * Tests that parameterized types are assignable if their raw types are directly assignable. * * {@literal Collection <: List} */ @Test public void isAssignableWithDirectlyAssignableParameterizedTypeRawTypes() { assertAsymmetricallyAssignable(valueOf("Collection"), valueOf("List")); } /** * Tests that parameterized types are assignable if their raw types are indirectly assignable. * * {@literal Collection <: ArrayList} */ @Test public void isAssignableWithIndirectlyAssignableParameterizedTypeRawTypes() { assertAsymmetricallyAssignable(valueOf("Collection"), valueOf("ArrayList")); } /** * Tests that parameterized types are not assignable if their raw types are not assignable. * * {@literal List !<: Set} */ @Test public void isAssignableWithUnassignableParameterizedTypeRawTypes() { assertUnassignable(valueOf("List"), valueOf("Set")); assertUnassignable(valueOf("Set"), valueOf("List")); } /** * Tests that parameterized types are not assignable even if their type arguments are assignable. * * {@literal List !<: List} */ @Test public void isAssignableWithAssignableParameterizedTypeArguments() { assertUnassignable(valueOf("List"), valueOf("List")); assertUnassignable(valueOf("List"), valueOf("List")); } /** * Tests that parameterized type arguments are assignable to wildcard types. * * {@literal List <: List} */ @Test public void isAssignableWithWildcardParameterizedTypeFromParameterizedType() { assertAsymmetricallyAssignable(valueOf("List"), valueOf("List")); } /** * Tests that parameterized type upper bounded wildcard type arguments are assignable to wildcard types. * * {@literal List <: List} */ @Test public void isAssignableWithWildcardParameterizedTypeFromUpperBoundedWildcardParameterizedType() { assertAsymmetricallyAssignable(valueOf("List"), valueOf("List")); } /** * Tests that parameterized type lower bounded wildcard type arguments are assignable to wildcard types. * * {@literal List <: List} */ // TODO: fix @Ignore @Test public void isAssignableWithWildcardParameterizedTypeFromLowerBoundedWildcardParameterizedType() { assertAsymmetricallyAssignable(valueOf("List"), valueOf("List")); } /** * Tests that parameterized type arguments are assignable to wildcard types on their upper bound. * * {@literal List <: List} */ @Test public void isAssignableWithUpperBoundedWildcardParameterizedTypeFromParameterizedType() { assertAsymmetricallyAssignable(valueOf("List"), valueOf("List")); } /** * Tests that parameterized type arguments are assignable to wildcard types within their upper bound. * * {@literal List <: List} */ @Test public void isAssignableWithUpperBoundedWildcardParameterizedTypeFromInBoundsParameterizedType() { assertAsymmetricallyAssignable(valueOf("List"), valueOf("List")); } /** * Tests that parameterized type arguments are not assignable to wildcard types outside of their upper bound. * * {@literal List !<: List} */ @Test public void isAssignableWithUpperBoundedWildcardParameterizedTypeFromOutOfBoundsParameterizedType() { assertUnassignable(valueOf("List"), valueOf("List")); assertUnassignable(valueOf("List"), valueOf("List")); } /** * {@literal List <: List} */ @Test public void isAssignableWithUpperBoundedWildcardParameterizedTypeFromInBoundsUpperBoundedWildcardParameterizedType() { assertAsymmetricallyAssignable(valueOf("List"), valueOf("List")); } /** * Tests that parameterized type arguments are assignable to wildcard types on their lower bound. * * {@literal List <: List} */ @Test public void isAssignableWithLowerBoundedWildcardParameterizedTypeFromParameterizedType() { assertAsymmetricallyAssignable(valueOf("List"), valueOf("List")); } /** * Tests that parameterized type arguments are assignable to wildcard types within their lower bound. * * {@literal List <: List} */ @Test public void isAssignableWithLowerBoundedWildcardParameterizedTypeFromInBoundsParameterizedType() { assertAsymmetricallyAssignable(valueOf("List"), valueOf("List")); } /** * Tests that parameterized type arguments are assignable to wildcard types outside of their lower bound. * * {@literal List !<: List} */ @Test public void isAssignableWithLowerBoundedWildcardParameterizedTypeFromOutOfBoundsParameterizedType() { assertUnassignable(valueOf("List"), valueOf("List")); assertUnassignable(valueOf("List"), valueOf("List")); } /** * {@literal List <: List} */ // TODO: fix @Ignore @Test public void isAssignableWithLowerBoundedWildcardParameterizedTypeFromInBoundsLowerBoundedWildcardParameterizedType() { assertAsymmetricallyAssignable(valueOf("List"), valueOf("List")); } /** * Tests that classes are assignable to parameterized supertypes. * * {@literal List <: IntegerArrayList} */ @Test public void isAssignableWithParameterizedTypeFromClassWithActualTypeArguments() { assertAsymmetricallyAssignable(valueOf("List"), IntegerArrayList.class); } @Test public void isAssignableWithUnboundedWildcardParameterizedTypeFromClass() { assertAsymmetricallyAssignable(valueOf("List"), ArrayList.class); } @Test public void isAssignableWithUnboundedWildcardParameterizedTypeFromClassWithActualTypeArguments() { assertAsymmetricallyAssignable(valueOf("Map"), StringsByIntegerHashMap.class); } /** * Tests that unbounded type variables are assignable to Object. * * {@literal Object <: T} */ @Test public void isAssignableWithObjectFromTypeVariableWithNoBounds() { assertAssignable(Object.class, Types.typeVariable(declaration, "T")); } /** * Tests that type variables with a single bound are assignable to their bound. * * {@literal Number <: T extends Number} */ @Test public void isAssignableWithBoundFromTypeVariableWithBound() { assertAssignable(Number.class, Types.typeVariable(declaration, "T", Number.class)); } /** * Tests that type variables with a single bound are not assignable to subtypes of their bound. * * {@literal Integer !<: T extends Number} */ @Test public void isAssignableWithTypeOutsideOfBoundFromTypeVariableWithBound() { assertUnassignable(Integer.class, Types.typeVariable(declaration, "T", Number.class)); } /** * Tests that type variables with a single bound are assignable to supertypes of their bound. * * {@literal Number <: T extends Integer} */ @Test public void isAssignableWithTypeInsideOfBoundFromTypeVariableWithBound() { assertAssignable(Number.class, Types.typeVariable(declaration, "T", Integer.class)); } /** * Tests that type variables with multiple bounds are assignable to their bounds. * * {@literal Number, Collection <: T extends Number & Collection} */ @Test public void isAssignableWithBoundsFromTypeVariableWithBounds() { TypeVariable type = Types.typeVariable(declaration, "T", Number.class, Collection.class); assertAssignable(Number.class, type); assertAssignable(Collection.class, type); } /** * Tests that type variables with multiple bounds are not assignable to supertypes of their bounds. * * {@literal Integer, Thread !<: T extends Number & Runnable} */ @Test public void isAssignableWithTypeOutsideOfBoundsFromTypeVariableWithBounds() { TypeVariable type = Types.typeVariable(declaration, "T", Number.class, Collection.class); assertUnassignable(Integer.class, type); assertUnassignable(List.class, type); } /** * Tests that type variables with multiple bounds are assignable to subtypes of their bounds. * * {@literal Number, Collection <: T extends Integer & List} */ @Test public void isAssignableWithTypeInsideOfBoundsFromTypeVariableWithBounds() { TypeVariable type = Types.typeVariable(declaration, "T", Integer.class, List.class); assertAssignable(Number.class, type); assertAssignable(Collection.class, type); } /** * Tests that unbounded wildcards are assignable to Object. * * {@literal Object <: ?} */ @Test public void isAssignableWithObjectFromUnboundedWildcardType() { assertAssignable(Object.class, Types.unboundedWildcardType()); } /** * Tests that upper bounded wildcards are assignable to their upper bound. * * {@literal Number <: ? extends Number} */ @Test public void isAssignableWithBoundFromUpperBoundedWildcardType() { assertAssignable(Number.class, Types.upperBoundedWildcardType(Number.class)); } /** * Tests that upper bounded wildcards are assignable to supertypes of their upper bound. * * {@literal Number <: ? extends Integer} */ @Test public void isAssignableWithBoundSupertypeFromUpperBoundedWildcardType() { assertAssignable(Number.class, Types.upperBoundedWildcardType(Integer.class)); } /** * Tests that upper bounded wildcards are not assignable to subtypes of their upper bound. * * {@literal Integer !<: ? extends Number} */ @Test public void isAssignableWithBoundSubtypeFromUpperBoundedWildcardType() { assertUnassignable(Integer.class, Types.upperBoundedWildcardType(Number.class)); } /** * Tests that lower bounded wildcards are assignable to Object. * * {@literal Object <: ? super Number} */ @Test public void isAssignableWithObjectFromLowerBoundedWildcardType() { assertAssignable(Object.class, Types.lowerBoundedWildcardType(Number.class)); } /** * Tests that lower bounded wildcards are not assignable to their lower bound. * * {@literal Number !<: ? super Number} */ @Test public void isAssignableWithBoundFromLowerBoundedWildcardType() { assertUnassignable(Number.class, Types.lowerBoundedWildcardType(Number.class)); } /** * Tests that lower bounded wildcards are not assignable to supertypes of their lower bound. * * {@literal Number !<: ? super Integer} */ @Test public void isAssignableWithBoundSupertypeFromLowerBoundedWildcardType() { assertUnassignable(Number.class, Types.lowerBoundedWildcardType(Integer.class)); } /** * Tests that upper bounded wildcards are not assignable to subtypes of their upper bound. * * {@literal Integer <: ? super Number} */ @Test public void isAssignableWithBoundSubtypeFromLowerBoundedWildcardType() { assertUnassignable(Integer.class, Types.lowerBoundedWildcardType(Number.class)); } // isInstance tests ------------------------------------------------------- @Test(expected = NullPointerException.class) public void isInstanceWithNullType() { TypeUtils.isInstance(null, Integer.valueOf(123)); } @Test public void isInstanceWithClass() { assertTrue(TypeUtils.isInstance(Integer.class, 123)); } @Test public void isInstanceWithClassAndSubclass() { assertTrue(TypeUtils.isInstance(Number.class, 123)); } @Test public void isInstanceWithClassAndSuperclass() { assertFalse(TypeUtils.isInstance(Number.class, new Object())); } @Test public void isInstanceWithClassAndDisjointClass() { assertFalse(TypeUtils.isInstance(Integer.class, 123L)); } @Test public void isInstanceWithClassAndNull() { assertFalse(TypeUtils.isInstance(Integer.class, null)); } @Test public void isInstanceWithClassArray() { assertTrue(TypeUtils.isInstance(Integer[].class, new Integer[0])); } @Test public void isInstanceWithClassArrayAndSubclassArray() { assertTrue(TypeUtils.isInstance(Number[].class, new Integer[0])); } @Test public void isInstanceWithClassArrayAndSuperclassArray() { assertFalse(TypeUtils.isInstance(Number[].class, new Object[0])); } @Test public void isInstanceWithClassArrayAndDisjointClass() { assertFalse(TypeUtils.isInstance(Integer[].class, new Long[0])); } @Test public void isInstanceWithGenericArrayType() { assertTrue(TypeUtils.isInstance(valueOf("List[]"), new List[0])); } @Test public void isInstanceWithGenericArrayTypeAndSubclassComponentType() { assertTrue(TypeUtils.isInstance(valueOf("Collection[]"), new List[0])); } @Test public void isInstanceWithGenericArrayTypeAndSuperclassComponentType() { assertFalse(TypeUtils.isInstance(valueOf("List[]"), new Collection[0])); } @Test public void isInstanceWithGenericArrayTypeWithDisjointComponentType() { assertFalse(TypeUtils.isInstance(valueOf("List[]"), new Set[0])); } @Test public void isInstanceWithParameterizedType() { assertTrue(TypeUtils.isInstance(valueOf("ArrayList"), new ArrayList())); } @Test public void isInstanceWithParameterizedTypeWithSubclassRawType() { assertTrue(TypeUtils.isInstance(valueOf("Collection"), new ArrayList())); } @Test public void isInstanceWithParameterizedTypeWithSuperclassRawType() { assertFalse(TypeUtils.isInstance(valueOf("LinkedHashSet"), new HashSet())); } @Test public void isInstanceWithParameterizedTypeWithDisjointRawType() { assertFalse(TypeUtils.isInstance(valueOf("List"), new HashSet())); } @Test public void isInstanceWithParameterizedTypeWithDisjointTypeArgument() { // support erasure assertTrue(TypeUtils.isInstance(valueOf("ArrayList"), new ArrayList())); } // getErasedType tests ---------------------------------------------------- @Test public void getErasedTypeWithNull() { assertNull(TypeUtils.getErasedType(null)); } @Test public void getErasedTypeWithClass() { assertEquals(Integer.class, TypeUtils.getErasedType(Integer.class)); } @Test public void getErasedTypeWithClassArray() { assertEquals(Integer[].class, TypeUtils.getErasedType(Integer[].class)); } @Test public void getErasedTypeWithTypeVariable() { assertEquals(Number.class, TypeUtils.getErasedType(Types.typeVariable(declaration, "T", Number.class, Comparable.class))); } @Test public void getErasedTypeWithGenericArrayType() { assertEquals(List[].class, TypeUtils.getErasedType(valueOf("List[]"))); } @Test public void getErasedTypeWithParameterizedType() { assertEquals(List.class, TypeUtils.getErasedType(valueOf("List"))); } @Test public void getErasedTypeWithWildcardType() { Type type = Types.unboundedWildcardType(); assertEquals(type, TypeUtils.getErasedType(type)); } // getErasedReferenceType tests ------------------------------------------- @Test public void getErasedReferenceTypeWithNull() { assertNull(TypeUtils.getErasedReferenceType(null)); } @Test public void getErasedReferenceTypeWithClass() { assertEquals(Integer.class, TypeUtils.getErasedReferenceType(Integer.class)); } @Test public void getErasedReferenceTypeWithClassArray() { assertEquals(Integer[].class, TypeUtils.getErasedReferenceType(Integer[].class)); } @Test public void getErasedReferenceTypeWithTypeVariable() { assertEquals(Number.class, TypeUtils.getErasedReferenceType(Types.typeVariable(declaration, "T", Number.class, Comparable.class))); } @Test public void getErasedReferenceTypeWithGenericArrayType() { assertEquals(List[].class, TypeUtils.getErasedReferenceType(valueOf("List[]"))); } @Test public void getErasedReferenceTypeWithParameterizedType() { assertEquals(List.class, TypeUtils.getErasedReferenceType(valueOf("List"))); } @Test(expected = IllegalArgumentException.class) public void getErasedReferenceTypeWithWildcardType() { TypeUtils.getErasedReferenceType(Types.unboundedWildcardType()); } // getRawType tests ------------------------------------------------------- @Deprecated @Test public void getRawTypeWithNull() { assertNull(TypeUtils.getRawType(null)); } @Deprecated @Test public void getRawTypeWithClass() { assertEquals(Integer.class, TypeUtils.getRawType(Integer.class)); } @Deprecated @Test public void getRawTypeWithGenericArrayType() { assertEquals(List[].class, TypeUtils.getRawType(valueOf("List[]"))); } @Deprecated @Test public void getRawTypeWithParameterizedType() { assertEquals(List.class, TypeUtils.getRawType(valueOf("List"))); } // isArray tests ---------------------------------------------------------- @Test public void isArrayWithNull() { assertFalse(TypeUtils.isArray(null)); } @Test public void isArrayWithClass() { assertFalse(TypeUtils.isArray(Integer.class)); } @Test public void isArrayWithClassArray() { assertTrue(TypeUtils.isArray(Integer[].class)); } @Test public void isArrayWithTypeVariable() { assertFalse(TypeUtils.isArray(Types.typeVariable(declaration, "T"))); } @Test public void isArrayWithGenericArrayType() { assertTrue(TypeUtils.isArray(Types.genericArrayType(Integer.class))); } @Test public void isArrayWithParameterizedType() { assertFalse(TypeUtils.isArray(Types.parameterizedType(List.class, Integer.class))); } @Test public void isArrayWithWildcardType() { assertFalse(TypeUtils.isArray(Types.unboundedWildcardType())); } // isPrimitive tests ------------------------------------------------------ @Test public void isPrimitiveWithNull() { assertFalse(TypeUtils.isPrimitive(null)); } @Test public void isPrimitiveWithPrimitives() { assertTrue(TypeUtils.isPrimitive(Boolean.TYPE)); assertTrue(TypeUtils.isPrimitive(Character.TYPE)); assertTrue(TypeUtils.isPrimitive(Byte.TYPE)); assertTrue(TypeUtils.isPrimitive(Short.TYPE)); assertTrue(TypeUtils.isPrimitive(Integer.TYPE)); assertTrue(TypeUtils.isPrimitive(Long.TYPE)); assertTrue(TypeUtils.isPrimitive(Float.TYPE)); assertTrue(TypeUtils.isPrimitive(Double.TYPE)); assertTrue(TypeUtils.isPrimitive(Void.TYPE)); } @Test public void isPrimitiveWithClass() { assertFalse(TypeUtils.isPrimitive(Integer.class)); } @Test public void isPrimitiveWithTypeVariable() { assertFalse(TypeUtils.isPrimitive(Types.typeVariable(declaration, "T"))); } @Test public void isPrimitiveWithGenericArrayType() { assertFalse(TypeUtils.isPrimitive(Types.genericArrayType(Integer.class))); } @Test public void isPrimitiveWithParameterizedType() { assertFalse(TypeUtils.isPrimitive(Types.parameterizedType(List.class, Integer.class))); } @Test public void isPrimitiveWithWildcardType() { assertFalse(TypeUtils.isPrimitive(Types.unboundedWildcardType())); } // getComponentType tests ------------------------------------------------- @Test public void getComponentTypeWithNull() { assertNull(TypeUtils.getComponentType(null)); } @Test public void getComponentTypeWithClass() { assertNull(TypeUtils.getComponentType(Integer.class)); } @Test public void getComponentTypeWithClassArray() { assertEquals(Integer.class, TypeUtils.getComponentType(Integer[].class)); } @Test public void getComponentTypeWithTypeVariable() { assertNull(TypeUtils.getComponentType(Types.typeVariable(declaration, "T"))); } @Test public void getComponentTypeWithGenericArrayType() { assertEquals(Integer.class, TypeUtils.getComponentType(Types.genericArrayType(Integer.class))); } @Test public void getComponentTypeWithParameterizedType() { assertNull(TypeUtils.getComponentType(Types.parameterizedType(List.class, Integer.class))); } @Test public void getComponentTypeWithWildcardType() { assertNull(TypeUtils.getComponentType(Types.unboundedWildcardType())); } // getArrayType tests ----------------------------------------------------- @Test(expected = NullPointerException.class) public void getArrayTypeWithNull() { TypeUtils.getArrayType(null); } @Test public void getArrayTypeWithClass() { assertEquals(Integer[].class, TypeUtils.getArrayType(Integer.class)); } @Test public void getArrayTypeWithTypeVariable() { assertEquals(Types.genericArrayType(Types.typeVariable(declaration, "T")), TypeUtils.getArrayType(Types.typeVariable(declaration, "T"))); } @Test public void getArrayTypeWithGenericArrayType() { assertEquals(Types.genericArrayType(Types.genericArrayType(Integer.class)), TypeUtils.getArrayType(Types.genericArrayType(Integer.class))); } @Test public void getArrayTypeWithParameterizedType() { assertEquals(Types.genericArrayType(Types.parameterizedType(List.class, String.class)), TypeUtils.getArrayType(Types.parameterizedType(List.class, String.class))); } @Test public void getArrayTypeWithWildcardType() { assertEquals(Types.genericArrayType(Types.unboundedWildcardType()), TypeUtils.getArrayType(Types.unboundedWildcardType())); } // isParameterizedType tests ---------------------------------------------- @Test(expected = NullPointerException.class) public void isParameterizedTypeWithNullType() { TypeUtils.isParameterizedType(null, List.class); } @Test(expected = NullPointerException.class) public void isParameterizedTypeWithNullRawType() { TypeUtils.isParameterizedType(List.class, null); } @Test public void isParameterizedTypeWithSameRawType() { assertTrue(TypeUtils.isParameterizedType(valueOf("List"), List.class)); } @Test public void isParameterizedTypeWithSuperRawType() { assertTrue(TypeUtils.isParameterizedType(valueOf("List"), Collection.class)); } @Test public void isParameterizedTypeWithUnassignableRawType() { assertFalse(TypeUtils.isParameterizedType(valueOf("List"), Number.class)); } @Test public void isParameterizedTypeWithClass() { assertFalse(TypeUtils.isParameterizedType(List.class, List.class)); } @Test public void isParameterizedTypeWithParameterizedTypeArgument() { assertTrue(TypeUtils.isParameterizedType(valueOf("List>"), List.class)); } // isSimpleParameterizedType tests ---------------------------------------- @Test(expected = NullPointerException.class) public void isSimpleParameterizedTypeWithNullType() { TypeUtils.isSimpleParameterizedType(null, List.class); } @Test(expected = NullPointerException.class) public void isSimpleParameterizedTypeWithNullRawType() { TypeUtils.isSimpleParameterizedType(List.class, null); } @Test public void isSimpleParameterizedTypeWithSameRawType() { assertTrue(TypeUtils.isSimpleParameterizedType(valueOf("List"), List.class)); } @Test public void isSimpleParameterizedTypeWithSuperRawType() { assertTrue(TypeUtils.isSimpleParameterizedType(valueOf("List"), Collection.class)); } @Test public void isSimpleParameterizedTypeWithUnassignableRawType() { assertFalse(TypeUtils.isSimpleParameterizedType(valueOf("List"), Number.class)); } @Test public void isSimpleParameterizedTypeWithClass() { assertFalse(TypeUtils.isSimpleParameterizedType(List.class, List.class)); } @Test public void isSimpleParameterizedTypeWithParameterizedTypeArgument() { assertTrue(TypeUtils.isSimpleParameterizedType(valueOf("List>"), List.class)); } // getActualTypeArgument tests -------------------------------------------- @Test(expected = NullPointerException.class) public void getActualTypeArgumentWithNull() { TypeUtils.getActualTypeArgument(null); } @Test public void getActualTypeArgument() { assertEquals(Integer.class, TypeUtils.getActualTypeArgument(valueOf("List"))); } @Test(expected = IllegalArgumentException.class) public void getActualTypeArgumentWithUnparameterizedType() { TypeUtils.getActualTypeArgument(Integer.class); } @Test(expected = NullPointerException.class) public void getActualTypeArgumentIndexWithNull() { TypeUtils.getActualTypeArgument(null, 0); } @Test(expected = IllegalArgumentException.class) public void getActualTypeArgumentIndexWithUnparameterizedType() { TypeUtils.getActualTypeArgument(Integer.class, 0); } @Test public void getActualTypeArgumentIndexWithFirstIndex() { assertEquals(Integer.class, TypeUtils.getActualTypeArgument(valueOf("List"), 0)); } @Test public void getActualTypeArgumentIndexWithSecondIndex() { assertEquals(Number.class, TypeUtils.getActualTypeArgument(valueOf("Map"), 1)); } @Test(expected = IllegalArgumentException.class) public void getActualTypeArgumentIndexWithNegativeIndex() { TypeUtils.getActualTypeArgument(valueOf("List"), -1); } @Test(expected = IllegalArgumentException.class) public void getActualTypeArgumentIndexWithOutOfBoundsIndex() { TypeUtils.getActualTypeArgument(valueOf("List"), 1); } // getResolvedSuperclass tests --------------------------------------------- @Test public void getResolvedSuperclassWithPrimitive() { assertNull(TypeUtils.getResolvedSuperclass(int.class)); } @Test public void getResolvedSuperclassWithPrimitiveArray() { assertEquals(Object.class, TypeUtils.getResolvedSuperclass(int[].class)); } @Test public void getResolvedSuperclassWithObject() { assertNull(TypeUtils.getResolvedSuperclass(Object.class)); } @Test public void getResolvedSuperclassWithClass() { assertEquals(Number.class, TypeUtils.getResolvedSuperclass(Integer.class)); } @Test public void getResolvedSuperclassWithArrayClass() { assertEquals(Object.class, TypeUtils.getResolvedSuperclass(Integer[].class)); } @Test public void getResolvedSuperclassWithTypeVariable() { assertNull(TypeUtils.getResolvedSuperclass(Types.typeVariable(declaration, "T"))); } @Test public void getResolvedSuperclassWithGenericArray() { assertEquals(Object.class, TypeUtils.getResolvedSuperclass(Types.genericArrayType(Integer.class))); } @Test public void getResolvedSuperclassWithParameterizedType() { assertEquals(valueOf("AbstractList"), TypeUtils.getResolvedSuperclass(valueOf("ArrayList"))); } @Test(expected = IllegalArgumentException.class) public void getResolvedSuperclassWithWildcardType() { TypeUtils.getResolvedSuperclass(Types.unboundedWildcardType()); } @Test(expected = NullPointerException.class) public void getResolvedSuperclassWithNull() { TypeUtils.getResolvedSuperclass(null); } // getResolvedInterfaces tests -------------------------------------------- @Test public void getResolvedInterfacesWithPrimitive() { assertArrayEquals(new Type[0], TypeUtils.getResolvedInterfaces(int.class)); } @Test public void getResolvedInterfacesWithPrimitiveArray() { Type[] expectedInterfaces = new Type[] { Cloneable.class, Serializable.class }; assertArrayEquals(expectedInterfaces, TypeUtils.getResolvedInterfaces(int[].class)); } @Test public void getResolvedInterfacesWithClass() { Type[] expectedInterfaces = new Type[] { valueOf("java.lang.Comparable") }; assertArrayEquals(expectedInterfaces, TypeUtils.getResolvedInterfaces(Integer.class)); } @Test public void getResolvedInterfacesWithArrayClass() { Type[] expectedInterfaces = new Type[] { Cloneable.class, Serializable.class }; assertArrayEquals(expectedInterfaces, TypeUtils.getResolvedInterfaces(Integer[].class)); } @Test public void getResolvedInterfacesWithTypeVariable() { assertArrayEquals(new Type[0], TypeUtils.getResolvedInterfaces(Types.typeVariable(declaration, "T"))); } @Test public void getResolvedInterfacesWithGenericArray() { Type[] expectedInterfaces = new Type[] { Cloneable.class, Serializable.class }; assertArrayEquals(expectedInterfaces, TypeUtils.getResolvedInterfaces(Types.genericArrayType(Integer.class))); } @Test public void getResolvedInterfacesWithParameterizedType() { Type[] expectedInterfaces = new Type[] { valueOf("List"), RandomAccess.class, Cloneable.class, Serializable.class }; assertArrayEquals(expectedInterfaces, TypeUtils.getResolvedInterfaces(valueOf("ArrayList"))); } @Test(expected = IllegalArgumentException.class) public void getResolvedInterfacesWithWildcardType() { TypeUtils.getResolvedInterfaces(Types.unboundedWildcardType()); } @Test(expected = NullPointerException.class) public void getResolvedInterfacesWithNull() { TypeUtils.getResolvedInterfaces(null); } // toString tests --------------------------------------------------------- @Test public void toStringWithClass() { assertEquals("java.lang.Integer", TypeUtils.toString(Integer.class)); } @Test public void toStringWithArrayClass() { assertEquals("java.lang.Integer[]", TypeUtils.toString(Integer[].class)); } @Test public void toStringWithMultidimensionalArrayClass() { assertEquals("java.lang.Integer[][]", TypeUtils.toString(Integer[][].class)); } @Test public void toStringWithUnboundedTypeVariable() { assertEquals("T", TypeUtils.toString(Types.typeVariable(declaration, "T"))); } @Test public void toStringWithBoundedTypeVariable() { assertEquals("T extends java.lang.Number & java.lang.Comparable", TypeUtils.toString(Types.typeVariable(declaration, "T", Number.class, Comparable.class))); } // TODO: fix @Test @Ignore public void toStringWithRecursiveTypeVariable() { // TODO: create recursive type variable TypeVariable typeVariable = Types.typeVariable(declaration, "T", Types.parameterizedType(Comparable.class, /*typeVariable*/ Object.class)); assertEquals("T extends java.lang.Comparable", TypeUtils.toString(typeVariable)); } @Test public void toStringWithGenericArrayType() { assertEquals("java.lang.Integer[]", TypeUtils.toString(Types.genericArrayType(Integer.class))); } @Test public void toStringWithParameterizedType() { assertEquals("java.util.List", TypeUtils.toString(Types.parameterizedType(List.class, Integer.class))); } @Test public void toStringWithParameterizedTypeByTypeVariable() { assertEquals("java.util.List", TypeUtils.toString(Types.parameterizedType(List.class, Types.typeVariable(declaration, "T")))); } // TODO: fix @Test @Ignore public void toStringWithParameterizedTypeByTypeVariableWithBounds() { assertEquals("java.util.List", TypeUtils.toString(Types.parameterizedType(List.class, Types.typeVariable(declaration, "T", Number.class)))); } @Test public void toStringWithUnboundedWildcardType() { assertEquals("?", TypeUtils.toString(Types.unboundedWildcardType())); } @Test public void toStringWithUpperBoundedWildcardType() { assertEquals("? extends java.lang.Number", TypeUtils.toString(Types.upperBoundedWildcardType(Number.class))); } @Test public void toStringWithLowerBoundedWildcardType() { assertEquals("? super java.lang.Integer", TypeUtils.toString(Types.lowerBoundedWildcardType(Integer.class))); } @Test public void toStringWithNull() { assertEquals("null", TypeUtils.toString(null)); } // toUnqualifiedString tests ---------------------------------------------- @Test public void toUnqualifiedStringWithClass() { assertEquals("Integer", TypeUtils.toUnqualifiedString(Integer.class)); } @Test public void toUnqualifiedStringWithArrayClass() { assertEquals("Integer[]", TypeUtils.toUnqualifiedString(Integer[].class)); } @Test public void toUnqualifiedStringWithUnboundedTypeVariable() { assertEquals("T", TypeUtils.toUnqualifiedString(Types.typeVariable(declaration, "T"))); } @Test public void toUnqualifiedStringWithBoundedTypeVariable() { TypeVariable type = Types.typeVariable(declaration, "T", Number.class, Comparable.class); assertEquals("T extends Number & Comparable", TypeUtils.toUnqualifiedString(type)); } @Test public void toUnqualifiedStringWithGenericArrayType() { assertEquals("Integer[]", TypeUtils.toUnqualifiedString(Types.genericArrayType(Integer.class))); } @Test public void toUnqualifiedStringWithParameterizedType() { ParameterizedType type = Types.parameterizedType(List.class, Integer.class); assertEquals("List", TypeUtils.toUnqualifiedString(type)); } @Test public void toUnqualifiedStringWithUnboundedWildcardType() { assertEquals("?", TypeUtils.toUnqualifiedString(Types.unboundedWildcardType())); } @Test public void toUnqualifiedStringWithUpperBoundedWildcardType() { WildcardType type = Types.upperBoundedWildcardType(Number.class); assertEquals("? extends Number", TypeUtils.toUnqualifiedString(type)); } @Test public void toUnqualifiedStringWithLowerBoundedWildcardType() { WildcardType type = Types.lowerBoundedWildcardType(Integer.class); assertEquals("? super Integer", TypeUtils.toUnqualifiedString(type)); } @Test public void toUnqualifiedStringWithNull() { assertEquals("null", TypeUtils.toUnqualifiedString(null)); } // private methods -------------------------------------------------------- private static void assertAsymmetricallyAssignable(Type supertype, Type type) { assertAssignable(supertype, type); assertUnassignable(type, supertype); } private static void assertAssignable(Type supertype, Type type) { assertTrue("Expected " + type + " assignable to " + supertype, TypeUtils.isAssignable(supertype, type)); } private static void assertUnassignable(Type supertype, Type type) { assertFalse("Expected " + type + " not assignable to " + supertype, TypeUtils.isAssignable(supertype, type)); } private Type valueOf(String typeName) { return Types.valueOf(typeName, valueOfImports); } } libjtype-java-0.1.3/src/test/java/com/googlecode/jtype/TypesTest.java0000644000175000017500000004043511663755704024362 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import static com.googlecode.jtype.test.TypeAssert.assertGenericArrayType; import static com.googlecode.jtype.test.TypeAssert.assertParameterizedType; import static com.googlecode.jtype.test.TypeAssert.assertTypeVariable; import static com.googlecode.jtype.test.TypeAssert.assertWildcardType; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; import java.lang.reflect.Constructor; import java.lang.reflect.GenericArrayType; import java.lang.reflect.MalformedParameterizedTypeException; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.lang.reflect.WildcardType; import java.util.Collections; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.jmock.Expectations; import org.jmock.Mockery; import org.jmock.integration.junit4.JMock; import org.jmock.integration.junit4.JUnit4Mockery; import org.jmock.lib.legacy.ClassImposteriser; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; /** * Tests {@code Types}. * * @author Mark Hobson * @version $Id: TypesTest.java 115 2011-11-25 18:17:40Z markhobson@gmail.com $ * @see Types */ @RunWith(JMock.class) public class TypesTest { // fields ----------------------------------------------------------------- private Mockery context; @SuppressWarnings("unused") private Map stringIntegerMap; private Type stringIntegerMapType; @SuppressWarnings("unused") private List unboundedWildcardList; private Type unboundedWildcardType; @SuppressWarnings("unused") private List numberUpperBoundedWildcardList; private Type numberUpperBoundedWildcardType; @SuppressWarnings("unused") private List integerLowerBoundedWildcardList; private Type integerLowerBoundedWildcardType; // public methods --------------------------------------------------------- @Before public void setUp() throws NoSuchFieldException { context = new JUnit4Mockery(); context.setImposteriser(ClassImposteriser.INSTANCE); stringIntegerMapType = getFieldType("stringIntegerMap"); unboundedWildcardType = getFieldActualTypeArgument("unboundedWildcardList"); numberUpperBoundedWildcardType = getFieldActualTypeArgument("numberUpperBoundedWildcardList"); integerLowerBoundedWildcardType = getFieldActualTypeArgument("integerLowerBoundedWildcardList"); } // typeVariable tests ----------------------------------------------------- @Test public void typeVariableWithNoBounds() throws NoSuchMethodException { Constructor constructor = TypesTest.class.getConstructor(); TypeVariable> typeVariable = Types.typeVariable(constructor, "T"); assertTypeVariable(constructor, "T", new Type[] {Object.class}, typeVariable); } @Test public void typeVariableWithBounds() throws NoSuchMethodException { Constructor constructor = TypesTest.class.getConstructor(); TypeVariable> typeVariable = Types.typeVariable(constructor, "T", Number.class, Comparable.class); assertTypeVariable(constructor, "T", new Type[] {Number.class, Comparable.class}, typeVariable); } // genericArrayType tests ------------------------------------------------- @Test public void genericArrayType() { GenericArrayType type = Types.genericArrayType(Integer.class); assertGenericArrayType(Integer.class, type); } @Test(expected = NullPointerException.class) public void genericArrayTypeWithNull() { try { Types.genericArrayType(null); } catch (NullPointerException exception) { assertEquals("componentType cannot be null", exception.getMessage()); throw exception; } } // parameterizedType tests ------------------------------------------------ @Test public void parameterizedType() { ParameterizedType type = Types.parameterizedType(Map.class, new Type[] {String.class, Integer.class}); assertParameterizedType(Map.class, new Type[] {String.class, Integer.class}, type); assertEquals(type, stringIntegerMapType); assertEquals(stringIntegerMapType, type); } @Test(expected = NullPointerException.class) public void parameterizedTypeWithNullRawType() { try { Types.parameterizedType(null, String.class, Integer.class); } catch (NullPointerException exception) { assertEquals("rawType cannot be null", exception.getMessage()); throw exception; } } @Test(expected = MalformedParameterizedTypeException.class) public void parameterizedTypeWithUnparameterizedRawType() { Types.parameterizedType(Integer.class); } @Test(expected = MalformedParameterizedTypeException.class) public void parameterizedTypeWithMismatchedActualTypeArguments() { Types.parameterizedType(Map.class, String.class); } @Test public void unboundedParameterizedType() { ParameterizedType type = Types.unboundedParameterizedType(Map.class); Type[] expectedActualTypeArguments = new Type[] {Types.unboundedWildcardType(), Types.unboundedWildcardType()}; assertParameterizedType(Map.class, expectedActualTypeArguments, type); } @Test(expected = NullPointerException.class) public void unboundedParameterizedTypeWithNullRawType() { try { Types.unboundedParameterizedType(null); } catch (NullPointerException exception) { assertEquals("rawType cannot be null", exception.getMessage()); throw exception; } } @Test(expected = MalformedParameterizedTypeException.class) public void unboundedParameterizedTypeWithUnparameterizedRawType() { Types.unboundedParameterizedType(Integer.class); } // unboundedWildcardType tests -------------------------------------------- @Test public void unboundedWildcardType() { WildcardType type = Types.unboundedWildcardType(); assertWildcardType(new Type[] {Object.class}, new Type[0], type); assertEquals(type, unboundedWildcardType); assertEquals(unboundedWildcardType, type); } @Test public void unboundedWildcardTypeIsCached() { assertSame(Types.unboundedWildcardType(), Types.unboundedWildcardType()); } // upperBoundedWildcardType tests ----------------------------------------- @Test public void upperBoundedWildcardType() { WildcardType type = Types.upperBoundedWildcardType(Number.class); assertWildcardType(new Type[] {Number.class}, new Type[0], type); assertEquals(type, numberUpperBoundedWildcardType); assertEquals(numberUpperBoundedWildcardType, type); } @Test(expected = NullPointerException.class) public void upperBoundedWildcardTypeWithNullUpperBound() { try { Types.upperBoundedWildcardType(null); } catch (NullPointerException exception) { assertEquals("upperBound cannot be null", exception.getMessage()); throw exception; } } // lowerBoundedWildcardType tests ----------------------------------------- @Test public void lowerBoundedWildcardType() { WildcardType type = Types.lowerBoundedWildcardType(Integer.class); assertWildcardType(new Type[] {Object.class}, new Type[] {Integer.class}, type); assertEquals(type, integerLowerBoundedWildcardType); assertEquals(integerLowerBoundedWildcardType, type); } @Test(expected = NullPointerException.class) public void lowerBoundedWildcardTypeWithNullLowerBound() { try { Types.lowerBoundedWildcardType(null); } catch (NullPointerException exception) { assertEquals("lowerBound cannot be null", exception.getMessage()); throw exception; } } // valueOf tests ---------------------------------------------------------- @Test public void valueOfWithBooleanPrimitive() { assertEquals(Boolean.TYPE, Types.valueOf("boolean")); } @Test public void valueOfWithBytePrimitive() { assertEquals(Byte.TYPE, Types.valueOf("byte")); } @Test public void valueOfWithCharPrimitive() { assertEquals(Character.TYPE, Types.valueOf("char")); } @Test public void valueOfWithDoublePrimitive() { assertEquals(Double.TYPE, Types.valueOf("double")); } @Test public void valueOfWithFloatPrimitive() { assertEquals(Float.TYPE, Types.valueOf("float")); } @Test public void valueOfWithIntPrimitive() { assertEquals(Integer.TYPE, Types.valueOf("int")); } @Test public void valueOfWithLongPrimitive() { assertEquals(Long.TYPE, Types.valueOf("long")); } @Test public void valueOfWithShortPrimitive() { assertEquals(Short.TYPE, Types.valueOf("short")); } @Test public void valueOfWithClass() { assertEquals(Integer.class, Types.valueOf("java.lang.Integer")); } @Test public void valueOfWithClassAndWhitespace() { assertEquals(Integer.class, Types.valueOf(" java.lang.Integer ")); } @Test public void valueOfWithClassAndCustomClassLoader() throws ClassNotFoundException { ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); final ClassLoader newClassLoader = context.mock(ClassLoader.class); context.checking(new Expectations() {{ one(newClassLoader).loadClass("java.lang.Integer"); will(returnValue(Integer.class)); }}); try { Thread.currentThread().setContextClassLoader(newClassLoader); assertEquals(Integer.class, Types.valueOf("java.lang.Integer")); } finally { Thread.currentThread().setContextClassLoader(oldClassLoader); } } @Test public void valueOfWithArray() { assertEquals(Integer[].class, Types.valueOf("java.lang.Integer[]")); } @Test public void valueOfWithArrayAndWhitespace() { assertEquals(Integer[].class, Types.valueOf(" java.lang.Integer [ ] ")); } @Test public void valueOfWithGenericArrayType() { assertEquals(Types.genericArrayType(Types.parameterizedType(List.class, Integer.class)), Types.valueOf("java.util.List[]")); } @Test public void valueOfWithSingleArgumentParameterizedType() { assertEquals(Types.parameterizedType(List.class, Integer.class), Types.valueOf("java.util.List")); } @Test public void valueOfWithSingleArgumentParameterizedTypeAndWhitespace() { assertEquals(Types.parameterizedType(List.class, Integer.class), Types.valueOf(" java.util.List < java.lang.Integer > ")); } @Test public void valueOfWithSingleUnboundedWildcardParameterizedType() { assertEquals(Types.parameterizedType(List.class, Types.unboundedWildcardType()), Types.valueOf("java.util.List")); } @Test public void valueOfWithMultipleArgumentParameterizedType() { assertEquals(Types.parameterizedType(Map.class, String.class, Integer.class), Types.valueOf("java.util.Map")); } @Test public void valueOfWithMultipleArgumentParameterizedTypeAndNoWhitespace() { assertEquals(Types.parameterizedType(Map.class, String.class, Integer.class), Types.valueOf("java.util.Map")); } @Test public void valueOfWithMultipleArgumentParameterizedTypeAndWhitespace() { assertEquals(Types.parameterizedType(Map.class, String.class, Integer.class), Types.valueOf(" java.util.Map < java.lang.String , java.lang.Integer > ")); } @Test public void valueOfWithMultipleUnboundedWildcardParameterizedType() { assertEquals(Types.parameterizedType(Map.class, Types.unboundedWildcardType(), Types.unboundedWildcardType()), Types.valueOf("java.util.Map")); } @Test(expected = MalformedParameterizedTypeException.class) public void valueOfWithMismatchedArgumentParameterizedType() { Types.valueOf("java.util.Map"); } @Test public void valueOfWithMultiParameterizedType() { assertEquals(Types.parameterizedType(List.class, Types.parameterizedType(List.class, Integer.class)), Types.valueOf("java.util.List>")); } @Test public void valueOfWithMultiAndMultipleArgumentParameterizedType() { Type expected = Types.parameterizedType(Map.class, Types.parameterizedType(List.class, String.class), Types.parameterizedType(List.class, Integer.class)); assertEquals(expected, Types.valueOf("java.util.Map, java.util.List>")); } @Test public void valueOfWithUnboundedWildcardType() { assertEquals(Types.unboundedWildcardType(), Types.valueOf("?")); } @Test public void valueOfWithUnboundedWildcardTypeAndWhitespace() { assertEquals(Types.unboundedWildcardType(), Types.valueOf(" ? ")); } @Test public void valueOfWithUpperBoundedWildcardType() { assertEquals(Types.upperBoundedWildcardType(Number.class), Types.valueOf("? extends java.lang.Number")); } @Test public void valueOfWithUpperBoundedWildcardTypeAndWhitespace() { assertEquals(Types.upperBoundedWildcardType(Number.class), Types.valueOf(" ? extends java.lang.Number ")); } @Test public void valueOfWithLowerBoundedWildcardType() { assertEquals(Types.lowerBoundedWildcardType(Integer.class), Types.valueOf("? super java.lang.Integer")); } @Test public void valueOfWithLowerBoundedWildcardTypeAndWhitespace() { assertEquals(Types.lowerBoundedWildcardType(Integer.class), Types.valueOf(" ? super java.lang.Integer ")); } @Test(expected = NullPointerException.class) public void valueOfWithNullTypeName() { Types.valueOf(null); } @Test public void valueOfWithClassAndImportContext() { Set importContext = Collections.singleton(Integer.class.getName()); assertEquals(Integer.class, Types.valueOf("Integer", importContext)); } @Test(expected = IllegalArgumentException.class) public void valueOfWithClassAndMissingImportContext() { try { Types.valueOf("Integer"); } catch (IllegalArgumentException exception) { assertEquals("Class not found: Integer", exception.getMessage()); throw exception; } } @Test(expected = IllegalArgumentException.class) public void valueOfWithClassAndInvalidImportContext() { Set importContext = new LinkedHashSet(); importContext.add(Integer.class.getName()); importContext.add("another.Integer"); try { Types.valueOf("Integer", importContext); } catch (IllegalArgumentException exception) { assertEquals("Duplicate imports: java.lang.Integer and another.Integer", exception.getMessage()); throw exception; } } // TODO: fix @Ignore @Test public void valueOfWithMemberClassAndImportContext() { Set importContext = Collections.singleton(Map.class.getName()); assertEquals(Map.Entry.class, Types.valueOf("Map.Entry", importContext)); } @Test public void valueOfWithMemberClassAndMemberImportContext() { Set importContext = Collections.singleton(Map.Entry.class.getName()); assertEquals(Map.Entry.class, Types.valueOf("Entry", importContext)); } @Test public void valueOfWithParameterizedTypeAndImportContext() { Set importContext = new HashSet(); importContext.add(List.class.getName()); importContext.add(Integer.class.getName()); assertEquals(Types.parameterizedType(List.class, Integer.class), Types.valueOf("List", importContext)); } @Test public void valueOfWithWilcardTypeAndImportContext() { Set importContext = Collections.singleton(Number.class.getName()); assertEquals(Types.upperBoundedWildcardType(Number.class), Types.valueOf("? extends Number", importContext)); } @Test public void valueOfWithClassAndNullImportContext() { assertEquals(Integer.class, Types.valueOf("java.lang.Integer", null)); } // private methods -------------------------------------------------------- private Type getFieldType(String name) throws NoSuchFieldException { return getClass().getDeclaredField(name).getGenericType(); } private Type getFieldActualTypeArgument(String name) throws NoSuchFieldException { return ((ParameterizedType) getFieldType(name)).getActualTypeArguments()[0]; } } libjtype-java-0.1.3/src/test/java/com/googlecode/jtype/ClassUtilsTest.java0000644000175000017500000000617411463521342025331 0ustar tonytony/* * Copyright 2009 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype; import static org.junit.Assert.assertEquals; import org.junit.Test; /** * Tests {@code ClassUtils}. * * @author Mark Hobson * @version $Id: ClassUtilsTest.java 82 2010-11-01 11:22:10Z markhobson $ * @see ClassUtils */ public class ClassUtilsTest { // test methods ----------------------------------------------------------- @Test public void getSimpleClassNameWithClass() { assertEquals("A", ClassUtils.getSimpleClassName("A")); } @Test public void getSimpleClassNameWithClassInPackage() { assertEquals("B", ClassUtils.getSimpleClassName("a.B")); } @Test public void getSimpleClassNameWithClassInDeepPackage() { assertEquals("C", ClassUtils.getSimpleClassName("a.b.C")); } @Test public void getSimpleClassNameWithMemberClass() { assertEquals("B", ClassUtils.getSimpleClassName("A$B")); } @Test public void getSimpleClassNameWithMemberClassInPackage() { assertEquals("C", ClassUtils.getSimpleClassName("a.B$C")); } @Test public void getSimpleClassNameWithMemberClassInDeepPackage() { assertEquals("D", ClassUtils.getSimpleClassName("a.b.C$D")); } @Test public void getSimpleClassNameWithDeepMemberClass() { assertEquals("C", ClassUtils.getSimpleClassName("A$B$C")); } @Test public void getSimpleClassNameWithDeepMemberClassInPackage() { assertEquals("D", ClassUtils.getSimpleClassName("a.B$C$D")); } @Test public void getSimpleClassNameWithDeepMemberClassInDeepPackage() { assertEquals("E", ClassUtils.getSimpleClassName("a.b.C$D$E")); } @Test public void valueOfWithBooleanPrimitive() { assertEquals(Boolean.TYPE, ClassUtils.valueOf("boolean")); } @Test public void valueOfWithBytePrimitive() { assertEquals(Byte.TYPE, ClassUtils.valueOf("byte")); } @Test public void valueOfWithCharPrimitive() { assertEquals(Character.TYPE, ClassUtils.valueOf("char")); } @Test public void valueOfWithDoublePrimitive() { assertEquals(Double.TYPE, ClassUtils.valueOf("double")); } @Test public void valueOfWithFloatPrimitive() { assertEquals(Float.TYPE, ClassUtils.valueOf("float")); } @Test public void valueOfWithIntPrimitive() { assertEquals(Integer.TYPE, ClassUtils.valueOf("int")); } @Test public void valueOfWithLongPrimitive() { assertEquals(Long.TYPE, ClassUtils.valueOf("long")); } @Test public void valueOfWithShortPrimitive() { assertEquals(Short.TYPE, ClassUtils.valueOf("short")); } @Test public void valueOfWithClass() { assertEquals(Integer.class, ClassUtils.valueOf("java.lang.Integer")); } } libjtype-java-0.1.3/src/test/java/com/googlecode/jtype/test/0000755000175000017500000000000012142346612022507 5ustar tonytonylibjtype-java-0.1.3/src/test/java/com/googlecode/jtype/test/AbstractTypeTest.java0000644000175000017500000000406211663755704026636 0ustar tonytony/* * Copyright 2011 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype.test; import java.lang.reflect.Type; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.Set; import com.googlecode.jtype.Types; import org.junit.Before; /** * Provides support for testing with types. * * @author Mark Hobson * @version $Id: AbstractTypeTest.java 115 2011-11-25 18:17:40Z markhobson@gmail.com $ */ public abstract class AbstractTypeTest { // fields ----------------------------------------------------------------- private Set imports; // public methods --------------------------------------------------------- @Before public final void setUpAbstractTypeTest() { imports = Collections.unmodifiableSet(new HashSet(createImports())); } // protected methods ------------------------------------------------------ protected void addImports(Set> imports) { // no-op } protected final Type type(String typeName) { return Types.valueOf(typeName, imports); } // private methods -------------------------------------------------------- private Set createImports() { Set> classImports = new HashSet>(); addImports(classImports); return toClassNames(classImports); } private static Set toClassNames(Collection> classes) { Set names = new HashSet(); for (Class klass : classes) { names.add(klass.getName()); } return names; } } libjtype-java-0.1.3/src/test/java/com/googlecode/jtype/test/TypeAssert.java0000644000175000017500000000601011663755704025467 0ustar tonytony/* * Copyright 2011 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype.test; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import java.lang.reflect.GenericArrayType; import java.lang.reflect.GenericDeclaration; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.lang.reflect.WildcardType; /** * Provides custom assertions for testing types. * * @author Mark Hobson * @version $Id: TypeAssert.java 115 2011-11-25 18:17:40Z markhobson@gmail.com $ */ public final class TypeAssert { // constructors ----------------------------------------------------------- private TypeAssert() { throw new AssertionError(); } // public methods --------------------------------------------------------- public static void assertTypeVariable(D expectedGenericDeclaration, String expectedName, Type[] expectedBounds, TypeVariable actual) { assertNotNull(actual); assertEquals("Generic declaration", expectedGenericDeclaration, actual.getGenericDeclaration()); assertEquals("Name", expectedName, actual.getName()); assertArrayEquals("Bounds", expectedBounds, actual.getBounds()); } public static void assertGenericArrayType(Type expectedComponentType, GenericArrayType actual) { assertNotNull(actual); assertEquals("Component type", expectedComponentType, actual.getGenericComponentType()); } public static void assertParameterizedType(Class expectedRawType, Type[] expectedActualTypeArguments, ParameterizedType actual) { assertParameterizedType(null, expectedRawType, expectedActualTypeArguments, actual); } public static void assertParameterizedType(Type expectedOwnerType, Class expectedRawType, Type[] expectedActualTypeArguments, ParameterizedType actual) { assertNotNull(actual); assertEquals("Owner type", expectedOwnerType, actual.getOwnerType()); assertEquals("Raw type", expectedRawType, actual.getRawType()); assertArrayEquals("Actual type arguments", expectedActualTypeArguments, actual.getActualTypeArguments()); } public static void assertWildcardType(Type[] expectedUpperBounds, Type[] expectedLowerBounds, WildcardType actual) { assertNotNull(actual); assertArrayEquals("Upper bounds", expectedUpperBounds, actual.getUpperBounds()); assertArrayEquals("Lower bounds", expectedLowerBounds, actual.getLowerBounds()); } } libjtype-java-0.1.3/src/test/java/com/googlecode/jtype/test/SerializableAssert.java0000644000175000017500000000435711663755704027170 0ustar tonytony/* * Copyright 2010 IIZUKA Software Technologies Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.jtype.test; import static org.junit.Assert.assertEquals; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; /** * Provides custom assertions for testing serializable objects. * * @author Mark Hobson * @version $Id: SerializableAssert.java 115 2011-11-25 18:17:40Z markhobson@gmail.com $ */ public final class SerializableAssert { // constructors ----------------------------------------------------------- private SerializableAssert() { throw new AssertionError(); } // public methods --------------------------------------------------------- public static void assertSerializable(Object object) throws IOException, ClassNotFoundException { byte[] bytes = serialize(object); Object actual = deserialize(bytes); assertEquals("Serialized object", object, actual); } // private methods -------------------------------------------------------- private static byte[] serialize(Object object) throws IOException { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); ObjectOutputStream objectOut = new ObjectOutputStream(byteOut); try { objectOut.writeObject(object); } finally { objectOut.close(); } return byteOut.toByteArray(); } private static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException { ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes); ObjectInputStream objectIn = new ObjectInputStream(byteIn); try { return objectIn.readObject(); } finally { objectIn.close(); } } } libjtype-java-0.1.3/pom.xml0000644000175000017500000001350511757125403014346 0ustar tonytony 4.0.0 com.googlecode.jtype jtype jar 0.1.3 JType Library for working with the Java 5 type system http://jtype.googlecode.com/ 2008 2.2.1 The Apache Software License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt repo scm:svn:http://jtype.googlecode.com/svn/tags/0.1.3 scm:svn:https://jtype.googlecode.com/svn/tags/0.1.3 http://code.google.com/p/jtype/source/browse/tags/0.1.3 Google Code http://code.google.com/p/jtype/issues/list markhobson Mark Hobson markhobson@gmail.com Project Lead 0 org.jvnet.wagon-svn wagon-svn 1.9 org.apache.maven.plugins maven-clean-plugin 2.3 org.apache.maven.plugins maven-compiler-plugin 2.0.2 UTF-8 1.5 1.5 org.apache.maven.plugins maven-deploy-plugin 2.4 org.apache.maven.plugins maven-gpg-plugin 1.0-alpha-4 org.apache.maven.plugins maven-install-plugin 2.3 org.apache.maven.plugins maven-jar-plugin 2.2 org.apache.maven.plugins maven-javadoc-plugin 2.6.1 org.apache.maven.plugins maven-release-plugin 2.0-beta-9 org.apache.maven.plugins maven-resources-plugin 2.4.1 org.apache.maven.plugins maven-site-plugin 2.0.1 org.apache.maven.plugins maven-surefire-plugin 2.4.3 UTF-8 release performRelease org.apache.maven.plugins maven-gpg-plugin sign sign junit junit 4.5 test org.jmock jmock-junit4 2.5.1 test org.jmock jmock-legacy 2.5.1 test org.apache.maven.plugins maven-project-info-reports-plugin 2.1.2 org.apache.maven.plugins maven-javadoc-plugin 2.6.1 javadoc http://java.sun.com/j2se/1.5.0/docs/api sonatype-nexus-staging Sonatype Nexus Staging Repository https://oss.sonatype.org/service/local/staging/deploy/maven2/ sonatype-nexus-snapshots Sonatype Nexus Snapshot Repository https://oss.sonatype.org/content/repositories/snapshots/ jtype-site JType Maven Site svn:https://jtype.googlecode.com/svn/site/