com.google.gwt.core.ext.typeinfo.JParameter Java Examples

The following examples show how to use com.google.gwt.core.ext.typeinfo.JParameter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServiceBinderCreator.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void generateServiceImplementationWithCallback(TreeLogger logger, SourceWriter srcWriter)
	throws UnableToCompleteException {
	for (JMethod method : this.serviceBinderType.getMethods()) {
		JParameter[] methodParams = method.getParameters();
		JParameter callbackParam = null;
		if (methodParams != null && methodParams.length > 0) {
			callbackParam = methodParams[methodParams.length - 1];
		}
		if (callbackParam == null) {
			break;
		}
		this.writeStartMethod(srcWriter, method);
		this.writeCommandDefinition(logger, srcWriter, method, callbackParam);
		this.writeCommandParam(srcWriter, method, null, callbackParam);
		this.writeEndMethod(srcWriter, method);
	}
}
 
Example #2
Source File: ServiceBinderCreator.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void writeCommandDefinition(TreeLogger logger, SourceWriter srcWriter, JMethod method,
	JParameter callbackParameter) throws UnableToCompleteException {
	srcWriter.print("CommandDefinition commandDefinition  = new CommandDefinition(");
	srcWriter.print("\"%s\", ", this.serviceType.getQualifiedSourceName());
	srcWriter.print("\"%s\", ", method.getName());
	if (callbackParameter != null) {
		JParameterizedType parameterizedCallback = callbackParameter.getType().isParameterized();
		if (parameterizedCallback != null) {
			srcWriter.print("\"%s\" ", parameterizedCallback.getTypeArgs()[0].getQualifiedSourceName());
		} else {
			logger.branch(TreeLogger.ERROR, "Callback argument type for method " + method.getName()
				+ " is not parametrized", null);
			throw new UnableToCompleteException();
		}

	} else {
		srcWriter.print("\"%s\" ", method.getReturnType().getQualifiedSourceName());
	}
	for (JParameter parameter : method.getParameters()) {
		if (!parameter.equals(callbackParameter)) {
			srcWriter.print(", \"%s\"", parameter.getType().getQualifiedSourceName());
		}
	}
	srcWriter.println(");");
}
 
Example #3
Source File: ServiceBinderCreator.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void collectImports() {
	Collection<JType> toImports = Sets.newHashSet();
	for (JMethod method : this.serviceType.getOverridableMethods()) {
		toImports.add(method.getReturnType());
		for (JParameter parameter : method.getParameters()) {
			toImports.add(parameter.getType());
		}
	}
	this.imports.addAll(toImports);
	for (JType importType : this.imports) {
		if (importType instanceof JParameterizedType) {
			JParameterizedType parameterizedType = (JParameterizedType) importType;
			for (JType paramType : parameterizedType.getTypeArgs()) {
				toImports.add(paramType);
			}
		}
	}
	this.imports.addAll(toImports);
}
 
Example #4
Source File: RestServiceBinderCreator.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void generateServiceImplementationWithCallback(SourceWriter srcWriter) {
	for (JMethod method : this.serviceBinderType.getMethods()) {
		JParameter[] methodParams = method.getParameters();
		JParameter callbackParam = null;
		if (methodParams != null && methodParams.length > 0) {
			callbackParam = methodParams[methodParams.length - 1];
		}
		if (callbackParam == null) {
			break;
		}
		this.writeStartMethod(srcWriter, method);
		this.writeMethodCallback(srcWriter, method, null, callbackParam);
		this.writeMethodCall(srcWriter, method, callbackParam);
		this.writeEndMethod(srcWriter, method);
	}
}
 
Example #5
Source File: RestServiceBinderCreator.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void collectImports() {
	Collection<JType> toImports = Sets.newHashSet();
	for (JMethod method : this.serviceType.getOverridableMethods()) {
		toImports.add(method.getReturnType());
		for (JParameter parameter : method.getParameters()) {
			toImports.add(parameter.getType());
		}
	}
	this.imports.addAll(toImports);
	for (JType importType : this.imports) {
		if (importType instanceof JParameterizedType) {
			JParameterizedType parameterizedType = (JParameterizedType) importType;
			for (JType paramType : parameterizedType.getTypeArgs()) {
				toImports.add(paramType);
			}
		}
	}
	this.imports.addAll(toImports);
}
 
