| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2008 Google Inc. | 2 * Copyright 2008 Google Inc. |
| 3 * | 3 * |
| 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not | 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 * use this file except in compliance with the License. You may obtain a copy of | 5 * use this file except in compliance with the License. You may obtain a copy of |
| 6 * the License at | 6 * the License at |
| 7 * | 7 * |
| 8 * http://www.apache.org/licenses/LICENSE-2.0 | 8 * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 * | 9 * |
| 10 * Unless required by applicable law or agreed to in writing, software | 10 * Unless required by applicable law or agreed to in writing, software |
| 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 * License for the specific language governing permissions and limitations under | 13 * License for the specific language governing permissions and limitations under |
| 14 * the License. | 14 * the License. |
| 15 */ | 15 */ |
| 16 package com.google.gwt.inject.rebind.binding; | 16 package com.google.gwt.inject.rebind.binding; |
| 17 | 17 |
| 18 import com.google.gwt.core.ext.Generator; | 18 import com.google.gwt.core.ext.Generator; |
| 19 import com.google.gwt.inject.rebind.util.SourceWriteUtil; | 19 import com.google.gwt.inject.rebind.util.SourceWriteUtil; |
| 20 import com.google.gwt.inject.rebind.util.NameGenerator; |
| 20 import com.google.gwt.user.rebind.SourceWriter; | 21 import com.google.gwt.user.rebind.SourceWriter; |
| 21 import com.google.inject.Inject; | 22 import com.google.inject.Inject; |
| 22 import com.google.inject.Key; | 23 import com.google.inject.Key; |
| 23 | 24 |
| 24 import java.lang.reflect.Type; | 25 import java.lang.reflect.Type; |
| 25 import java.util.Collections; | 26 import java.util.Collections; |
| 26 | 27 |
| 27 /** | 28 /** |
| 28 * Binding for a constant value. | 29 * Binding for a constant value. |
| 29 */ | 30 */ |
| 30 public class BindConstantBinding implements Binding { | 31 public class BindConstantBinding implements Binding { |
| 32 |
| 33 private final NameGenerator nameGenerator; |
| 31 private String valueToOutput; | 34 private String valueToOutput; |
| 32 | 35 |
| 33 /** | 36 /** |
| 34 * Returns true if the provided key is a valid constant key, i.e. if a | 37 * Returns true if the provided key is a valid constant key, i.e. if a |
| 35 * constant binding can be legally created for it. | 38 * constant binding can be legally created for it. |
| 36 * | 39 * |
| 37 * @param key key to check | 40 * @param key key to check |
| 38 * @return true if constant key | 41 * @return true if constant key |
| 39 */ | 42 */ |
| 40 public static boolean isConstantKey(Key<?> key) { | 43 public static boolean isConstantKey(Key<?> key) { |
| 41 Type type = key.getTypeLiteral().getType(); | 44 Type type = key.getTypeLiteral().getType(); |
| 42 | 45 |
| 43 if (!(type instanceof Class)) { | 46 if (!(type instanceof Class)) { |
| 44 return false; | 47 return false; |
| 45 } | 48 } |
| 46 | 49 |
| 47 Class clazz = (Class) type; | 50 Class clazz = (Class) type; |
| 48 return clazz == String.class || clazz.isPrimitive() || Number.class.isAssign
ableFrom(clazz) | 51 return clazz == String.class || clazz.isPrimitive() || Number.class.isAssign
ableFrom(clazz) |
| 49 || Character.class.isAssignableFrom(clazz) || Boolean.class.isAssignable
From(clazz) | 52 || Character.class.isAssignableFrom(clazz) || Boolean.class.isAssignable
From(clazz) |
| 50 || clazz.isEnum(); | 53 || clazz.isEnum(); |
| 51 } | 54 } |
| 52 | 55 |
| 53 private final SourceWriteUtil sourceWriteUtil; | 56 private final SourceWriteUtil sourceWriteUtil; |
| 54 | 57 |
| 55 @Inject | 58 @Inject |
| 56 public BindConstantBinding(SourceWriteUtil sourceWriteUtil) { | 59 public BindConstantBinding(SourceWriteUtil sourceWriteUtil, NameGenerator name
Generator) { |
| 57 this.sourceWriteUtil = sourceWriteUtil; | 60 this.sourceWriteUtil = sourceWriteUtil; |
| 61 this.nameGenerator = nameGenerator; |
| 58 } | 62 } |
| 59 | 63 |
| 60 /** | 64 /** |
| 61 * Sets this binding's key and instance. Must be called before | 65 * Sets this binding's key and instance. Must be called before |
| 62 * writeCreatorMethod is invoked. | 66 * writeCreatorMethod is invoked. |
| 63 * | 67 * |
| 64 * @param key key to bind to | 68 * @param key key to bind to |
| 65 * @param instance value to bind to | 69 * @param instance value to bind to |
| 66 */ | 70 */ |
| 67 public <T> void setKeyAndInstance(Key<T> key, T instance) { | 71 public <T> void setKeyAndInstance(Key<T> key, T instance) { |
| 68 Type type = key.getTypeLiteral().getType(); | 72 Type type = key.getTypeLiteral().getType(); |
| 69 | 73 |
| 70 if (type == String.class) { | 74 if (type == String.class) { |
| 71 valueToOutput = "\"" + Generator.escape(instance.toString()) + "\""; | 75 valueToOutput = "\"" + Generator.escape(instance.toString()) + "\""; |
| 72 } else if (type == Character.class) { | 76 } else if (type == Character.class) { |
| 73 valueToOutput = "'" + (Character.valueOf('\'').equals(instance) ? "\\" : "
") + instance + "'"; | 77 valueToOutput = "'" + (Character.valueOf('\'').equals(instance) ? "\\" : "
") + instance + "'"; |
| 74 } else if (type == Float.class) { | 78 } else if (type == Float.class) { |
| 75 valueToOutput = instance.toString() + "f"; | 79 valueToOutput = instance.toString() + "f"; |
| 76 } else if (type == Long.class) { | 80 } else if (type == Long.class) { |
| 77 valueToOutput = instance.toString() + "L"; | 81 valueToOutput = instance.toString() + "L"; |
| 78 } else if (type == Double.class) { | 82 } else if (type == Double.class) { |
| 79 valueToOutput = instance.toString() + "d"; | 83 valueToOutput = instance.toString() + "d"; |
| 80 } else if (instance instanceof Number || instance instanceof Boolean) { | 84 } else if (instance instanceof Number || instance instanceof Boolean) { |
| 81 valueToOutput = instance.toString(); // Includes int & short. | 85 valueToOutput = instance.toString(); // Includes int & short. |
| 82 } else if (instance instanceof Enum) { | 86 } else if (instance instanceof Enum) { |
| 83 valueToOutput = instance.getClass().getName() + "." + ((Enum) instance).na
me(); | 87 String className = instance.getClass().getName(); |
| 88 |
| 89 // Enums become anonymous inner classes if they have a custom |
| 90 // implementation. Their classname is then of the form "com.foo.Bar$1". |
| 91 // We need to be careful here to not clobber inner enums (which also |
| 92 // have a $ in their classname). The regex below matches any classname |
| 93 // that terminates in a $ followed by a number, i.e. an anonymous class. |
| 94 if (className.matches(".+\\$\\d+\\z")) { |
| 95 className = instance.getClass().getEnclosingClass().getName(); |
| 96 } |
| 97 className = nameGenerator.binaryNameToSourceName(className); |
| 98 |
| 99 valueToOutput = className + "." + ((Enum) instance).name(); |
| 84 } else { | 100 } else { |
| 85 throw new IllegalArgumentException("Attempted to create a constant binding
with a " | 101 throw new IllegalArgumentException("Attempted to create a constant binding
with a " |
| 86 + "non-constant type: " + type); | 102 + "non-constant type: " + type); |
| 87 } | 103 } |
| 88 } | 104 } |
| 89 | 105 |
| 90 public void writeCreatorMethods(SourceWriter writer, String creatorMethodSigna
ture) { | 106 public void writeCreatorMethods(SourceWriter writer, String creatorMethodSigna
ture) { |
| 91 assert valueToOutput != null; | 107 assert valueToOutput != null; |
| 92 | 108 |
| 93 sourceWriteUtil.writeMethod(writer, creatorMethodSignature, "return " + valu
eToOutput + ";"); | 109 sourceWriteUtil.writeMethod(writer, creatorMethodSignature, "return " + valu
eToOutput + ";"); |
| 94 } | 110 } |
| 95 | 111 |
| 96 public RequiredKeys getRequiredKeys() { | 112 public RequiredKeys getRequiredKeys() { |
| 97 return new RequiredKeys(Collections.<Key<?>>emptySet()); | 113 return new RequiredKeys(Collections.<Key<?>>emptySet()); |
| 98 } | 114 } |
| 99 } | 115 } |
| OLD | NEW |