List of usage examples for com.google.gwt.editor.rebind.model ModelUtils findParameterizationOf
public static JClassType[] findParameterizationOf(JClassType intfType, JClassType subType)
From source file:com.colinalworth.celltable.columns.rebind.model.ColumnSetModel.java
License:Apache License
private JClassType getBeanType(JClassType toGenerate) { JClassType columnsInterface = context.getTypeOracle().findType(Name.getSourceNameForClass(Columns.class)); JClassType[] params = ModelUtils.findParameterizationOf(columnsInterface, toGenerate); assert params.length == 1 : "Too many generic params in Column<T>"; return params[0]; }
From source file:com.colinalworth.celltable.columns.rebind.model.ColumnSetModel.java
License:Apache License
/** * Gets the type of factory this class needs to do its thing. Returns null if no factory is * required./* w w w . j a va 2 s . c om*/ * @param toGenerate * @return */ private JClassType getFactoryType(JClassType toGenerate) { JClassType columnsWithFactoryInterface = context.getTypeOracle() .findType(Name.getSourceNameForClass(ColumnsWithFactory.class)); JClassType[] params = ModelUtils.findParameterizationOf(columnsWithFactoryInterface, toGenerate); if (params != null) { assert params.length == 2 : "Exactly two generic params needed for ColumnsWithFactory<T, F>"; return params[1]; } return null; }
From source file:com.colinalworth.gwt.websockets.rebind.ServerBuilderGenerator.java
License:Apache License
@Override public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException { TypeOracle oracle = context.getTypeOracle(); JClassType toGenerate = oracle.findType(typeName).isInterface(); if (toGenerate == null) { logger.log(Type.ERROR, "Error generating " + typeName + ", either not an interface, or cannot be reached from client code."); throw new UnableToCompleteException(); }//ww w . ja va 2 s . c o m JClassType serverBuilderType = oracle.findType(ServerBuilder.class.getName()); JClassType serverImplType = ModelUtils.findParameterizationOf(serverBuilderType, toGenerate)[0]; // Build an impl so we can call it ourselves ServerCreator creator = new ServerCreator(serverImplType); creator.create(logger, context); String packageName = toGenerate.getPackage().getName(); String simpleName = toGenerate.getName().replace('.', '_') + "_Impl"; PrintWriter pw = context.tryCreate(logger, packageName, simpleName); if (pw == null) { return packageName + "." + simpleName; } ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName, simpleName); factory.setSuperclass(Name.getSourceNameForClass(ServerBuilderImpl.class) + "<" + serverImplType.getQualifiedSourceName() + ">"); factory.addImplementedInterface(typeName); SourceWriter sw = factory.createSourceWriter(context, pw); RemoteServiceRelativePath path = serverImplType.getAnnotation(RemoteServiceRelativePath.class); if (path != null) { sw.println("public %1$s() {", simpleName); sw.indentln("setPath(\"%1$s\");", path.value()); sw.println("}"); } sw.println(); // start method sw.println("public %1$s start() {", serverImplType.getQualifiedSourceName()); sw.indent(); sw.println("String url = getUrl();"); sw.println("if (url == null) {"); sw.indentln("return new %1$s(getErrorHandler());", creator.getQualifiedSourceName()); sw.println("} else {"); sw.indentln("return new %1$s(getErrorHandler(), url);", creator.getQualifiedSourceName()); sw.println("}"); sw.outdent(); sw.println("}"); sw.commit(logger); return factory.getCreatedClassName(); }
From source file:com.colinalworth.gwt.websockets.rebind.ServerCreator.java
License:Apache License
public void create(TreeLogger logger, GeneratorContext context) throws UnableToCompleteException { String typeName = this.serverType.getQualifiedSourceName(); String packageName = getPackageName(); String simpleName = getSimpleName(); TypeOracle oracle = context.getTypeOracle(); PrintWriter pw = context.tryCreate(logger, packageName, simpleName); if (pw == null) { return;// w w w.j av a2 s. com } JClassType serverType = oracle.findType(Name.getSourceNameForClass(Server.class)); JClassType clientType = ModelUtils.findParameterizationOf(serverType, this.serverType)[1]; ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName, simpleName); factory.setSuperclass(Name.getSourceNameForClass(ServerImpl.class) + "<" + typeName + "," + clientType.getQualifiedSourceName() + ">"); factory.addImplementedInterface(typeName); SourceWriter sw = factory.createSourceWriter(context, pw); //TODO move this check before the printwriter creation can fail, and allow the warn to be optional sw.println("public %1$s(%2$s errorHandler) {", simpleName, Name.getSourceNameForClass(ServerBuilder.ConnectionErrorHandler.class)); RemoteServiceRelativePath path = this.serverType.getAnnotation(RemoteServiceRelativePath.class); if (path == null) { // logger.log(Type.WARN, "@RemoteServiceRelativePath required on " + typeName + " to make a connection to the server without a ServerBuilder"); // throw new UnableToCompleteException(); sw.indentln("super(null);"); sw.indentln( "throw new RuntimeException(\"@RemoteServiceRelativePath annotation required on %1$s to make a connection without a path defined in ServerBuilder\");"); } else { sw.indentln("super(errorHandler, " + "com.google.gwt.user.client.Window.Location.getProtocol().toLowerCase().startsWith(\"https\") ? \"wss://\": \"ws://\", " + "com.google.gwt.user.client.Window.Location.getHost(), \"%1$s\");", path.value()); } sw.println("}"); sw.println("public %1$s(%2$s errorHandler, String url) {", simpleName, Name.getSourceNameForClass(ServerBuilder.ConnectionErrorHandler.class)); sw.indentln("super(errorHandler, url);"); sw.println("}"); //Find all types that may go over the wire // Collect the types the server will send to the client using the Client interface SerializableTypeOracleBuilder serverSerializerBuilder = new SerializableTypeOracleBuilder(logger, context); appendMethodParameters(logger, clientType, Client.class, serverSerializerBuilder); // Also add the wrapper object ClientInvocation serverSerializerBuilder.addRootType(logger, oracle.findType(ClientInvocation.class.getName())); serverSerializerBuilder.addRootType(logger, oracle.findType(ClientCallbackInvocation.class.getName())); // Collect the types the client will send to the server using the Server interface SerializableTypeOracleBuilder clientSerializerBuilder = new SerializableTypeOracleBuilder(logger, context); appendMethodParameters(logger, this.serverType, Server.class, clientSerializerBuilder); // Also add the ServerInvocation wrapper clientSerializerBuilder.addRootType(logger, oracle.findType(ServerInvocation.class.getName())); clientSerializerBuilder.addRootType(logger, oracle.findType(ServerCallbackInvocation.class.getName())); String tsName = simpleName + "_TypeSerializer"; TypeSerializerCreator serializerCreator = new TypeSerializerCreator(logger, clientSerializerBuilder.build(logger), serverSerializerBuilder.build(logger), context, packageName + "." + tsName, tsName); serializerCreator.realize(logger); // Make the newly created Serializer available at runtime sw.println("protected %1$s __getSerializer() {", Serializer.class.getName()); sw.indentln("return %2$s.<%1$s>create(%1$s.class);", tsName, GWT.class.getName()); sw.println("}"); // Build methods that call from the client to the server for (JMethod m : this.serverType.getInheritableMethods()) { if (isRemoteMethod(m, Server.class)) { printServerMethodBody(logger, context, sw, m); } } // Read incoming calls and dispatch them to the correct client method sw.println("protected void __invoke(String method, Object[] params) {"); for (JMethod m : clientType.getInheritableMethods()) { if (isRemoteMethod(m, Client.class)) { JParameter[] params = m.getParameters(); sw.println("if (method.equals(\"%1$s\") && params.length == %2$d) {", m.getName(), params.length); sw.indent(); sw.println("getClient().%1$s(", m.getName()); sw.indent(); for (int i = 0; i < params.length; i++) { if (i != 0) { sw.print(","); } sw.println("(%1$s)params[%2$d]", params[i].getType().getQualifiedSourceName(), i); } sw.outdent(); sw.println(");"); sw.outdent(); sw.println("}"); } } sw.println("}"); sw.println("protected void __onError(Exception error) {"); sw.println("}"); sw.commit(logger); }
From source file:com.gafactory.core.rebind.LabelProviderCreator.java
License:sencha.com license
@Override protected JClassType getObjectType() { JClassType[] params = ModelUtils.findParameterizationOf(labelProviderInterface, getSupertype()); return params[0]; }
From source file:com.gafactory.core.rebind.ModelKeyProviderCreator.java
License:sencha.com license
@Override protected JClassType getObjectType() { JClassType[] params = ModelUtils.findParameterizationOf(modelKeyProviderInterface, getSupertype()); return params[0]; }
From source file:com.gafactory.core.rebind.ValueProviderCreator.java
License:sencha.com license
/** * Gets the type of the Model this object should provide data for. * /* ww w .j a v a 2s .co m*/ * @return the parameterized type of the model */ protected JClassType getObjectType() { JClassType[] params = ModelUtils.findParameterizationOf(valueProviderInterface, supertypeToImplement); if (params[0].isTypeParameter() != null) { return params[0].isTypeParameter().getBaseType(); } else { return params[0]; } }
From source file:com.gafactory.core.rebind.ValueProviderCreator.java
License:sencha.com license
private JClassType getValueType() { JClassType[] params = ModelUtils.findParameterizationOf(valueProviderInterface, supertypeToImplement); return params[1]; }
From source file:com.google.web.bindery.autobean.gwt.rebind.model.AutoBeanFactoryModel.java
License:Apache License
public AutoBeanFactoryModel(TreeLogger logger, JClassType factoryType) throws UnableToCompleteException { this.logger = logger; oracle = factoryType.getOracle();/* w ww. ja v a 2 s . co m*/ autoBeanInterface = oracle.findType(AutoBean.class.getCanonicalName()).isGenericType(); autoBeanFactoryInterface = oracle.findType(AutoBeanFactory.class.getCanonicalName()).isInterface(); /* * We want to allow the user to override some of the useful Object methods, * so we'll extract them here. */ JClassType objectType = oracle.getJavaLangObject(); objectMethods = Arrays.asList(objectType.findMethod("equals", new JType[] { objectType }), objectType.findMethod("hashCode", EMPTY_JTYPE), objectType.findMethod("toString", EMPTY_JTYPE)); // Process annotations { Category categoryAnnotation = factoryType.getAnnotation(Category.class); if (categoryAnnotation != null) { categoryTypes = new ArrayList<JClassType>(categoryAnnotation.value().length); processClassArrayAnnotation(categoryAnnotation.value(), categoryTypes); } else { categoryTypes = null; } noWrapTypes = new ArrayList<JClassType>(); noWrapTypes.add(oracle.findType(AutoBean.class.getCanonicalName())); NoWrap noWrapAnnotation = factoryType.getAnnotation(NoWrap.class); if (noWrapAnnotation != null) { processClassArrayAnnotation(noWrapAnnotation.value(), noWrapTypes); } ExtraEnums extraEnumsAnnotation = factoryType.getAnnotation(ExtraEnums.class); if (extraEnumsAnnotation != null) { for (Class<?> clazz : extraEnumsAnnotation.value()) { JEnumType asEnum = oracle.findType(clazz.getCanonicalName()).isEnum(); assert asEnum != null; for (JEnumConstant value : asEnum.getEnumConstants()) { allEnumConstants.put(value, AutoBeanMethod.getEnumName(value)); } } } } for (JMethod method : factoryType.getOverridableMethods()) { if (method.getEnclosingType().equals(autoBeanFactoryInterface)) { // Ignore methods in AutoBeanFactory continue; } JClassType returnType = method.getReturnType().isInterface(); if (returnType == null) { poison("The return type of method %s is a primitive type", method.getName()); continue; } // AutoBean<FooIntf> blah() --> beanType = FooIntf JClassType beanType = ModelUtils.findParameterizationOf(autoBeanInterface, returnType)[0]; if (beanType.isInterface() == null) { poison("The %s parameterization is not an interface", beanType.getQualifiedSourceName()); continue; } // AutoBean<FooIntf> blah(FooIntfSub foo) --> toWrap = FooIntfSub JClassType toWrap; if (method.getParameters().length == 0) { toWrap = null; } else if (method.getParameters().length == 1) { toWrap = method.getParameters()[0].getType().isClassOrInterface(); if (!beanType.isAssignableFrom(toWrap)) { poison("The %s parameterization %s is not assignable from the delegate" + " type %s", autoBeanInterface.getSimpleSourceName(), toWrap.getQualifiedSourceName()); continue; } } else { poison("Unexpecetd parameters in method %s", method.getName()); continue; } AutoBeanType autoBeanType = getAutoBeanType(beanType); // Must wrap things that aren't simple interfaces if (!autoBeanType.isSimpleBean() && toWrap == null) { if (categoryTypes != null) { poison("The %s parameterization is not simple and the following" + " methods did not have static implementations:", beanType.getQualifiedSourceName()); for (AutoBeanMethod missing : autoBeanType.getMethods()) { if (missing.getAction().equals(JBeanMethod.CALL) && missing.getStaticImpl() == null) { poison(missing.getMethod().getReadableDeclaration()); } } } else { poison("The %s parameterization is not simple, but the %s method" + " does not provide a delegate", beanType.getQualifiedSourceName(), method.getName()); } continue; } AutoBeanFactoryMethod.Builder builder = new AutoBeanFactoryMethod.Builder(); builder.setAutoBeanType(autoBeanType); builder.setMethod(method); methods.add(builder.build()); } while (!toCalculate.isEmpty()) { Set<JClassType> examine = toCalculate; toCalculate = new LinkedHashSet<JClassType>(); for (JClassType beanType : examine) { getAutoBeanType(beanType); } } if (poisoned) { die("Unable to complete due to previous errors"); } }
From source file:com.google.web.bindery.requestfactory.gwt.rebind.model.RequestFactoryModel.java
License:Apache License
/** * Examine a RequestContext method to see if it returns a transportable type. *//*from w w w . j av a 2s . c o m*/ private boolean validateContextMethodAndSetDataType(RequestMethod.Builder methodBuilder, JMethod method, boolean allowSetters) throws UnableToCompleteException { JClassType requestReturnType = method.getReturnType().isInterface(); JClassType invocationReturnType; if (requestReturnType == null) { // Primitive return type poison(badContextReturnType(method, requestInterface, instanceRequestInterface)); return false; } if (instanceRequestInterface.isAssignableFrom(requestReturnType)) { // Instance method invocation JClassType[] params = ModelUtils.findParameterizationOf(instanceRequestInterface, requestReturnType); methodBuilder.setInstanceType(getEntityProxyType(params[0])); invocationReturnType = params[1]; } else if (requestInterface.isAssignableFrom(requestReturnType)) { // Static method invocation JClassType[] params = ModelUtils.findParameterizationOf(requestInterface, requestReturnType); invocationReturnType = params[0]; } else { // Unhandled return type, must be something random poison(badContextReturnType(method, requestInterface, instanceRequestInterface)); return false; } // Validate the parameters boolean paramsOk = true; JParameter[] params = method.getParameters(); for (int i = 0; i < params.length; ++i) { JParameter param = params[i]; paramsOk = validateTransportableType(new RequestMethod.Builder(), param.getType(), false) && paramsOk; } // Validate any extra properties on the request type for (JMethod maybeSetter : requestReturnType.getInheritableMethods()) { if (JBeanMethod.SET.matches(maybeSetter) || JBeanMethod.SET_BUILDER.matches(maybeSetter)) { if (allowSetters) { methodBuilder.addExtraSetter(maybeSetter); } else { poison(noSettersAllowed(maybeSetter)); } } } return validateTransportableType(methodBuilder, invocationReturnType, true); }