EMMA Coverage Report (generated Wed Aug 29 00:03:59 CDT 2007)
[all classes][org.fuwjax.jon.type]

COVERAGE SUMMARY FOR SOURCE FILE [ClassType.java]

nameclass, %method, %block, %line, %
ClassType.java100% (2/2)100% (16/16)99%  (246/249)98%  (44/45)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ClassType100% (1/1)100% (11/11)98%  (133/136)97%  (31/32)
reduce (String): String 100% (1/1)89%  (24/27)75%  (3/4)
<static initializer> 100% (1/1)100% (5/5)100% (1/1)
ClassType (): void 100% (1/1)100% (4/4)100% (2/2)
convertArray (String): String 100% (1/1)100% (35/35)100% (8/8)
createInstance (CharSequence, Object): Object 100% (1/1)100% (26/26)100% (6/6)
getBinaryName (String): String 100% (1/1)100% (25/25)100% (5/5)
getClassCastStrategy (): ClassCastStrategy 100% (1/1)100% (2/2)100% (1/1)
getLiteral (): Literal 100% (1/1)100% (2/2)100% (1/1)
getReferenceStrategy (): ReferenceStrategy 100% (1/1)100% (2/2)100% (1/1)
getSymbolSpec (): Symbol 100% (1/1)100% (2/2)100% (1/1)
toString (Object): CharSequence 100% (1/1)100% (6/6)100% (2/2)
     
class ClassType$BinaryName100% (1/1)100% (5/5)100% (113/113)100% (13/13)
<static initializer> 100% (1/1)100% (92/92)100% (9/9)
ClassType$BinaryName (String, int, char): void 100% (1/1)100% (8/8)100% (3/3)
toString (): String 100% (1/1)100% (4/4)100% (1/1)
valueOf (String): ClassType$BinaryName 100% (1/1)100% (5/5)100% (1/1)
values (): ClassType$BinaryName [] 100% (1/1)100% (4/4)100% (1/1)

1/*
2 * This file is part of JON.
3 *
4 * JON is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * JON is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 * 
17 * Copyright 2007 Michael Doberenz
18 */
19package org.fuwjax.jon.type;
20 
21import java.util.Locale;
22 
23import org.fuwjax.jon.ClassCastStrategy;
24import org.fuwjax.jon.ObjectAccessException;
25import org.fuwjax.jon.ReferenceStrategy;
26import org.fuwjax.jon.accessor.Literal;
27import org.fuwjax.jon.accessor.LiteralAccessor;
28import org.fuwjax.jon.accessor.Symbol;
29 
30/**
31 * An abstract type for objects.
32 * @author michaeldoberenz
33 */
34public final class ClassType extends AbstractIndirectType implements LiteralAccessor{
35        /**
36         * The singleton instance.
37         */
38        public static final ClassType DEFAULT = new ClassType();
39        private static final int LONGEST_PRIMITIVE_NAME_LENGTH = 7;
40 
41        /**
42         * The set of primitive types and their binary encoding.
43         * @author michaeldoberenz
44         */
45        private enum BinaryName{
46                /**
47                 * boolean.
48                 */
49                BOOLEAN('Z'),
50                /**
51                 * byte.
52                 */
53                BYTE('B'),
54                /**
55                 * char.
56                 */
57                CHAR('C'),
58                /**
59                 * double.
60                 */
61                DOUBLE('D'),
62                /**
63                 * float.
64                 */
65                FLOAT('F'),
66                /**
67                 * int.
68                 */
69                INT('I'),
70                /**
71                 * long.
72                 */
73                LONG('J'),
74                /**
75                 * short.
76                 */
77                SHORT('S');
78                private char binary;
79 
80                private BinaryName(final char binary){
81                        this.binary = binary;
82                }
83 
84                @Override
85                public String toString(){
86                        return Character.toString(binary);
87                }
88        }
89 
90        private ClassType(){
91                super(Class.class);
92        }
93 
94        public ReferenceStrategy getReferenceStrategy(){
95                return ReferenceStrategy.AssignedReference;
96        }
97 
98        public ClassCastStrategy getClassCastStrategy(){
99                return ClassCastStrategy.AlwaysCast;
100        }
101 
102        public Symbol getSymbolSpec(){
103                return Symbol.Literal;
104        }
105 
106        public Literal getLiteral(){
107                return Literal.Class;
108        }
109 
110        public CharSequence toString(final Object object) throws ObjectAccessException{
111                final Class<?> type = (Class<?>)object;
112                return type.getCanonicalName();
113        }
114 
115        public Object createInstance(final CharSequence parse, final Object source) throws ObjectAccessException{
116                String className = parse.toString();
117                try{
118                        if(className.charAt(className.length() - 1) == ']'){
119                                className = convertArray(className);
120                        }
121                        return Class.forName(className);
122                }catch(ClassNotFoundException e){
123                        return createInstance(reduce(className), source);
124                }
125        }
126 
127        private String reduce(final String className) throws ObjectAccessException{
128                final int index = className.lastIndexOf('.');
129                if(index < 0){
130                        throw ObjectAccessException.Message.ClassNotFound.exception();
131                }
132                return className.substring(0, index) + '$' + className.substring(index + 1);
133        }
134 
135        private String convertArray(final String className){
136                final StringBuilder builder = new StringBuilder();
137                int bracket = className.indexOf('[');
138                final String componentName = className.substring(0, bracket);
139                final String binaryName = getBinaryName(componentName);
140                do{
141                        builder.append('[');
142                        bracket = className.indexOf('[', bracket + 1);
143                }while(bracket >= 0);
144                return builder.append(binaryName).toString();
145        }
146 
147        private String getBinaryName(final String componentName){
148                if(componentName.length() <= LONGEST_PRIMITIVE_NAME_LENGTH){
149                        final BinaryName name = BinaryName.valueOf(componentName.toUpperCase(Locale.US));
150                        if(name != null){
151                                return name.toString();
152                        }
153                }
154                return 'L' + componentName + ';';
155        }
156}

[all classes][org.fuwjax.jon.type]
EMMA 2.0.5312 (C) Vladimir Roubtsov