01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.object.dna.impl;
06:
07: import com.tc.object.loaders.ClassProvider;
08:
09: import java.io.Serializable;
10: import java.util.HashMap;
11: import java.util.Map;
12:
13: public class ClassInstance implements Serializable {
14: private final static Map PRIMITIVE_TYPE_MAP = new HashMap();
15:
16: private final UTF8ByteDataHolder name;
17: private final UTF8ByteDataHolder loaderDef;
18:
19: static {
20: PRIMITIVE_TYPE_MAP.put(Integer.TYPE.getName(), Integer.TYPE);
21: PRIMITIVE_TYPE_MAP.put(Short.TYPE.getName(), Short.TYPE);
22: PRIMITIVE_TYPE_MAP.put(Long.TYPE.getName(), Long.TYPE);
23: PRIMITIVE_TYPE_MAP.put(Byte.TYPE.getName(), Byte.TYPE);
24: PRIMITIVE_TYPE_MAP.put(Double.TYPE.getName(), Double.TYPE);
25: PRIMITIVE_TYPE_MAP.put(Float.TYPE.getName(), Float.TYPE);
26: PRIMITIVE_TYPE_MAP.put(Double.TYPE.getName(), Double.TYPE);
27: PRIMITIVE_TYPE_MAP.put(Boolean.TYPE.getName(), Boolean.TYPE);
28: PRIMITIVE_TYPE_MAP.put(Void.TYPE.getName(), Void.TYPE);
29: }
30:
31: private static Class getPrimitiveClass(String className) {
32: return (Class) PRIMITIVE_TYPE_MAP.get(className);
33: }
34:
35: // Used in tests
36: ClassInstance(String className, String loaderDefinition) {
37: this (new UTF8ByteDataHolder(className), new UTF8ByteDataHolder(
38: loaderDefinition));
39: }
40:
41: public ClassInstance(UTF8ByteDataHolder className,
42: UTF8ByteDataHolder loaderDefinition) {
43: name = className;
44: loaderDef = loaderDefinition;
45: }
46:
47: public Class asClass(ClassProvider classProvider)
48: throws ClassNotFoundException {
49: String classLoaderdef = loaderDef.asString();
50: String className = name.asString();
51: Class clazz = getPrimitiveClass(className);
52: if (clazz != null) {
53: return clazz;
54: }
55: return classProvider.getClassFor(className, classLoaderdef);
56: }
57:
58: public boolean equals(Object obj) {
59: if (obj instanceof ClassInstance) {
60: ClassInstance other = (ClassInstance) obj;
61: return this .name.equals(other.name)
62: && this .loaderDef.equals(other.loaderDef);
63: }
64: return false;
65: }
66:
67: public int hashCode() {
68: int hash = 17;
69: hash = (37 * hash) + name.hashCode();
70: hash = (37 * hash) + loaderDef.hashCode();
71: return hash;
72: }
73:
74: public UTF8ByteDataHolder getLoaderDef() {
75: return loaderDef;
76: }
77:
78: public UTF8ByteDataHolder getName() {
79: return name;
80: }
81:
82: public String toString() {
83: return "Class(" + name.asString() + "," + loaderDef.asString()
84: + ")";
85: }
86:
87: }
|