Example #6
Source File: PropertyAccessors.java    From gwt-jackson with Apache License 2.0 5 votes vote down vote up
PropertyAccessors( String propertyName, Optional<JField> field, Optional<JMethod> getter, Optional<JMethod> setter,
                   Optional<JParameter> parameter, ImmutableList<JField> fields, ImmutableList<JMethod> getters,
                   ImmutableList<JMethod> setters, ImmutableList<HasAnnotations> accessors ) {

    this.propertyName = propertyName;
    this.field = field;
    this.getter = getter;
    this.setter = setter;
    this.parameter = parameter;
    this.fields = fields;
    this.getters = getters;
    this.setters = setters;
    this.accessors = accessors;
}
 
Example #7
Source File: BeanInfo.java    From gwt-jackson with Apache License 2.0 5 votes vote down vote up
BeanInfo( JClassType type, List<JClassType> parameterizedTypes, Optional<JClassType> builder, Optional<JAbstractMethod> creatorMethod, Map<String, JParameter> creatorParameters, boolean creatorDefaultConstructor, boolean creatorDelegation, Optional<BeanTypeInfo> typeInfo, Optional<PropertyInfo> valuePropertyInfo, Optional<PropertyInfo> anyGetterPropertyInfo, Optional<PropertyInfo> anySetterPropertyInfo, Set<String> ignoredFields, Visibility fieldVisibility, Visibility getterVisibility, Visibility isGetterVisibility, Visibility setterVisibility, Visibility creatorVisibility, boolean ignoreUnknown, List<String> propertyOrderList, boolean propertyOrderAlphabetic, Optional<BeanIdentityInfo> identityInfo, Optional<Include> include ) {

        this.type = type;
        this.parameterizedTypes = ImmutableList.copyOf( parameterizedTypes );

        this.builder = builder;
        this.creatorMethod = creatorMethod;
        this.creatorParameters = ImmutableMap.copyOf( creatorParameters );
        this.creatorDefaultConstructor = creatorDefaultConstructor;
        this.creatorDelegation = creatorDelegation;
        this.typeInfo = typeInfo;
        this.valuePropertyInfo = valuePropertyInfo;
        this.anyGetterPropertyInfo = anyGetterPropertyInfo;
        this.anySetterPropertyInfo = anySetterPropertyInfo;
        this.ignoredFields = ImmutableSet.copyOf( ignoredFields );

        this.fieldVisibility = fieldVisibility;
        this.getterVisibility = getterVisibility;
        this.isGetterVisibility = isGetterVisibility;
        this.setterVisibility = setterVisibility;
        this.creatorVisibility = creatorVisibility;

        this.ignoreUnknown = ignoreUnknown;
        this.propertyOrderList = ImmutableList.copyOf( propertyOrderList );
        this.propertyOrderAlphabetic = propertyOrderAlphabetic;
        this.identityInfo = identityInfo;
        this.include = include;
    }
 
Example #8
Source File: BeanProcessor.java    From gwt-jackson with Apache License 2.0 5 votes vote down vote up
private static <T extends Annotation> boolean isAllParametersAnnotatedWith( JAbstractMethod method, Class<T> annotation ) {
    for ( JParameter parameter : method.getParameters() ) {
        if ( !parameter.isAnnotationPresent( annotation ) ) {
            return false;
        }
    }

    return true;
}
 
