You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConnectorBundleLoaderFactory.java 48KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  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
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.server.widgetsetutils;
  17. import java.io.PrintWriter;
  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.Collection;
  21. import java.util.Collections;
  22. import java.util.Comparator;
  23. import java.util.HashMap;
  24. import java.util.HashSet;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Map.Entry;
  28. import java.util.Set;
  29. import java.util.TreeMap;
  30. import com.google.gwt.core.client.GWT;
  31. import com.google.gwt.core.client.RunAsyncCallback;
  32. import com.google.gwt.core.ext.Generator;
  33. import com.google.gwt.core.ext.GeneratorContext;
  34. import com.google.gwt.core.ext.TreeLogger;
  35. import com.google.gwt.core.ext.TreeLogger.Type;
  36. import com.google.gwt.core.ext.UnableToCompleteException;
  37. import com.google.gwt.core.ext.typeinfo.JClassType;
  38. import com.google.gwt.core.ext.typeinfo.JMethod;
  39. import com.google.gwt.core.ext.typeinfo.JParameterizedType;
  40. import com.google.gwt.core.ext.typeinfo.JPrimitiveType;
  41. import com.google.gwt.core.ext.typeinfo.JType;
  42. import com.google.gwt.core.ext.typeinfo.NotFoundException;
  43. import com.google.gwt.core.ext.typeinfo.TypeOracle;
  44. import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
  45. import com.google.gwt.user.rebind.SourceWriter;
  46. import com.vaadin.client.JsArrayObject;
  47. import com.vaadin.client.ServerConnector;
  48. import com.vaadin.client.annotations.OnStateChange;
  49. import com.vaadin.client.communication.JsonDecoder;
  50. import com.vaadin.client.metadata.ConnectorBundleLoader;
  51. import com.vaadin.client.metadata.ConnectorBundleLoader.CValUiInfo;
  52. import com.vaadin.client.metadata.InvokationHandler;
  53. import com.vaadin.client.metadata.OnStateChangeMethod;
  54. import com.vaadin.client.metadata.ProxyHandler;
  55. import com.vaadin.client.metadata.TypeData;
  56. import com.vaadin.client.metadata.TypeDataStore;
  57. import com.vaadin.client.metadata.TypeDataStore.MethodAttribute;
  58. import com.vaadin.client.ui.UnknownComponentConnector;
  59. import com.vaadin.client.ui.UnknownExtensionConnector;
  60. import com.vaadin.server.widgetsetutils.metadata.ClientRpcVisitor;
  61. import com.vaadin.server.widgetsetutils.metadata.ConnectorBundle;
  62. import com.vaadin.server.widgetsetutils.metadata.ConnectorInitVisitor;
  63. import com.vaadin.server.widgetsetutils.metadata.GeneratedSerializer;
  64. import com.vaadin.server.widgetsetutils.metadata.OnStateChangeVisitor;
  65. import com.vaadin.server.widgetsetutils.metadata.Property;
  66. import com.vaadin.server.widgetsetutils.metadata.RendererVisitor;
  67. import com.vaadin.server.widgetsetutils.metadata.ServerRpcVisitor;
  68. import com.vaadin.server.widgetsetutils.metadata.StateInitVisitor;
  69. import com.vaadin.server.widgetsetutils.metadata.TypeVisitor;
  70. import com.vaadin.server.widgetsetutils.metadata.WidgetInitVisitor;
  71. import com.vaadin.shared.annotations.DelegateToWidget;
  72. import com.vaadin.shared.annotations.NoLayout;
  73. import com.vaadin.shared.communication.ClientRpc;
  74. import com.vaadin.shared.communication.ServerRpc;
  75. import com.vaadin.shared.ui.Connect;
  76. import com.vaadin.shared.ui.Connect.LoadStyle;
  77. import com.vaadin.tools.CvalAddonsChecker;
  78. import com.vaadin.tools.CvalChecker;
  79. import com.vaadin.tools.CvalChecker.InvalidCvalException;
  80. public class ConnectorBundleLoaderFactory extends Generator {
  81. /**
  82. * Special SourceWriter that approximates the number of written bytes to
  83. * support splitting long methods into shorter chunks to avoid hitting the
  84. * 65535 byte limit.
  85. */
  86. private class SplittingSourceWriter implements SourceWriter {
  87. private final SourceWriter target;
  88. private final String baseName;
  89. private final int splitSize;
  90. private final List<String> methodNames;
  91. // Seems to be undercounted by about 15%
  92. private int approximateChars = 0;
  93. private int wrapCount = 0;
  94. public SplittingSourceWriter(SourceWriter target, String baseName,
  95. int splitSize) {
  96. this.target = target;
  97. this.baseName = baseName;
  98. this.splitSize = splitSize;
  99. methodNames = new ArrayList<String>();
  100. methodNames.add(baseName);
  101. }
  102. @Override
  103. public void beginJavaDocComment() {
  104. target.beginJavaDocComment();
  105. addChars(10);
  106. }
  107. private void addChars(int i) {
  108. approximateChars += i;
  109. }
  110. private void addChars(String s) {
  111. addChars(s.length());
  112. }
  113. private void addChars(String s, Object[] args) {
  114. addChars(String.format(s, args));
  115. }
  116. @Override
  117. public void commit(TreeLogger logger) {
  118. target.commit(logger);
  119. }
  120. @Override
  121. public void endJavaDocComment() {
  122. target.endJavaDocComment();
  123. addChars(10);
  124. }
  125. @Override
  126. public void indent() {
  127. target.indent();
  128. addChars(10);
  129. }
  130. @Override
  131. public void indentln(String s) {
  132. target.indentln(s);
  133. addChars(s);
  134. }
  135. @Override
  136. public void indentln(String s, Object... args) {
  137. target.indentln(s, args);
  138. addChars(s, args);
  139. }
  140. @Override
  141. public void outdent() {
  142. target.outdent();
  143. }
  144. @Override
  145. public void print(String s) {
  146. target.print(s);
  147. addChars(s);
  148. }
  149. @Override
  150. public void print(String s, Object... args) {
  151. target.print(s, args);
  152. addChars(s, args);
  153. }
  154. @Override
  155. public void println() {
  156. target.println();
  157. addChars(5);
  158. }
  159. @Override
  160. public void println(String s) {
  161. target.println(s);
  162. addChars(s);
  163. }
  164. @Override
  165. public void println(String s, Object... args) {
  166. target.println(s, args);
  167. addChars(s, args);
  168. }
  169. public void splitIfNeeded() {
  170. splitIfNeeded(false, null);
  171. }
  172. public void splitIfNeeded(boolean isNative, String params) {
  173. if (approximateChars > splitSize) {
  174. String newMethod = baseName + wrapCount++;
  175. String args = params == null ? "" : params;
  176. if (isNative) {
  177. outdent();
  178. println("}-*/;");
  179. // To support fields of type long (#13692)
  180. println("@com.google.gwt.core.client.UnsafeNativeLong");
  181. println("private native void %s(%s) /*-{", newMethod, args);
  182. } else {
  183. println("%s();", newMethod);
  184. outdent();
  185. println("}");
  186. println("private void %s(%s) {", newMethod, args);
  187. }
  188. methodNames.add(newMethod);
  189. indent();
  190. approximateChars = 0;
  191. }
  192. }
  193. public List<String> getMethodNames() {
  194. return Collections.unmodifiableList(methodNames);
  195. }
  196. }
  197. private CvalAddonsChecker cvalChecker = new CvalAddonsChecker();
  198. @Override
  199. public String generate(TreeLogger logger, GeneratorContext context,
  200. String typeName) throws UnableToCompleteException {
  201. TypeOracle typeOracle = context.getTypeOracle();
  202. try {
  203. JClassType classType = typeOracle.getType(typeName);
  204. String packageName = classType.getPackage().getName();
  205. String className = classType.getSimpleSourceName() + "Impl";
  206. generateClass(logger, context, packageName, className, typeName);
  207. return packageName + "." + className;
  208. } catch (UnableToCompleteException e) {
  209. // Just rethrow
  210. throw e;
  211. } catch (Exception e) {
  212. logger.log(Type.ERROR, getClass() + " failed", e);
  213. throw new UnableToCompleteException();
  214. }
  215. }
  216. private void generateClass(TreeLogger logger, GeneratorContext context,
  217. String packageName, String className, String requestedType)
  218. throws Exception {
  219. PrintWriter printWriter = context.tryCreate(logger, packageName,
  220. className);
  221. if (printWriter == null) {
  222. return;
  223. }
  224. List<CValUiInfo> cvalInfos = null;
  225. try {
  226. if (cvalChecker != null) {
  227. cvalInfos = cvalChecker.run();
  228. // Don't run twice
  229. cvalChecker = null;
  230. }
  231. } catch (InvalidCvalException e) {
  232. System.err.println("\n\n\n\n" + CvalChecker.LINE);
  233. for (String line : e.getMessage().split("\n")) {
  234. System.err.println(line);
  235. }
  236. System.err.println(CvalChecker.LINE + "\n\n\n\n");
  237. System.exit(1);
  238. throw new UnableToCompleteException();
  239. }
  240. List<ConnectorBundle> bundles = buildBundles(logger,
  241. context.getTypeOracle());
  242. ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(
  243. packageName, className);
  244. composer.setSuperclass(requestedType);
  245. SourceWriter w = composer.createSourceWriter(context, printWriter);
  246. w.println("public void init() {");
  247. w.indent();
  248. for (ConnectorBundle bundle : bundles) {
  249. detectBadProperties(bundle, logger);
  250. String name = bundle.getName();
  251. boolean isEager = name
  252. .equals(ConnectorBundleLoader.EAGER_BUNDLE_NAME);
  253. w.print("addAsyncBlockLoader(new AsyncBundleLoader(\"");
  254. w.print(escape(name));
  255. w.print("\", ");
  256. w.print("new String[] {");
  257. for (Entry<JClassType, Set<String>> entry : bundle.getIdentifiers()
  258. .entrySet()) {
  259. Set<String> identifiers = entry.getValue();
  260. for (String id : identifiers) {
  261. w.print("\"");
  262. w.print(escape(id));
  263. w.print("\",");
  264. }
  265. }
  266. w.println("}) {");
  267. w.indent();
  268. w.print("protected void load(final ");
  269. w.print(TypeDataStore.class.getName());
  270. w.println(" store) {");
  271. w.indent();
  272. if (!isEager) {
  273. w.print(GWT.class.getName());
  274. w.print(".runAsync(");
  275. }
  276. w.println("new %s() {", RunAsyncCallback.class.getName());
  277. w.indent();
  278. w.println("public void onSuccess() {");
  279. w.indent();
  280. w.println("load();");
  281. w.println("%s.get().setLoaded(getName());",
  282. ConnectorBundleLoader.class.getName());
  283. // Close onSuccess method
  284. w.outdent();
  285. w.println("}");
  286. w.println("private void load() {");
  287. w.indent();
  288. String loadNativeJsBundle = "loadJsBundle";
  289. printBundleData(logger, w, bundle, loadNativeJsBundle);
  290. // Close load method
  291. w.outdent();
  292. w.println("}");
  293. // Separate method for loading native JS stuff (e.g. callbacks)
  294. String loadNativeJsMethodName = "loadNativeJs";
  295. // To support fields of type long (#13692)
  296. w.println("@com.google.gwt.core.client.UnsafeNativeLong");
  297. w.println("private native void %s(%s store) /*-{",
  298. loadNativeJsMethodName, TypeDataStore.class.getName());
  299. w.indent();
  300. List<String> jsMethodNames = printJsBundleData(logger, w, bundle,
  301. loadNativeJsMethodName);
  302. w.outdent();
  303. w.println("}-*/;");
  304. // Call all generated native method inside one Java method to avoid
  305. // refercences inside native methods to each other
  306. w.println("private void %s(%s store) {", loadNativeJsBundle,
  307. TypeDataStore.class.getName());
  308. w.indent();
  309. printLoadJsBundleData(w, loadNativeJsBundle, jsMethodNames);
  310. w.outdent();
  311. w.println("}");
  312. // onFailure method declaration starts
  313. w.println("public void onFailure(Throwable reason) {");
  314. w.indent();
  315. w.println("%s.get().setLoadFailure(getName(), reason);",
  316. ConnectorBundleLoader.class.getName());
  317. w.outdent();
  318. w.println("}");
  319. // Close new RunAsyncCallback() {}
  320. w.outdent();
  321. w.print("}");
  322. if (isEager) {
  323. w.println(".onSuccess();");
  324. } else {
  325. w.println(");");
  326. }
  327. // Close load method
  328. w.outdent();
  329. w.println("}");
  330. // Close add(new ...
  331. w.outdent();
  332. w.println("});");
  333. }
  334. if (cvalInfos != null && !cvalInfos.isEmpty()) {
  335. w.println("{");
  336. for (CValUiInfo c : cvalInfos) {
  337. if ("evaluation".equals(c.type)) {
  338. w.println("cvals.add(new CValUiInfo(\"" + c.product
  339. + "\", \"" + c.version + "\", \"" + c.widgetset
  340. + "\", null));");
  341. }
  342. }
  343. w.println("}");
  344. }
  345. w.outdent();
  346. w.println("}");
  347. w.commit(logger);
  348. }
  349. private void printLoadJsBundleData(SourceWriter w, String methodName,
  350. List<String> methods) {
  351. SplittingSourceWriter writer = new SplittingSourceWriter(w, methodName,
  352. 30000);
  353. for (String method : methods) {
  354. writer.println("%s(store);", method);
  355. writer.splitIfNeeded();
  356. }
  357. }
  358. private void detectBadProperties(ConnectorBundle bundle, TreeLogger logger)
  359. throws UnableToCompleteException {
  360. Map<JClassType, Set<String>> definedProperties = new HashMap<JClassType, Set<String>>();
  361. for (Property property : bundle.getNeedsProperty()) {
  362. JClassType beanType = property.getBeanType();
  363. Set<String> usedPropertyNames = definedProperties.get(beanType);
  364. if (usedPropertyNames == null) {
  365. usedPropertyNames = new HashSet<String>();
  366. definedProperties.put(beanType, usedPropertyNames);
  367. }
  368. String name = property.getName();
  369. if (!usedPropertyNames.add(name)) {
  370. logger.log(Type.ERROR, beanType.getQualifiedSourceName()
  371. + " has multiple properties with the name " + name
  372. + ". This can happen if there are multiple "
  373. + "setters with identical names ignoring case.");
  374. throw new UnableToCompleteException();
  375. }
  376. if (!property.hasAccessorMethods()) {
  377. logger.log(Type.ERROR,
  378. beanType.getQualifiedSourceName()
  379. + " has the property '" + name
  380. + "' without getter defined.");
  381. throw new UnableToCompleteException();
  382. }
  383. }
  384. }
  385. private List<String> printJsBundleData(TreeLogger logger, SourceWriter w,
  386. ConnectorBundle bundle, String methodName) {
  387. SplittingSourceWriter writer = new SplittingSourceWriter(w, methodName,
  388. 30000);
  389. Set<Property> needsProperty = bundle.getNeedsProperty();
  390. for (Property property : needsProperty) {
  391. writer.println("var data = {");
  392. writer.indent();
  393. if (property.getAnnotation(NoLayout.class) != null) {
  394. writer.println("noLayout: 1, ");
  395. }
  396. writer.println("setter: function(bean, value) {");
  397. writer.indent();
  398. property.writeSetterBody(logger, writer, "bean", "value");
  399. writer.outdent();
  400. writer.println("},");
  401. writer.println("getter: function(bean) {");
  402. writer.indent();
  403. property.writeGetterBody(logger, writer, "bean");
  404. writer.outdent();
  405. writer.println("}");
  406. writer.outdent();
  407. writer.println("};");
  408. // Method declaration
  409. writer.print(
  410. "store.@%s::setPropertyData(Ljava/lang/Class;Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)",
  411. TypeDataStore.class.getName());
  412. writer.println("(@%s::class, '%s', data);",
  413. property.getBeanType().getQualifiedSourceName(),
  414. property.getName());
  415. writer.println();
  416. writer.splitIfNeeded(true,
  417. String.format("%s store", TypeDataStore.class.getName()));
  418. }
  419. return writer.getMethodNames();
  420. }
  421. private void printBundleData(TreeLogger logger, SourceWriter sourceWriter,
  422. ConnectorBundle bundle, String loadNativeJsMethodName)
  423. throws UnableToCompleteException {
  424. // Split into new load method when reaching approximately 30000 bytes
  425. SplittingSourceWriter w = new SplittingSourceWriter(sourceWriter,
  426. "load", 30000);
  427. writeSuperClasses(w, bundle);
  428. writeIdentifiers(w, bundle);
  429. writeGwtConstructors(w, bundle);
  430. writeReturnTypes(w, bundle);
  431. writeInvokers(logger, w, bundle);
  432. writeParamTypes(w, bundle);
  433. writeProxys(w, bundle);
  434. writeMethodAttributes(logger, w, bundle);
  435. w.println("%s(store);", loadNativeJsMethodName);
  436. // Must use Java code to generate Type data (because of Type[]), doing
  437. // this after the JS property data has been initialized
  438. writePropertyTypes(logger, w, bundle);
  439. writeSerializers(logger, w, bundle);
  440. writePresentationTypes(w, bundle);
  441. writeDelegateToWidget(logger, w, bundle);
  442. writeOnStateChangeHandlers(logger, w, bundle);
  443. }
  444. private void writeOnStateChangeHandlers(TreeLogger logger,
  445. SplittingSourceWriter w, ConnectorBundle bundle)
  446. throws UnableToCompleteException {
  447. Map<JClassType, Set<JMethod>> needsOnStateChangeHandler = bundle
  448. .getNeedsOnStateChangeHandler();
  449. for (Entry<JClassType, Set<JMethod>> entry : needsOnStateChangeHandler
  450. .entrySet()) {
  451. JClassType connector = entry.getKey();
  452. TreeLogger typeLogger = logger.branch(Type.DEBUG,
  453. "Generating @OnStateChange support for "
  454. + connector.getName());
  455. // Build map to speed up error checking
  456. HashMap<String, Property> stateProperties = new HashMap<String, Property>();
  457. JClassType stateType = ConnectorBundle
  458. .findInheritedMethod(connector, "getState").getReturnType()
  459. .isClassOrInterface();
  460. for (Property property : bundle.getProperties(stateType)) {
  461. stateProperties.put(property.getName(), property);
  462. }
  463. for (JMethod method : entry.getValue()) {
  464. TreeLogger methodLogger = typeLogger.branch(Type.DEBUG,
  465. "Processing method " + method.getName());
  466. if (method.isPublic() || method.isProtected()) {
  467. methodLogger.log(Type.ERROR,
  468. "@OnStateChange is only supported for methods with private or default visibility.");
  469. throw new UnableToCompleteException();
  470. }
  471. OnStateChange onStateChange = method
  472. .getAnnotation(OnStateChange.class);
  473. String[] properties = onStateChange.value();
  474. if (properties.length == 0) {
  475. methodLogger.log(Type.ERROR,
  476. "There are no properties to listen to");
  477. throw new UnableToCompleteException();
  478. }
  479. // Verify that all properties do exist
  480. for (String propertyName : properties) {
  481. if (!stateProperties.containsKey(propertyName)) {
  482. methodLogger.log(Type.ERROR,
  483. "State class has no property named "
  484. + propertyName);
  485. throw new UnableToCompleteException();
  486. }
  487. }
  488. if (method.getParameters().length != 0) {
  489. methodLogger.log(Type.ERROR,
  490. "Method should accept zero parameters");
  491. throw new UnableToCompleteException();
  492. }
  493. // new OnStateChangeMethod(Class declaringClass, String
  494. // methodName, String[], properties)
  495. w.print("store.addOnStateChangeMethod(%s, new %s(",
  496. getClassLiteralString(connector),
  497. OnStateChangeMethod.class.getName());
  498. if (!connector.equals(method.getEnclosingType())) {
  499. w.print("%s, ",
  500. getClassLiteralString(method.getEnclosingType()));
  501. }
  502. w.print("\"%s\", ", method.getName());
  503. w.print("new String[] {");
  504. for (String propertyName : properties) {
  505. w.print("\"%s\", ", propertyName);
  506. }
  507. w.print("}");
  508. w.println("));");
  509. w.splitIfNeeded();
  510. }
  511. }
  512. }
  513. private void writeSuperClasses(SplittingSourceWriter w,
  514. ConnectorBundle bundle) {
  515. List<JClassType> needsSuperclass = new ArrayList<JClassType>(
  516. bundle.getNeedsSuperclass());
  517. // Emit in hierarchy order to ensure superclass is defined when
  518. // referenced
  519. Collections.sort(needsSuperclass, new Comparator<JClassType>() {
  520. @Override
  521. public int compare(JClassType type1, JClassType type2) {
  522. int depthDiff = getDepth(type1) - getDepth(type2);
  523. if (depthDiff != 0) {
  524. return depthDiff;
  525. } else {
  526. // Just something to get a stable compare
  527. return type1.getName().compareTo(type2.getName());
  528. }
  529. }
  530. private int getDepth(JClassType type) {
  531. int depth = 0;
  532. while (type != null) {
  533. depth++;
  534. type = type.getSuperclass();
  535. }
  536. return depth;
  537. }
  538. });
  539. for (JClassType jClassType : needsSuperclass) {
  540. JClassType superclass = jClassType.getSuperclass();
  541. while (superclass != null && !superclass.isPublic()) {
  542. superclass = superclass.getSuperclass();
  543. }
  544. String classLiteralString;
  545. if (superclass == null) {
  546. classLiteralString = "null";
  547. } else {
  548. classLiteralString = getClassLiteralString(superclass);
  549. }
  550. w.println("store.setSuperClass(%s, %s);",
  551. getClassLiteralString(jClassType), classLiteralString);
  552. }
  553. }
  554. private void writeDelegateToWidget(TreeLogger logger,
  555. SplittingSourceWriter w, ConnectorBundle bundle) {
  556. Map<JClassType, Set<Property>> needsDelegateToWidget = bundle
  557. .getNeedsDelegateToWidget();
  558. for (Entry<JClassType, Set<Property>> entry : needsDelegateToWidget
  559. .entrySet()) {
  560. JClassType beanType = entry.getKey();
  561. for (Property property : entry.getValue()) {
  562. w.println("store.setDelegateToWidget(%s, \"%s\", \"%s\");",
  563. getClassLiteralString(beanType), // property.getBeanType()),
  564. property.getName(),
  565. property.getAnnotation(DelegateToWidget.class).value());
  566. }
  567. w.splitIfNeeded();
  568. }
  569. }
  570. private void writeSerializers(TreeLogger logger, SplittingSourceWriter w,
  571. ConnectorBundle bundle) throws UnableToCompleteException {
  572. Map<JType, GeneratedSerializer> serializers = bundle.getSerializers();
  573. for (Entry<JType, GeneratedSerializer> entry : serializers.entrySet()) {
  574. JType type = entry.getKey();
  575. GeneratedSerializer serializer = entry.getValue();
  576. w.print("store.setSerializerFactory(");
  577. writeClassLiteral(w, type);
  578. w.print(", ");
  579. w.println("new Invoker() {");
  580. w.indent();
  581. w.println("public Object invoke(Object target, Object[] params) {");
  582. w.indent();
  583. serializer.writeSerializerInstantiator(logger, w);
  584. w.outdent();
  585. w.println("}");
  586. w.outdent();
  587. w.print("}");
  588. w.println(");");
  589. w.splitIfNeeded();
  590. }
  591. }
  592. private void writePresentationTypes(SplittingSourceWriter w,
  593. ConnectorBundle bundle) {
  594. Map<JClassType, JType> presentationTypes = bundle
  595. .getPresentationTypes();
  596. for (Entry<JClassType, JType> entry : presentationTypes.entrySet()) {
  597. w.print("store.setPresentationType(");
  598. writeClassLiteral(w, entry.getKey());
  599. w.print(", ");
  600. writeClassLiteral(w, entry.getValue());
  601. w.println(");");
  602. w.splitIfNeeded();
  603. }
  604. }
  605. private void writePropertyTypes(TreeLogger logger, SplittingSourceWriter w,
  606. ConnectorBundle bundle) {
  607. Set<Property> properties = bundle.getNeedsProperty();
  608. for (Property property : properties) {
  609. w.print("store.setPropertyType(");
  610. writeClassLiteral(w, property.getBeanType());
  611. w.print(", \"");
  612. w.print(escape(property.getName()));
  613. w.print("\", ");
  614. writeTypeCreator(w, property.getPropertyType());
  615. w.println(");");
  616. w.splitIfNeeded();
  617. }
  618. }
  619. private void writeMethodAttributes(TreeLogger logger,
  620. SplittingSourceWriter w, ConnectorBundle bundle) {
  621. for (Entry<JClassType, Map<JMethod, Set<MethodAttribute>>> typeEntry : bundle
  622. .getMethodAttributes().entrySet()) {
  623. JClassType type = typeEntry.getKey();
  624. for (Entry<JMethod, Set<MethodAttribute>> methodEntry : typeEntry
  625. .getValue().entrySet()) {
  626. JMethod method = methodEntry.getKey();
  627. Set<MethodAttribute> attributes = methodEntry.getValue();
  628. for (MethodAttribute attribute : attributes) {
  629. w.println("store.setMethodAttribute(%s, \"%s\", %s.%s);",
  630. getClassLiteralString(type), method.getName(),
  631. MethodAttribute.class.getCanonicalName(),
  632. attribute.name());
  633. w.splitIfNeeded();
  634. }
  635. }
  636. }
  637. }
  638. private void writeProxys(SplittingSourceWriter w, ConnectorBundle bundle) {
  639. Set<JClassType> needsProxySupport = bundle.getNeedsProxySupport();
  640. for (JClassType type : needsProxySupport) {
  641. w.print("store.setProxyHandler(");
  642. writeClassLiteral(w, type);
  643. w.print(", new ");
  644. w.print(ProxyHandler.class.getCanonicalName());
  645. w.println("() {");
  646. w.indent();
  647. w.println("public Object createProxy(final "
  648. + InvokationHandler.class.getName() + " handler) {");
  649. w.indent();
  650. w.print("return new ");
  651. w.print(type.getQualifiedSourceName());
  652. w.println("() {");
  653. w.indent();
  654. JMethod[] methods = type.getOverridableMethods();
  655. for (JMethod method : methods) {
  656. if (method.isAbstract()) {
  657. w.print("public ");
  658. w.print(method.getReturnType().getQualifiedSourceName());
  659. w.print(" ");
  660. w.print(method.getName());
  661. w.print("(");
  662. JType[] types = method.getParameterTypes();
  663. for (int i = 0; i < types.length; i++) {
  664. if (i != 0) {
  665. w.print(", ");
  666. }
  667. w.print(types[i].getQualifiedSourceName());
  668. w.print(" p");
  669. w.print(Integer.toString(i));
  670. }
  671. w.println(") {");
  672. w.indent();
  673. if (!method.getReturnType().getQualifiedSourceName()
  674. .equals("void")) {
  675. w.print("return ");
  676. }
  677. w.print("handler.invoke(this, ");
  678. w.print(TypeData.class.getCanonicalName());
  679. w.print(".getType(");
  680. writeClassLiteral(w, type);
  681. w.print(").getMethod(\"");
  682. w.print(escape(method.getName()));
  683. w.print("\"), new Object [] {");
  684. for (int i = 0; i < types.length; i++) {
  685. w.print("p" + i + ", ");
  686. }
  687. w.println("});");
  688. w.outdent();
  689. w.println("}");
  690. }
  691. }
  692. w.outdent();
  693. w.println("};");
  694. w.outdent();
  695. w.println("}");
  696. w.outdent();
  697. w.println("});");
  698. w.splitIfNeeded();
  699. }
  700. }
  701. private void writeParamTypes(SplittingSourceWriter w,
  702. ConnectorBundle bundle) {
  703. Map<JClassType, Set<JMethod>> needsParamTypes = bundle
  704. .getNeedsParamTypes();
  705. for (Entry<JClassType, Set<JMethod>> entry : needsParamTypes
  706. .entrySet()) {
  707. JClassType type = entry.getKey();
  708. Set<JMethod> methods = entry.getValue();
  709. for (JMethod method : methods) {
  710. w.print("store.setParamTypes(");
  711. writeClassLiteral(w, type);
  712. w.print(", \"");
  713. w.print(escape(method.getName()));
  714. w.print("\", new Type[] {");
  715. for (JType parameter : method.getParameterTypes()) {
  716. ConnectorBundleLoaderFactory.writeTypeCreator(w, parameter);
  717. w.print(", ");
  718. }
  719. w.println("});");
  720. w.splitIfNeeded();
  721. }
  722. }
  723. }
  724. private void writeInvokers(TreeLogger logger, SplittingSourceWriter w,
  725. ConnectorBundle bundle) throws UnableToCompleteException {
  726. Map<JClassType, Set<JMethod>> needsInvoker = bundle.getNeedsInvoker();
  727. for (Entry<JClassType, Set<JMethod>> entry : needsInvoker.entrySet()) {
  728. JClassType type = entry.getKey();
  729. TreeLogger typeLogger = logger.branch(Type.DEBUG,
  730. "Creating invokers for " + type);
  731. Set<JMethod> methods = entry.getValue();
  732. for (JMethod method : methods) {
  733. w.print("store.setInvoker(");
  734. writeClassLiteral(w, type);
  735. w.print(", \"");
  736. w.print(escape(method.getName()));
  737. w.print("\",");
  738. if (method.isPublic()) {
  739. typeLogger.log(Type.DEBUG,
  740. "Invoking " + method.getName() + " using java");
  741. writeJavaInvoker(w, type, method);
  742. } else {
  743. TreeLogger methodLogger = typeLogger.branch(Type.DEBUG,
  744. "Invoking " + method.getName() + " using jsni");
  745. // Must use JSNI to access non-public methods
  746. writeJsniInvoker(methodLogger, w, type, method);
  747. }
  748. w.println(");");
  749. w.splitIfNeeded();
  750. }
  751. }
  752. }
  753. private void writeJsniInvoker(TreeLogger logger, SplittingSourceWriter w,
  754. JClassType type, JMethod method) throws UnableToCompleteException {
  755. w.println("new JsniInvoker() {");
  756. w.indent();
  757. w.println(
  758. "protected native Object jsniInvoke(Object target, %s<Object> params) /*-{ ",
  759. JsArrayObject.class.getName());
  760. w.indent();
  761. JType returnType = method.getReturnType();
  762. boolean hasReturnType = !"void"
  763. .equals(returnType.getQualifiedSourceName());
  764. // Note that void is also a primitive type
  765. boolean hasPrimitiveReturnType = hasReturnType
  766. && returnType.isPrimitive() != null;
  767. if (hasReturnType) {
  768. w.print("return ");
  769. if (hasPrimitiveReturnType) {
  770. // Integer.valueOf(expression);
  771. w.print("@%s::valueOf(%s)(",
  772. returnType.isPrimitive().getQualifiedBoxedSourceName(),
  773. returnType.getJNISignature());
  774. // Implementation tested briefly, but I don't dare leave it
  775. // enabled since we are not using it in the framework and we
  776. // have not tests for it.
  777. logger.log(Type.ERROR,
  778. "JSNI invocation is not yet supported for methods with "
  779. + "primitive return types. Change your method "
  780. + "to public to be able to use conventional"
  781. + " Java invoking instead.");
  782. throw new UnableToCompleteException();
  783. }
  784. }
  785. JType[] parameterTypes = method.getParameterTypes();
  786. w.print("target.@%s::" + method.getName() + "(*)(",
  787. method.getEnclosingType().getQualifiedSourceName());
  788. for (int i = 0; i < parameterTypes.length; i++) {
  789. if (i != 0) {
  790. w.print(", ");
  791. }
  792. w.print("params[" + i + "]");
  793. JPrimitiveType primitive = parameterTypes[i].isPrimitive();
  794. if (primitive != null) {
  795. // param.intValue();
  796. w.print(".@%s::%sValue()()",
  797. primitive.getQualifiedBoxedSourceName(),
  798. primitive.getQualifiedSourceName());
  799. }
  800. }
  801. if (hasPrimitiveReturnType) {
  802. assert hasReturnType;
  803. w.print(")");
  804. }
  805. w.println(");");
  806. if (!hasReturnType) {
  807. w.println("return null;");
  808. }
  809. w.outdent();
  810. w.println("}-*/;");
  811. w.outdent();
  812. w.print("}");
  813. }
  814. private void writeJavaInvoker(SplittingSourceWriter w, JClassType type,
  815. JMethod method) {
  816. w.println("new Invoker() {");
  817. w.indent();
  818. w.println("public Object invoke(Object target, Object[] params) {");
  819. w.indent();
  820. JType returnType = method.getReturnType();
  821. boolean hasReturnType = !"void"
  822. .equals(returnType.getQualifiedSourceName());
  823. if (hasReturnType) {
  824. w.print("return ");
  825. }
  826. JType[] parameterTypes = method.getParameterTypes();
  827. w.print("((" + type.getQualifiedSourceName() + ") target)."
  828. + method.getName() + "(");
  829. for (int i = 0; i < parameterTypes.length; i++) {
  830. JType parameterType = parameterTypes[i];
  831. if (i != 0) {
  832. w.print(", ");
  833. }
  834. String parameterTypeName = getBoxedTypeName(parameterType);
  835. if (parameterTypeName.startsWith("elemental.json.Json")) {
  836. // Need to pass through native method to allow casting Object to
  837. // JSO if the value is a string
  838. w.print("%s.<%s>obj2jso(params[%d])",
  839. JsonDecoder.class.getCanonicalName(), parameterTypeName,
  840. i);
  841. } else {
  842. w.print("(" + parameterTypeName + ") params[" + i + "]");
  843. }
  844. }
  845. w.println(");");
  846. if (!hasReturnType) {
  847. w.println("return null;");
  848. }
  849. w.outdent();
  850. w.println("}");
  851. w.outdent();
  852. w.print("}");
  853. }
  854. private void writeReturnTypes(SplittingSourceWriter w,
  855. ConnectorBundle bundle) {
  856. Map<JClassType, Set<JMethod>> methodReturnTypes = bundle
  857. .getMethodReturnTypes();
  858. for (Entry<JClassType, Set<JMethod>> entry : methodReturnTypes
  859. .entrySet()) {
  860. JClassType type = entry.getKey();
  861. Set<JMethod> methods = entry.getValue();
  862. for (JMethod method : methods) {
  863. // setReturnType(Class<?> type, String methodName, Type
  864. // returnType)
  865. w.print("store.setReturnType(");
  866. writeClassLiteral(w, type);
  867. w.print(", \"");
  868. w.print(escape(method.getName()));
  869. w.print("\", ");
  870. writeTypeCreator(w, method.getReturnType());
  871. w.println(");");
  872. w.splitIfNeeded();
  873. }
  874. }
  875. }
  876. private void writeGwtConstructors(SplittingSourceWriter w,
  877. ConnectorBundle bundle) {
  878. Set<JClassType> constructors = bundle.getGwtConstructors();
  879. for (JClassType type : constructors) {
  880. w.print("store.setConstructor(");
  881. writeClassLiteral(w, type);
  882. w.println(", new Invoker() {");
  883. w.indent();
  884. w.println("public Object invoke(Object target, Object[] params) {");
  885. w.indent();
  886. w.print("return ");
  887. w.print(GWT.class.getName());
  888. w.print(".create(");
  889. writeClassLiteral(w, type);
  890. w.println(");");
  891. w.outdent();
  892. w.println("}");
  893. w.outdent();
  894. w.println("});");
  895. w.splitIfNeeded();
  896. }
  897. }
  898. public static void writeClassLiteral(SourceWriter w, JType type) {
  899. w.print(getClassLiteralString(type));
  900. }
  901. public static String getClassLiteralString(JType type) {
  902. return type.getQualifiedSourceName() + ".class";
  903. }
  904. private void writeIdentifiers(SplittingSourceWriter w,
  905. ConnectorBundle bundle) {
  906. Map<JClassType, Set<String>> identifiers = bundle.getIdentifiers();
  907. for (Entry<JClassType, Set<String>> entry : identifiers.entrySet()) {
  908. Set<String> ids = entry.getValue();
  909. JClassType type = entry.getKey();
  910. for (String id : ids) {
  911. w.print("store.setClass(\"");
  912. w.print(escape(id));
  913. w.print("\", ");
  914. writeClassLiteral(w, type);
  915. w.println(");");
  916. w.splitIfNeeded();
  917. }
  918. }
  919. }
  920. private List<ConnectorBundle> buildBundles(TreeLogger logger,
  921. TypeOracle typeOracle)
  922. throws NotFoundException, UnableToCompleteException {
  923. Map<LoadStyle, Collection<JClassType>> connectorsByLoadStyle = new HashMap<LoadStyle, Collection<JClassType>>();
  924. for (LoadStyle loadStyle : LoadStyle.values()) {
  925. connectorsByLoadStyle.put(loadStyle, new ArrayList<JClassType>());
  926. }
  927. // Find all types with a valid mapping
  928. Collection<JClassType> selectedTypes = getConnectorsForWidgetset(logger,
  929. typeOracle);
  930. // Group by load style
  931. for (JClassType connectorSubtype : selectedTypes) {
  932. LoadStyle loadStyle = getLoadStyle(connectorSubtype);
  933. if (loadStyle != null) {
  934. connectorsByLoadStyle.get(loadStyle).add(connectorSubtype);
  935. }
  936. }
  937. List<ConnectorBundle> bundles = new ArrayList<ConnectorBundle>();
  938. Collection<TypeVisitor> visitors = getVisitors(typeOracle);
  939. ConnectorBundle eagerBundle = new ConnectorBundle(
  940. ConnectorBundleLoader.EAGER_BUNDLE_NAME, visitors, typeOracle);
  941. TreeLogger eagerLogger = logger.branch(Type.TRACE,
  942. "Populating eager bundle");
  943. // Eager connectors and all RPC interfaces are loaded by default
  944. eagerBundle.processTypes(eagerLogger,
  945. connectorsByLoadStyle.get(LoadStyle.EAGER));
  946. eagerBundle.processType(eagerLogger, typeOracle
  947. .findType(UnknownComponentConnector.class.getCanonicalName()));
  948. eagerBundle.processType(eagerLogger, typeOracle
  949. .findType(UnknownExtensionConnector.class.getCanonicalName()));
  950. eagerBundle.processSubTypes(eagerLogger,
  951. typeOracle.getType(ClientRpc.class.getName()));
  952. eagerBundle.processSubTypes(eagerLogger,
  953. typeOracle.getType(ServerRpc.class.getName()));
  954. bundles.add(eagerBundle);
  955. ConnectorBundle deferredBundle = new ConnectorBundle(
  956. ConnectorBundleLoader.DEFERRED_BUNDLE_NAME, eagerBundle);
  957. TreeLogger deferredLogger = logger.branch(Type.TRACE,
  958. "Populating deferred bundle");
  959. deferredBundle.processTypes(deferredLogger,
  960. connectorsByLoadStyle.get(LoadStyle.DEFERRED));
  961. bundles.add(deferredBundle);
  962. Collection<JClassType> lazy = connectorsByLoadStyle.get(LoadStyle.LAZY);
  963. for (JClassType type : lazy) {
  964. ConnectorBundle bundle = new ConnectorBundle(type.getName(),
  965. eagerBundle);
  966. TreeLogger subLogger = logger.branch(Type.TRACE,
  967. "Populating " + type.getName() + " bundle");
  968. bundle.processType(subLogger, type);
  969. bundles.add(bundle);
  970. }
  971. return bundles;
  972. }
  973. /**
  974. * Returns the connector types that should be included in the widgetset.
  975. * This method can be overridden to create a widgetset only containing
  976. * selected connectors.
  977. * <p>
  978. * The default implementation finds all type implementing
  979. * {@link ServerConnector} that have a @{@link Connect} annotation. It also
  980. * checks that multiple connectors aren't connected to the same server-side
  981. * class.
  982. *
  983. * @param logger
  984. * the logger to which information can be logged
  985. * @param typeOracle
  986. * the type oracle that can be used for finding types
  987. * @return a collection of all the connector types that should be included
  988. * in the widgetset
  989. * @throws UnableToCompleteException
  990. * if the operation fails
  991. */
  992. protected Collection<JClassType> getConnectorsForWidgetset(
  993. TreeLogger logger, TypeOracle typeOracle)
  994. throws UnableToCompleteException {
  995. JClassType serverConnectorType;
  996. try {
  997. serverConnectorType = typeOracle
  998. .getType(ServerConnector.class.getName());
  999. } catch (NotFoundException e) {
  1000. logger.log(Type.ERROR,
  1001. "Can't find " + ServerConnector.class.getName());
  1002. throw new UnableToCompleteException();
  1003. }
  1004. JClassType[] types = serverConnectorType.getSubtypes();
  1005. Map<String, JClassType> mappings = new TreeMap<String, JClassType>();
  1006. // Keep track of what has happened to avoid logging intermediate state
  1007. Map<JClassType, List<JClassType>> replaced = new TreeMap<JClassType, List<JClassType>>(
  1008. ConnectorBundle.jClassComparator);
  1009. for (JClassType type : types) {
  1010. Connect connectAnnotation = type.getAnnotation(Connect.class);
  1011. if (connectAnnotation == null) {
  1012. continue;
  1013. }
  1014. String identifier = connectAnnotation.value().getCanonicalName();
  1015. JClassType previousMapping = mappings.put(identifier, type);
  1016. if (previousMapping != null) {
  1017. // There are multiple mappings, pick the subclass
  1018. JClassType subclass;
  1019. JClassType superclass;
  1020. if (previousMapping.isAssignableFrom(type)) {
  1021. subclass = type;
  1022. superclass = previousMapping;
  1023. } else if (type.isAssignableFrom(previousMapping)) {
  1024. subclass = previousMapping;
  1025. superclass = type;
  1026. } else {
  1027. // Neither inherits from the other - this is a conflict
  1028. logger.log(Type.ERROR,
  1029. "Conflicting @Connect mappings detected for "
  1030. + identifier + ": "
  1031. + type.getQualifiedSourceName() + " and "
  1032. + previousMapping.getQualifiedSourceName()
  1033. + ". There can only be multiple @Connect mappings for the same server-side type if one is the subclass of the other.");
  1034. throw new UnableToCompleteException();
  1035. }
  1036. mappings.put(identifier, subclass);
  1037. // Inherit any previous replacements
  1038. List<JClassType> previousReplacements = replaced
  1039. .remove(superclass);
  1040. if (previousReplacements == null) {
  1041. previousReplacements = new ArrayList<JClassType>();
  1042. }
  1043. previousReplacements.add(superclass);
  1044. replaced.put(subclass, previousReplacements);
  1045. }
  1046. }
  1047. // Log the final set of replacements
  1048. for (Entry<JClassType, List<JClassType>> entry : replaced.entrySet()) {
  1049. String msg = entry.getKey().getQualifiedSourceName() + " replaces ";
  1050. List<JClassType> list = entry.getValue();
  1051. for (int i = 0; i < list.size(); i++) {
  1052. if (i != 0) {
  1053. msg += ", ";
  1054. }
  1055. msg += list.get(i).getQualifiedSourceName();
  1056. }
  1057. logger.log(Type.INFO, msg);
  1058. }
  1059. // Return the types of the final mapping
  1060. return mappings.values();
  1061. }
  1062. private Collection<TypeVisitor> getVisitors(TypeOracle oracle)
  1063. throws NotFoundException {
  1064. List<TypeVisitor> visitors = Arrays.<TypeVisitor> asList(
  1065. new ConnectorInitVisitor(), new StateInitVisitor(),
  1066. new WidgetInitVisitor(), new RendererVisitor(),
  1067. new ClientRpcVisitor(), new ServerRpcVisitor(),
  1068. new OnStateChangeVisitor());
  1069. for (TypeVisitor typeVisitor : visitors) {
  1070. typeVisitor.init(oracle);
  1071. }
  1072. return visitors;
  1073. }
  1074. protected LoadStyle getLoadStyle(JClassType connectorType) {
  1075. Connect annotation = connectorType.getAnnotation(Connect.class);
  1076. return annotation.loadStyle();
  1077. }
  1078. public static String getBoxedTypeName(JType type) {
  1079. if (type.isPrimitive() != null) {
  1080. // Used boxed types for primitives
  1081. return type.isPrimitive().getQualifiedBoxedSourceName();
  1082. } else {
  1083. return type.getErasedType().getQualifiedSourceName();
  1084. }
  1085. }
  1086. public static void writeTypeCreator(SourceWriter sourceWriter, JType type) {
  1087. String typeName = ConnectorBundleLoaderFactory.getBoxedTypeName(type);
  1088. JParameterizedType parameterized = type.isParameterized();
  1089. if (parameterized != null) {
  1090. sourceWriter.print("new Type(\"" + typeName + "\", ");
  1091. sourceWriter.print("new Type[] {");
  1092. JClassType[] typeArgs = parameterized.getTypeArgs();
  1093. for (JClassType jClassType : typeArgs) {
  1094. writeTypeCreator(sourceWriter, jClassType);
  1095. sourceWriter.print(", ");
  1096. }
  1097. sourceWriter.print("}");
  1098. } else {
  1099. sourceWriter.print("new Type(" + typeName + ".class");
  1100. }
  1101. sourceWriter.print(")");
  1102. }
  1103. }