limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2008 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | package com.google.inject; |
| 19 | |
| 20 | import com.google.inject.internal.Errors; |
| 21 | import com.google.inject.internal.MatcherAndConverter; |
| 22 | import com.google.inject.internal.SourceProvider; |
| 23 | import com.google.inject.internal.Strings; |
| 24 | import com.google.inject.matcher.AbstractMatcher; |
| 25 | import com.google.inject.matcher.Matcher; |
| 26 | import com.google.inject.matcher.Matchers; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 27 | import com.google.inject.spi.TypeConverter; |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 28 | import com.google.inject.spi.TypeConverterBinding; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 29 | import java.lang.reflect.InvocationTargetException; |
| 30 | import java.lang.reflect.Method; |
| 31 | import java.lang.reflect.Type; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Handles {@link Binder#convertToTypes} commands. |
| 35 | * |
| 36 | * @author crazybob@google.com (Bob Lee) |
| 37 | * @author jessewilson@google.com (Jesse Wilson) |
| 38 | */ |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 39 | class TypeConverterBindingProcessor extends AbstractProcessor { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 40 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 41 | TypeConverterBindingProcessor(Errors errors) { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 42 | super(errors); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 43 | } |
| 44 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 45 | /** Installs default converters for primitives, enums, and class literals. */ |
| 46 | public void prepareBuiltInConverters(InjectorImpl injector) { |
| 47 | this.injector = injector; |
| 48 | try { |
| 49 | // Configure type converters. |
| 50 | convertToPrimitiveType(int.class, Integer.class); |
| 51 | convertToPrimitiveType(long.class, Long.class); |
| 52 | convertToPrimitiveType(boolean.class, Boolean.class); |
| 53 | convertToPrimitiveType(byte.class, Byte.class); |
| 54 | convertToPrimitiveType(short.class, Short.class); |
| 55 | convertToPrimitiveType(float.class, Float.class); |
| 56 | convertToPrimitiveType(double.class, Double.class); |
| 57 | |
| 58 | convertToClass(Character.class, new TypeConverter() { |
| 59 | public Object convert(String value, TypeLiteral<?> toType) { |
| 60 | value = value.trim(); |
| 61 | if (value.length() != 1) { |
| 62 | throw new RuntimeException("Length != 1."); |
| 63 | } |
| 64 | return value.charAt(0); |
| 65 | } |
| 66 | |
| 67 | @Override public String toString() { |
| 68 | return "TypeConverter<Character>"; |
| 69 | } |
| 70 | }); |
| 71 | |
| 72 | convertToClasses(Matchers.subclassesOf(Enum.class), new TypeConverter() { |
| 73 | @SuppressWarnings("unchecked") |
| 74 | public Object convert(String value, TypeLiteral<?> toType) { |
| 75 | return Enum.valueOf((Class) toType.getRawType(), value); |
| 76 | } |
| 77 | |
| 78 | @Override public String toString() { |
| 79 | return "TypeConverter<E extends Enum<E>>"; |
| 80 | } |
| 81 | }); |
| 82 | |
| 83 | internalConvertToTypes( |
| 84 | new AbstractMatcher<TypeLiteral<?>>() { |
| 85 | public boolean matches(TypeLiteral<?> typeLiteral) { |
| 86 | return typeLiteral.getRawType() == Class.class; |
| 87 | } |
| 88 | |
| 89 | @Override public String toString() { |
| 90 | return "Class<?>"; |
| 91 | } |
| 92 | }, |
| 93 | new TypeConverter() { |
| 94 | @SuppressWarnings("unchecked") |
| 95 | public Object convert(String value, TypeLiteral<?> toType) { |
| 96 | try { |
| 97 | return Class.forName(value); |
| 98 | } catch (ClassNotFoundException e) { |
| 99 | throw new RuntimeException(e.getMessage()); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | @Override public String toString() { |
| 104 | return "TypeConverter<Class<?>>"; |
| 105 | } |
| 106 | } |
| 107 | ); |
| 108 | } finally { |
| 109 | this.injector = null; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | private <T> void convertToPrimitiveType(Class<T> primitiveType, final Class<T> wrapperType) { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 114 | try { |
| 115 | final Method parser = wrapperType.getMethod( |
| 116 | "parse" + Strings.capitalize(primitiveType.getName()), String.class); |
| 117 | |
| 118 | TypeConverter typeConverter = new TypeConverter() { |
| 119 | @SuppressWarnings("unchecked") |
| 120 | public Object convert(String value, TypeLiteral<?> toType) { |
| 121 | try { |
| 122 | return parser.invoke(null, value); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 123 | } catch (IllegalAccessException e) { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 124 | throw new AssertionError(e); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 125 | } catch (InvocationTargetException e) { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 126 | throw new RuntimeException(e.getTargetException().getMessage()); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | @Override public String toString() { |
| 131 | return "TypeConverter<" + wrapperType.getSimpleName() + ">"; |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | convertToClass(wrapperType, typeConverter); |
| 136 | } catch (NoSuchMethodException e) { |
| 137 | throw new AssertionError(e); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | private <T> void convertToClass(Class<T> type, TypeConverter converter) { |
| 142 | convertToClasses(Matchers.identicalTo(type), converter); |
| 143 | } |
| 144 | |
| 145 | private void convertToClasses(final Matcher<? super Class<?>> typeMatcher, |
| 146 | TypeConverter converter) { |
| 147 | internalConvertToTypes(new AbstractMatcher<TypeLiteral<?>>() { |
| 148 | public boolean matches(TypeLiteral<?> typeLiteral) { |
| 149 | Type type = typeLiteral.getType(); |
| 150 | if (!(type instanceof Class)) { |
| 151 | return false; |
| 152 | } |
| 153 | Class<?> clazz = (Class<?>) type; |
| 154 | return typeMatcher.matches(clazz); |
| 155 | } |
| 156 | |
| 157 | @Override public String toString() { |
| 158 | return typeMatcher.toString(); |
| 159 | } |
| 160 | }, converter); |
| 161 | } |
| 162 | |
| 163 | private void internalConvertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, |
| 164 | TypeConverter converter) { |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 165 | injector.state.addConverter( |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 166 | new MatcherAndConverter(typeMatcher, converter, SourceProvider.UNKNOWN_SOURCE)); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 167 | } |
| 168 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 169 | @Override public Boolean visit(TypeConverterBinding command) { |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 170 | injector.state.addConverter(new MatcherAndConverter( |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 171 | command.getTypeMatcher(), command.getTypeConverter(), command.getSource())); |
| 172 | return true; |
| 173 | } |
| 174 | } |