Example #9
Source File: RebindConfiguration.java    From gwt-jackson with Apache License 2.0 5 votes vote down vote up
private MapperType[] getParameters( JType mappedType, JAbstractMethod method, boolean isSerializers ) {
    if ( !isSerializers && typeOracle.isEnumSupertype( mappedType ) ) {
        // For enums, the constructor requires the enum class. We just return an empty array and will handle it later
        if ( method.getParameters().length == 1 && Class.class.getName().equals( method.getParameters()[0].getType()
                .getQualifiedSourceName() ) ) {
            return new MapperType[0];
        } else {
            // Not a valid method to create enum deserializer
            return null;
        }
    }

    MapperType[] parameters = new MapperType[method.getParameters().length];
    for ( int i = 0; i < method.getParameters().length; i++ ) {
        JParameter parameter = method.getParameters()[i];
        if ( isSerializers ) {
            if ( typeOracle.isKeySerializer( parameter.getType() ) ) {
                parameters[i] = MapperType.KEY_SERIALIZER;
            } else if ( typeOracle.isJsonSerializer( parameter.getType() ) ) {
                parameters[i] = MapperType.JSON_SERIALIZER;
            } else {
                // the parameter is unknown, we ignore this method
                return null;
            }
        } else {
            if ( typeOracle.isKeyDeserializer( parameter.getType() ) ) {
                parameters[i] = MapperType.KEY_DESERIALIZER;
            } else if ( typeOracle.isJsonDeserializer( parameter.getType() ) ) {
                parameters[i] = MapperType.JSON_DESERIALIZER;
            } else {
                // the parameter is unknown, we ignore this method
                return null;
            }
        }
    }
    return parameters;
}
 
Example #10
Source File: BeanJsonDeserializerCreator.java    From gwt-jackson with Apache License 2.0 5 votes vote down vote up
private MethodSpec buildInstanceBuilderCreateMethod() {
    JAbstractMethod method = beanInfo.getCreatorMethod().get();

    MethodSpec.Builder builder = MethodSpec.methodBuilder( "create" )
            .addModifiers( Modifier.PRIVATE )
            .returns( typeName( beanInfo.getType() ) );

    StringBuilder parametersNameBuilder = new StringBuilder();
    int index = 0;
    for ( Map.Entry<String, JParameter> parameterEntry : beanInfo.getCreatorParameters().entrySet() ) {
        if ( index > 0 ) {
            parametersNameBuilder.append( ", " );
        }
        PropertyInfo property = properties.get( parameterEntry.getKey() );

        String variableName = String.format( INSTANCE_BUILDER_VARIABLE_FORMAT, index++ );
        builder.addParameter( typeName( property.getType() ), variableName );
        parametersNameBuilder.append( variableName );
    }
    String parametersName = parametersNameBuilder.toString();

    if ( method.isPrivate() || (!method.isPublic() && !mapperInfo.isSamePackage()) ) {
        // private method, we use jsni
        builder.addModifiers( Modifier.NATIVE );
        builder.addCode( JsniCodeBlockBuilder.builder()
                .addStatement( "return $L($L)", method.getJsniSignature(), parametersName )
                .build() );
    } else {
        if ( null != method.isConstructor() ) {
            builder.addStatement( "return new $T($L)", typeName( beanInfo.getType() ), parametersName );
        } else {
            builder.addStatement( "return $T.$L($L)", typeName( beanInfo.getType() ), method.getName(), parametersName );
        }
    }

    return builder.build();
}
 
Example #11
Source File: RestServiceBinderCreator.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void writeMethodCall(SourceWriter srcWriter, JMethod method, JParameter callbackParam) {
	srcWriter.print("REST.<%s> withCallback(compositeCallback).call(restService).%s(", this.typeAsString(method
		.getReturnType(), true), method.getName());
	int i = 0;
	for (JParameter parameter : method.getParameters()) {
		if (!parameter.equals(callbackParam)) {
			if (i++ > 0) {
				srcWriter.print(", ");
			}
			srcWriter.print("$%d_%s", i, parameter.getName());
		}
	}
	srcWriter.println(");");
}
 
Example #12
Source File: RestServiceBinderCreator.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void writeStartMethod(SourceWriter srcWriter, JMethod method) {
	srcWriter.print("public %s %s(", this.typeAsString(method.getReturnType(), false), method.getName());
	int i = 0;
	for (JParameter parameter : method.getParameters()) {
		if (i++ > 0) {
			srcWriter.print(", ");
		}
		srcWriter.print("%s $%d_%s", this.typeAsString(parameter.getType(), false), i, parameter.getName());
	}
	srcWriter.println("){");
	srcWriter.indent();
}
 
