您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ConnectorBundleLoaderFactory.java 48KB

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