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 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 17 | package com.google.inject.internal; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 18 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 19 | import com.google.inject.TypeLiteral; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 20 | import com.google.inject.matcher.AbstractMatcher; |
| 21 | import com.google.inject.matcher.Matcher; |
| 22 | import com.google.inject.matcher.Matchers; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 23 | import com.google.inject.spi.TypeConverter; |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 24 | import com.google.inject.spi.TypeConverterBinding; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 25 | import java.lang.reflect.InvocationTargetException; |
| 26 | import java.lang.reflect.Method; |
| 27 | import java.lang.reflect.Type; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 28 | |
| 29 | /** |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 30 | * Handles {@code Binder.convertToTypes} commands. |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 31 | * |
| 32 | * @author crazybob@google.com (Bob Lee) |
| 33 | * @author jessewilson@google.com (Jesse Wilson) |
| 34 | */ |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 35 | final class TypeConverterBindingProcessor extends AbstractProcessor { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 36 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 37 | TypeConverterBindingProcessor(Errors errors) { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 38 | super(errors); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 39 | } |
| 40 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 41 | /** Installs default converters for primitives, enums, and class literals. */ |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 42 | void prepareBuiltInConverters(InjectorImpl injector) { |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 43 | this.injector = injector; |
| 44 | try { |
| 45 | // Configure type converters. |
| 46 | convertToPrimitiveType(int.class, Integer.class); |
| 47 | convertToPrimitiveType(long.class, Long.class); |
| 48 | convertToPrimitiveType(boolean.class, Boolean.class); |
| 49 | convertToPrimitiveType(byte.class, Byte.class); |
| 50 | convertToPrimitiveType(short.class, Short.class); |
| 51 | convertToPrimitiveType(float.class, Float.class); |
| 52 | convertToPrimitiveType(double.class, Double.class); |
| 53 | |
| 54 | convertToClass(Character.class, new TypeConverter() { |
| 55 | public Object convert(String value, TypeLiteral<?> toType) { |
| 56 | value = value.trim(); |
| 57 | if (value.length() != 1) { |
| 58 | throw new RuntimeException("Length != 1."); |
| 59 | } |
| 60 | return value.charAt(0); |
| 61 | } |
| 62 | |
| 63 | @Override public String toString() { |
| 64 | return "TypeConverter<Character>"; |
| 65 | } |
| 66 | }); |
| 67 | |
| 68 | convertToClasses(Matchers.subclassesOf(Enum.class), new TypeConverter() { |
| 69 | @SuppressWarnings("unchecked") |
| 70 | public Object convert(String value, TypeLiteral<?> toType) { |
| 71 | return Enum.valueOf((Class) toType.getRawType(), value); |
| 72 | } |
| 73 | |
| 74 | @Override public String toString() { |
| 75 | return "TypeConverter<E extends Enum<E>>"; |
| 76 | } |
| 77 | }); |
| 78 | |
| 79 | internalConvertToTypes( |
| 80 | new AbstractMatcher<TypeLiteral<?>>() { |
| 81 | public boolean matches(TypeLiteral<?> typeLiteral) { |
| 82 | return typeLiteral.getRawType() == Class.class; |
| 83 | } |
| 84 | |
| 85 | @Override public String toString() { |
| 86 | return "Class<?>"; |
| 87 | } |
| 88 | }, |
| 89 | new TypeConverter() { |
| 90 | @SuppressWarnings("unchecked") |
| 91 | public Object convert(String value, TypeLiteral<?> toType) { |
| 92 | try { |
| 93 | return Class.forName(value); |
| 94 | } catch (ClassNotFoundException e) { |
| 95 | throw new RuntimeException(e.getMessage()); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | @Override public String toString() { |
| 100 | return "TypeConverter<Class<?>>"; |
| 101 | } |
| 102 | } |
| 103 | ); |
| 104 | } finally { |
| 105 | this.injector = null; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | private <T> void convertToPrimitiveType(Class<T> primitiveType, final Class<T> wrapperType) { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 110 | try { |
| 111 | final Method parser = wrapperType.getMethod( |
| 112 | "parse" + Strings.capitalize(primitiveType.getName()), String.class); |
| 113 | |
| 114 | TypeConverter typeConverter = new TypeConverter() { |
| 115 | @SuppressWarnings("unchecked") |
| 116 | public Object convert(String value, TypeLiteral<?> toType) { |
| 117 | try { |
| 118 | return parser.invoke(null, value); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 119 | } catch (IllegalAccessException e) { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 120 | throw new AssertionError(e); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 121 | } catch (InvocationTargetException e) { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 122 | throw new RuntimeException(e.getTargetException().getMessage()); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | @Override public String toString() { |
| 127 | return "TypeConverter<" + wrapperType.getSimpleName() + ">"; |
| 128 | } |
| 129 | }; |
| 130 | |
| 131 | convertToClass(wrapperType, typeConverter); |
| 132 | } catch (NoSuchMethodException e) { |
| 133 | throw new AssertionError(e); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | private <T> void convertToClass(Class<T> type, TypeConverter converter) { |
| 138 | convertToClasses(Matchers.identicalTo(type), converter); |
| 139 | } |
| 140 | |
| 141 | private void convertToClasses(final Matcher<? super Class<?>> typeMatcher, |
| 142 | TypeConverter converter) { |
| 143 | internalConvertToTypes(new AbstractMatcher<TypeLiteral<?>>() { |
| 144 | public boolean matches(TypeLiteral<?> typeLiteral) { |
| 145 | Type type = typeLiteral.getType(); |
| 146 | if (!(type instanceof Class)) { |
| 147 | return false; |
| 148 | } |
| 149 | Class<?> clazz = (Class<?>) type; |
| 150 | return typeMatcher.matches(clazz); |
| 151 | } |
| 152 | |
| 153 | @Override public String toString() { |
| 154 | return typeMatcher.toString(); |
| 155 | } |
| 156 | }, converter); |
| 157 | } |
| 158 | |
| 159 | private void internalConvertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, |
| 160 | TypeConverter converter) { |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 161 | injector.state.addConverter( |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 162 | new MatcherAndConverter(typeMatcher, converter, SourceProvider.UNKNOWN_SOURCE)); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 163 | } |
| 164 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 165 | @Override public Boolean visit(TypeConverterBinding command) { |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 166 | injector.state.addConverter(new MatcherAndConverter( |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 167 | command.getTypeMatcher(), command.getTypeConverter(), command.getSource())); |
| 168 | return true; |
| 169 | } |
| 170 | } |