Example #13
Source File: ServiceBinderCreator.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void writeStartMethod(SourceWriter srcWriter, JMethod method) {
	srcWriter.print("public %s %s(", this.typeAsString(method.getReturnType(), false), method.getName());
	int i = 0;
	for (JParameter parameter : method.getParameters()) {
		if (i++ > 0) {
			srcWriter.print(", ");
		}
		srcWriter.print("%s $%d_%s", this.typeAsString(parameter.getType(), false), i, parameter.getName());
	}
	srcWriter.println("){");
	srcWriter.indent();
}
 
Example #14
Source File: RestServiceBinderCreator.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void writeMethodCallback(SourceWriter srcWriter, JMethod method, Collection<CallbackMethod> callbackSuccess,
	JParameter callbackParam) {
	boolean quiet = method.getAnnotation(QuietCommand.class) != null;

	srcWriter.print("CompositeMethodCallback compositeCallback = new CompositeMethodCallback(");
	srcWriter.print("%s, ", quiet);
	srcWriter.println("Lists.newArrayList(");
	srcWriter.indent();
	int i = 0;
	if (callbackParam != null) {
		srcWriter.print("$%d_%s", method.getParameters().length, callbackParam);
		i++;
	}

	if (callbackSuccess != null) {
		for (CallbackMethod callbackMethod : callbackSuccess) {
			if (i++ > 0) {
				srcWriter.print(", ");
			}
			srcWriter.println("new MethodCallbackAdapter<%s>(){", this.typeAsString(method.getReturnType(), true));
			srcWriter.indent();
			if (callbackMethod.successMethodName != null) {
				srcWriter.println("public void onSuccess(Method method, %s result){", this.typeAsString(method
					.getReturnType(), true));
				srcWriter.indent();
				srcWriter.println("getHandler().%s(result);", callbackMethod.successMethodName);
				srcWriter.outdent();
				srcWriter.println("}");
			}
			if (callbackMethod.failureMethodName != null) {
				srcWriter
					.println("public void onFailure(Method method, Throwable caught){", this.typeAsString(method
						.getReturnType(), true));
				srcWriter.indent();
				srcWriter.println("getHandler().%s(caught);", callbackMethod.failureMethodName);
				srcWriter.outdent();
				srcWriter.println("}");
			}
			srcWriter.outdent();
			srcWriter.print("}");
		}
	}
	srcWriter.outdent();
	srcWriter.println("));");
}
 
Example #15
Source File: ExtendedServiceProxyGenerator.java    From appinventor-extensions with Apache License 2.0 4 votes vote down vote up
/**
 * Generate the implementation of a single method.
 *
 * @param out where to print the method to
 * @param method the method
 * @param typeName type name of the containing proxy class
 */
private void printMethod(PrintWriter out, JMethod method, String typeName) {
  // Build parameter lists
  int i = 0;
  StringBuilder actualParamsBuilder = new StringBuilder();
  StringBuilder formalParamsBuilder = new StringBuilder();
  for (JParameter param : method.getParameters()) {
    if (i != 0) {
      actualParamsBuilder.append(", ");
      formalParamsBuilder.append(", ");
    }

    String paramType = param.getType().getParameterizedQualifiedSourceName();
    String paramName = "p" + i++;
    actualParamsBuilder.append(paramName);
    formalParamsBuilder.append(paramType + " " + paramName);
  }
  String actualParams = actualParamsBuilder.toString();
  String formalParams = formalParamsBuilder.toString();

  // Information about the return type
  JType returnType = method.getReturnType();
  boolean hasReturnValue = !returnType.getSimpleSourceName().equals("void");

  JPrimitiveType primitiveReturnType = returnType.isPrimitive();
  String resultType =
      primitiveReturnType != null ? primitiveReturnType.getQualifiedBoxedSourceName()
          : returnType.getParameterizedQualifiedSourceName();

  String callbackType = AsyncCallback.class.getName() + "<" + resultType + ">";

  // Print method
  out.println("  public void " + method.getName() + "(" + formalParams
      + (formalParams.isEmpty() ? "" : ", ") + "final " + callbackType + " callback" + ") {");
  out.println("    fireStart(\"" + method.getName() + "\"" + (actualParams.isEmpty() ? "" : ", ")
      + actualParams + ");");
  out.println("    proxy." + method.getName() + "(" + actualParams
      + (actualParams.isEmpty() ? "" : ", ") + "new " + callbackType + "() {");
  out.println("      public void onSuccess(" + resultType + " result) {");
  out.println("        " + outcome(method, "Success", "result"));
  out.println("      }");
  out.println("      public void onFailure(Throwable caught) {");
  out.println("        " + outcome(method, "Failure", "caught"));
  out.println("      }");
  out.println("    });");
  out.println("  }");
}
 
