01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package java.beans;
19:
20: import java.io.IOException;
21: import java.io.InputStream;
22: import java.io.ObjectInputStream;
23: import java.io.ObjectStreamClass;
24: import java.lang.reflect.Array;
25:
26: import org.apache.harmony.beans.internal.nls.Messages;
27:
28: /**
29: * Customized object input stream that allows to read objects by specified class
30: * loader
31: */
32: class CustomizedObjectInputStream extends ObjectInputStream {
33:
34: private ClassLoader cls;
35:
36: public CustomizedObjectInputStream(InputStream in, ClassLoader cls)
37: throws IOException {
38: super (in);
39: this .cls = cls;
40: }
41:
42: @Override
43: protected Class<?> resolveClass(ObjectStreamClass desc)
44: throws IOException, ClassNotFoundException {
45: String className = desc.getName();
46:
47: if (className.startsWith("[")) { //$NON-NLS-1$
48: int idx = className.lastIndexOf("["); //$NON-NLS-1$
49: String prefix = className.substring(0, idx + 1);
50: int[] dimensions = new int[prefix.length()];
51: String postfix;
52: Class<?> componentType = null;
53:
54: for (int i = 0; i < dimensions.length; ++i) {
55: dimensions[i] = 0;
56: }
57:
58: postfix = className.substring(idx + 1);
59: if (postfix.equals("Z")) { //$NON-NLS-1$
60: componentType = boolean.class;
61: } else if (postfix.equals("B")) { //$NON-NLS-1$
62: componentType = byte.class;
63: } else if (postfix.equals("C")) { //$NON-NLS-1$
64: componentType = char.class;
65: } else if (postfix.equals("D")) { //$NON-NLS-1$
66: componentType = double.class;
67: } else if (postfix.equals("F")) { //$NON-NLS-1$
68: componentType = float.class;
69: } else if (postfix.equals("I")) { //$NON-NLS-1$
70: componentType = int.class;
71: } else if (postfix.equals("L")) { //$NON-NLS-1$
72: componentType = long.class;
73: } else if (postfix.equals("S")) { //$NON-NLS-1$
74: componentType = short.class;
75: } else if (postfix.equals("V")) { //$NON-NLS-1$
76: // expected, componentType is already null
77: } else if (postfix.startsWith("L")) { //$NON-NLS-1$
78: componentType = cls.loadClass(postfix.substring(1,
79: postfix.length() - 1));
80: } else {
81: throw new IllegalArgumentException(Messages.getString(
82: "beans.1E", className)); //$NON-NLS-1$
83: }
84:
85: return Array.newInstance(componentType, dimensions)
86: .getClass();
87: }
88:
89: return Class.forName(className, true, cls);
90: }
91: }
|