001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 the ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016+-------------------------------------------------------------------------*/
017package org.conqat.lib.commons.serialization.classes;
018
019import java.io.DataInputStream;
020import java.io.DataOutputStream;
021import java.io.IOException;
022import java.io.ObjectStreamConstants;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.conqat.lib.commons.serialization.SerializedEntityParser;
027import org.conqat.lib.commons.serialization.SerializedEntityPool;
028import org.conqat.lib.commons.serialization.SerializedEntitySerializer;
029import org.conqat.lib.commons.string.StringUtils;
030
031/**
032 * A serialized <a href=
033 * "http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Proxy.html">proxy
034 * class</a>.
035 */
036public class SerializedProxyClass extends SerializedClassBase {
037
038        /** The names of interfaces implemented by the proxy class. */
039        private List<String> proxyInterfaceNames;
040
041        /** Constructor. */
042        public SerializedProxyClass(DataInputStream din, SerializedEntityPool pool, SerializedEntityParser parser)
043                        throws IOException {
044                super(din, pool, parser);
045        }
046
047        /** {@inheritDoc} */
048        @Override
049        protected void parseClass(DataInputStream din, SerializedEntityPool pool, SerializedEntityParser parser)
050                        throws IOException {
051                int proxyClassCount = din.readInt();
052                proxyInterfaceNames = new ArrayList<>();
053                for (int i = 0; i < proxyClassCount; ++i) {
054                        proxyInterfaceNames.add(din.readUTF());
055                }
056        }
057
058        /** {@inheritDoc} */
059        @Override
060        protected void serializeClass(DataOutputStream dos, SerializedEntitySerializer serializer) throws IOException {
061                dos.writeByte(ObjectStreamConstants.TC_PROXYCLASSDESC);
062                dos.writeInt(proxyInterfaceNames.size());
063                for (String proxyInterfaceName : proxyInterfaceNames) {
064                        dos.writeUTF(proxyInterfaceName);
065                }
066        }
067
068        /** {@inheritDoc} */
069        @Override
070        public String toString() {
071                return "Proxy class for " + StringUtils.concat(proxyInterfaceNames, "|");
072        }
073}