Example #16
Source File: PropertyAccessorsBuilder.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
void setParameter( JParameter parameter ) {
    this.parameter = Optional.of( parameter );
    this.accessors.add( parameter );
}
 
Example #17
Source File: BeanJsonDeserializerCreator.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
private MethodSpec buildInitInstanceBuilderMethod() throws UnableToCompleteException, UnsupportedTypeException {

        MethodSpec.Builder initInstanceBuilderMethodBuilder = MethodSpec.methodBuilder( "initInstanceBuilder" )
                .addModifiers( Modifier.PROTECTED )
                .addAnnotation( Override.class )
                .returns( parameterizedName( InstanceBuilder.class, beanInfo.getType() ) );

        TypeName deserializersMapTypeName = ParameterizedTypeName.get( ClassName.get( SimpleStringMap.class ),
                ClassName.get( HasDeserializerAndParameters.class ) );

        if ( null != beanInfo.getCreatorParameters() && !beanInfo.getCreatorParameters().isEmpty() ) {

            initInstanceBuilderMethodBuilder
                    .addStatement( "final $T deserializers = $T.createObject().cast()", deserializersMapTypeName, SimpleStringMap.class );

            // for each constructor parameters, we initialize its deserializer.
            int index = 0;
            for ( Entry<String, JParameter> entry : beanInfo.getCreatorParameters().entrySet() ) {

                TypeName typeName = typeName( true, entry.getValue().getType() );
                TypeName deserializerTypeName = ParameterizedTypeName.get( ClassName.get( HasDeserializerAndParameters.class ), typeName,
                        ParameterizedTypeName.get( ClassName.get( JsonDeserializer.class ), typeName ) );

                TypeSpec.Builder deserializerBuilder = TypeSpec.anonymousClassBuilder( "" )
                        .superclass( deserializerTypeName );
                List<MethodSpec> commonMethods = buildCommonPropertyDeserializerMethods( properties.get( entry.getKey() ) );
                for ( MethodSpec method : commonMethods ) {
                    deserializerBuilder.addMethod( method );
                }

                String deserializerName = INSTANCE_BUILDER_DESERIALIZER_PREFIX + String
                        .format( INSTANCE_BUILDER_VARIABLE_FORMAT, index++ );
                initInstanceBuilderMethodBuilder
                        .addStatement( "final $T $L = $L", deserializerTypeName, deserializerName, deserializerBuilder.build() );
                initInstanceBuilderMethodBuilder.addStatement( "deserializers.put($S, $L)", entry.getKey(), deserializerName );
            }
        } else {
            initInstanceBuilderMethodBuilder.addStatement( "final $T deserializers = null", deserializersMapTypeName );
        }

        if ( beanInfo.getBuilder().isPresent() ) {
            // we create the deserializer for the builder class itself
            JDeserializerType builderType = getJsonDeserializerFromType( beanInfo.getBuilder().get() );
            initInstanceBuilderMethodBuilder.addStatement( "final $T builderDeserializer = $L",
                    JTypeName.parameterizedName( AbstractBeanJsonDeserializer.class, beanInfo.getBuilder().get() ),
                    builderType.getInstance() );
        }

        initInstanceBuilderMethodBuilder.addCode( "\n" );

        return initInstanceBuilderMethodBuilder
                .addStatement( "return $L", buildInstanceBuilderClass() )
                .build();
    }
 
