Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(107)

Unified Diff: src/com/google/gwt/inject/rebind/binding/BindConstantBinding.java

Issue 90085: Fixed enum binding support (issue #49) (Closed) SVN Base: http://google-gin.googlecode.com/svn/trunk/
Patch Set: Using getEnclosingClass Created 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side by-side-diff with in-line comments
Download patch
Index: src/com/google/gwt/inject/rebind/binding/BindConstantBinding.java
===================================================================
--- src/com/google/gwt/inject/rebind/binding/BindConstantBinding.java (revision 108)
+++ src/com/google/gwt/inject/rebind/binding/BindConstantBinding.java (working copy)
@@ -17,6 +17,7 @@
import com.google.gwt.core.ext.Generator;
import com.google.gwt.inject.rebind.util.SourceWriteUtil;
+import com.google.gwt.inject.rebind.util.NameGenerator;
import com.google.gwt.user.rebind.SourceWriter;
import com.google.inject.Inject;
import com.google.inject.Key;
@@ -28,6 +29,8 @@
* Binding for a constant value.
*/
public class BindConstantBinding implements Binding {
+
+ private final NameGenerator nameGenerator;
private String valueToOutput;
/**
@@ -53,8 +56,9 @@
private final SourceWriteUtil sourceWriteUtil;
@Inject
- public BindConstantBinding(SourceWriteUtil sourceWriteUtil) {
+ public BindConstantBinding(SourceWriteUtil sourceWriteUtil, NameGenerator nameGenerator) {
this.sourceWriteUtil = sourceWriteUtil;
+ this.nameGenerator = nameGenerator;
}
/**
@@ -80,7 +84,19 @@
} else if (instance instanceof Number || instance instanceof Boolean) {
valueToOutput = instance.toString(); // Includes int & short.
} else if (instance instanceof Enum) {
- valueToOutput = instance.getClass().getName() + "." + ((Enum) instance).name();
+ String className = instance.getClass().getName();
+
+ // Enums become anonymous inner classes if they have a custom
+ // implementation. Their classname is then of the form "com.foo.Bar$1".
+ // We need to be careful here to not clobber inner enums (which also
+ // have a $ in their classname). The regex below matches any classname
+ // that terminates in a $ followed by a number, i.e. an anonymous class.
+ if (className.matches(".+\\$\\d+\\z")) {
+ className = instance.getClass().getEnclosingClass().getName();
+ }
+ className = nameGenerator.binaryNameToSourceName(className);
+
+ valueToOutput = className + "." + ((Enum) instance).name();
} else {
throw new IllegalArgumentException("Attempted to create a constant binding with a "
+ "non-constant type: " + type);

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld r497