PageRenderTime 36ms CodeModel.GetById 26ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/sitebricks-converter/src/main/java/com/google/sitebricks/conversion/generics/GenericArrayTypeImpl.java

http://github.com/dhanji/sitebricks
Java | 64 lines | 49 code | 9 blank | 6 comment | 2 complexity | d78cd8c58ba12375a1c82c8bf0150fc6 MD5 | raw file
 1/*
 2 * Copied from Gentyref project http://code.google.com/p/gentyref/
 3 * Code was reformatted and moved to fit package structure
 4 */
 5package com.google.sitebricks.conversion.generics;
 6
 7import java.lang.reflect.Array;
 8import java.lang.reflect.GenericArrayType;
 9import java.lang.reflect.Type;
10
11class GenericArrayTypeImpl implements GenericArrayType
12{
13	private Type componentType;
14
15	static Class<?> createArrayType(Class<?> componentType)
16	{
17		// there's no (clean) other way to create a array class, then create an
18		// instance of it
19		return Array.newInstance(componentType, 0).getClass();
20	}
21
22	static Type createArrayType(Type componentType)
23	{
24		if (componentType instanceof Class<?>)
25		{
26			return createArrayType((Class<?>) componentType);
27		}
28		else
29		{
30			return new GenericArrayTypeImpl(componentType);
31		}
32	}
33
34	private GenericArrayTypeImpl(Type componentType)
35	{
36		super();
37		this.componentType = componentType;
38	}
39
40	public Type getGenericComponentType()
41	{
42		return componentType;
43	}
44
45	@Override
46	public boolean equals(Object obj)
47	{
48		if (!(obj instanceof GenericArrayType))
49			return false;
50		return componentType.equals(((GenericArrayType) obj).getGenericComponentType());
51	}
52
53	@Override
54	public int hashCode()
55	{
56		return componentType.hashCode() * 7;
57	}
58
59	@Override
60	public String toString()
61	{
62		return componentType + "[]";
63	}
64}