Example #18
Source File: ServiceBinderCreator.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void writeCommandParam(SourceWriter srcWriter, JMethod method, Collection<CallbackMethod> callbackSuccess,
	JParameter callbackParameter) {
	boolean lazy = method.getAnnotation(LazyCommand.class) != null;
	boolean quiet = method.getAnnotation(QuietCommand.class) != null;

	srcWriter.print("CommandParam commandParam = new CommandParam(");
	srcWriter.print("%s, ", lazy);
	srcWriter.print("%s, ", quiet);
	srcWriter.print("this.serializer, ");
	srcWriter.print("Lists.newArrayList(Arrays.asList(");
	int i = 0;
	for (JParameter parameter : method.getParameters()) {
		if (!parameter.equals(callbackParameter)) {
			if (i++ > 0) {
				srcWriter.print(", ");
			}
			srcWriter.print("$%d_%s", i, parameter.getName());
		}
	}
	srcWriter.println(")), ");
	srcWriter.indent();
	srcWriter.println("Lists.newArrayList(");
	srcWriter.indent();
	i = 0;
	if (callbackParameter != null) {
		srcWriter.print("$%d_%s", method.getParameters().length, callbackParameter.getName());
		i++;
	}

	if (callbackSuccess != null) {
		for (CallbackMethod callbackMethod : callbackSuccess) {
			if (i++ > 0) {
				srcWriter.print(", ");
			}
			srcWriter.println("new CallbackAdapter<%s>(){", this.typeAsString(method.getReturnType(), true));
			srcWriter.indent();
			if (callbackMethod.successMethodName != null) {
				srcWriter.println("public void onSuccess(%s result){", this.typeAsString(method.getReturnType(), true));
				srcWriter.indent();
				srcWriter.println("getHandler().%s(result);", callbackMethod.successMethodName);
				srcWriter.outdent();
				srcWriter.println("}");
			}
			if (callbackMethod.failureMethodName != null) {
				srcWriter
					.println("public void onFailure(Throwable caught){", this.typeAsString(method.getReturnType(), true));
				srcWriter.indent();
				srcWriter.println("getHandler().%s(caught);", callbackMethod.failureMethodName);
				srcWriter.outdent();
				srcWriter.println("}");
			}
			srcWriter.outdent();
			srcWriter.print("}");
		}
	}
	srcWriter.outdent();
	srcWriter.println("));");
	srcWriter.outdent();
}
 
Example #19
Source File: BeanInfoBuilder.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
void setCreatorParameters( Map<String, JParameter> creatorParameters ) {
    this.creatorParameters = creatorParameters;
}
 
Example #20
Source File: PropertyAccessors.java    From gwt-jackson with Apache License 2.0 2 votes vote down vote up
/**
 * <p>Getter for the field <code>parameter</code>.</p>
 *
 * @return a {@link com.google.gwt.thirdparty.guava.common.base.Optional} object.
 */
public Optional<JParameter> getParameter() {
    return parameter;
}
 
Example #21
Source File: BeanInfo.java    From gwt-jackson with Apache License 2.0 2 votes vote down vote up
/**
 * <p>Getter for the field <code>creatorParameters</code>.</p>
 *
 * @return a {@link com.google.gwt.thirdparty.guava.common.collect.ImmutableMap} object.
 */
public ImmutableMap<String, JParameter> getCreatorParameters() {
    return creatorParameters;
}
 
Example #22
Source File: BeanInfoBuilder.java    From gwt-jackson with Apache License 2.0 2 votes vote down vote up
/**
 * <p>Getter for the field <code>creatorParameters</code>.</p>
 *
 * @return a {@link java.util.Map} object.
 */
public Map<String, JParameter> getCreatorParameters() {
    return creatorParameters;
}