001/** 002 * GRANITE DATA SERVICES 003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S. 004 * 005 * This file is part of the Granite Data Services Platform. 006 * 007 * Granite Data Services is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * Granite Data Services is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 015 * General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 020 * USA, or see <http://www.gnu.org/licenses/>. 021 */ 022package org.granite.messaging.jmf.codec.std.impl; 023 024import java.io.IOException; 025import java.lang.reflect.InvocationTargetException; 026 027import org.granite.messaging.jmf.DumpContext; 028import org.granite.messaging.jmf.InputContext; 029import org.granite.messaging.jmf.OutputContext; 030import org.granite.messaging.jmf.codec.std.ClassCodec; 031 032public class ClassCodecImpl extends AbstractIntegerStringCodec<Object> implements ClassCodec { 033 034 protected static final StringTypeHandler TYPE_HANDLER = new StringTypeHandler() { 035 036 public int type(IntegerComponents ics, boolean reference) { 037 return (reference ? (0x40 | (ics.length << 4) | JMF_CLASS) : ((ics.length << 4) | JMF_CLASS)); 038 } 039 040 public int indexOrLengthBytesCount(int parameterizedJmfType) { 041 return (parameterizedJmfType >> 4) & 0x03; 042 } 043 044 public boolean isReference(int parameterizedJmfType) { 045 return (parameterizedJmfType & 0x40) != 0; 046 } 047 }; 048 049 public int getObjectType() { 050 return JMF_CLASS; 051 } 052 053 public boolean canEncode(Object v) { 054 return (v instanceof Class); 055 } 056 057 public void encode(OutputContext ctx, Object v) throws IOException, IllegalAccessException { 058 writeString(ctx, ctx.getAlias(((Class<?>)v).getName()), TYPE_HANDLER); 059 } 060 061 public Object decode(InputContext ctx, int parameterizedJmfType) 062 throws IOException, ClassNotFoundException, InstantiationException, 063 IllegalAccessException, InvocationTargetException, 064 SecurityException, NoSuchMethodException { 065 int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType); 066 067 if (jmfType != JMF_CLASS) 068 throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType); 069 070 String className = readString(ctx, parameterizedJmfType, TYPE_HANDLER); 071 className = ctx.getAlias(className); 072 return ctx.getReflection().loadClass(className); 073 } 074 075 public void dump(DumpContext ctx, int parameterizedJmfType) throws IOException { 076 int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType); 077 078 if (jmfType != JMF_CLASS) 079 throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType); 080 081 String className = readString(ctx, parameterizedJmfType, TYPE_HANDLER); 082 className = ctx.getAlias(className); 083 ctx.indentPrintLn(Class.class.getName() + ": " + className + ".class"); 084 } 085}