001    package org.granite.messaging.jmf.codec.std.impl;
002    
003    import java.io.IOException;
004    import java.lang.reflect.InvocationTargetException;
005    
006    import org.granite.messaging.jmf.DumpContext;
007    import org.granite.messaging.jmf.InputContext;
008    import org.granite.messaging.jmf.OutputContext;
009    import org.granite.messaging.jmf.codec.std.ClassCodec;
010    
011    public class ClassCodecImpl extends AbstractIntegerStringCodec<Object> implements ClassCodec {
012    
013            protected static final StringTypeHandler TYPE_HANDLER = new StringTypeHandler() {
014    
015                    public int type(IntegerComponents ics, boolean reference) {
016                            return (reference ? (0x40 | (ics.length << 4) | JMF_CLASS) : ((ics.length << 4) | JMF_CLASS));
017                    }
018    
019                    public int indexOrLengthBytesCount(int parameterizedJmfType) {
020                            return (parameterizedJmfType >> 4) & 0x03;
021                    }
022    
023                    public boolean isReference(int parameterizedJmfType) {
024                            return (parameterizedJmfType & 0x40) != 0;
025                    }
026            };
027    
028            public int getObjectType() {
029                    return JMF_CLASS;
030            }
031    
032            public boolean canEncode(Object v) {
033                    return (v instanceof Class);
034            }
035    
036            public void encode(OutputContext ctx, Object v) throws IOException, IllegalAccessException {
037                    writeString(ctx, ctx.getAlias(((Class<?>)v).getName()), TYPE_HANDLER);
038            }
039    
040            public Object decode(InputContext ctx, int parameterizedJmfType)
041                            throws IOException, ClassNotFoundException, InstantiationException,
042                            IllegalAccessException, InvocationTargetException,
043                            SecurityException, NoSuchMethodException {
044                    int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
045                    
046                    if (jmfType != JMF_CLASS)
047                            throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
048                    
049                    String className = readString(ctx, parameterizedJmfType, TYPE_HANDLER);
050                    className = ctx.getAlias(className);
051                    return ctx.getReflection().loadClass(className);
052            }
053    
054            public void dump(DumpContext ctx, int parameterizedJmfType) throws IOException {
055                    int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
056                    
057                    if (jmfType != JMF_CLASS)
058                            throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
059                    
060                    String className = readString(ctx, parameterizedJmfType, TYPE_HANDLER);
061                    className = ctx.getAlias(className);
062                    ctx.indentPrintLn(Class.class.getName() + ": " + className + ".class");
063            }
064    }