1 16 package com.google.gwt.i18n.rebind; 17 18 import com.google.gwt.core.ext.TreeLogger; 19 import com.google.gwt.core.ext.UnableToCompleteException; 20 import com.google.gwt.core.ext.typeinfo.JClassType; 21 import com.google.gwt.core.ext.typeinfo.JMethod; 22 import com.google.gwt.core.ext.typeinfo.JType; 23 import com.google.gwt.core.ext.typeinfo.NotFoundException; 24 import com.google.gwt.core.ext.typeinfo.TypeOracle; 25 import com.google.gwt.core.ext.typeinfo.TypeOracleException; 26 import com.google.gwt.i18n.rebind.util.AbstractResource; 27 import com.google.gwt.user.rebind.SourceWriter; 28 29 import java.util.Map ; 30 31 35 class ConstantsImplCreator extends AbstractLocalizableImplCreator { 36 39 private boolean needCache = false; 40 41 51 public ConstantsImplCreator(TreeLogger logger, SourceWriter writer, 52 JClassType localizableClass, AbstractResource messageBindings, 53 TypeOracle oracle) throws UnableToCompleteException { 54 super(writer, localizableClass, messageBindings); 55 try { 56 JClassType stringClass = oracle.getType(String .class.getName()); 57 JClassType mapClass = oracle.getType(Map .class.getName()); 58 JType stringArrayClass = oracle.getArrayType(stringClass); 59 JType intClass = oracle.parse(int.class.getName()); 60 JType doubleClass = oracle.parse(double.class.getName()); 61 JType floatClass = oracle.parse(float.class.getName()); 62 JType booleanClass = oracle.parse(boolean.class.getName()); 63 register(stringClass, new SimpleValueMethodCreator(this, 64 SimpleValueMethodCreator.STRING)); 65 register(mapClass, new ConstantsMapMethodCreator(this)); 66 register(intClass, new SimpleValueMethodCreator(this, 67 SimpleValueMethodCreator.INT)); 68 register(doubleClass, new SimpleValueMethodCreator(this, 69 SimpleValueMethodCreator.DOUBLE)); 70 register(floatClass, new SimpleValueMethodCreator(this, 71 SimpleValueMethodCreator.FLOAT)); 72 register(booleanClass, new SimpleValueMethodCreator(this, 73 SimpleValueMethodCreator.BOOLEAN)); 74 75 register(stringArrayClass, new ConstantsStringArrayMethodCreator(this)); 76 } catch (NotFoundException e) { 77 throw error(logger, e); 78 } catch (TypeOracleException e) { 79 throw error(logger, e); 80 } 81 } 82 83 protected void classEpilog() { 84 if (isNeedCache()) { 85 getWriter().println( 86 "java.util.Map cache = new java.util.HashMap();".toString()); 87 } 88 } 89 90 94 protected void emitMethodBody(TreeLogger logger, JMethod method) 95 throws UnableToCompleteException { 96 checkConstantMethod(logger, method); 97 delegateToCreator(logger, method); 98 } 99 100 boolean isNeedCache() { 101 return needCache; 102 } 103 104 void setNeedCache(boolean needCache) { 105 this.needCache = needCache; 106 } 107 108 114 private void checkConstantMethod(TreeLogger logger, JMethod method) 115 throws UnableToCompleteException { 116 if (method.getParameters().length > 0) { 117 String s = "Methods in interfaces extending Constant must have no parameters and a return type of String/int/float/boolean/double/String[]/Map"; 118 throw error(logger, s); 119 } 120 } 121 } 122 | Popular Tags |