| 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 | */ |
| 19 | package org.fuwjax.jon.type; |
| 20 | |
| 21 | import java.util.Locale; |
| 22 | |
| 23 | import org.fuwjax.jon.ClassCastStrategy; |
| 24 | import org.fuwjax.jon.ObjectAccessException; |
| 25 | import org.fuwjax.jon.ReferenceStrategy; |
| 26 | import org.fuwjax.jon.accessor.Literal; |
| 27 | import org.fuwjax.jon.accessor.LiteralAccessor; |
| 28 | import org.fuwjax.jon.accessor.Symbol; |
| 29 | |
| 30 | /** |
| 31 | * An abstract type for objects. |
| 32 | * @author michaeldoberenz |
| 33 | */ |
| 34 | public 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 | } |