Java Code Examples for com.squareup.javapoet.TypeName#VOID

The following examples show how to use com.squareup.javapoet.TypeName#VOID . 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: BindingSet.java    From butterknife with Apache License 2.0 6 votes vote down vote up
private static TypeName bestGuess(String type) {
  switch (type) {
    case "void": return TypeName.VOID;
    case "boolean": return TypeName.BOOLEAN;
    case "byte": return TypeName.BYTE;
    case "char": return TypeName.CHAR;
    case "double": return TypeName.DOUBLE;
    case "float": return TypeName.FLOAT;
    case "int": return TypeName.INT;
    case "long": return TypeName.LONG;
    case "short": return TypeName.SHORT;
    default:
      int left = type.indexOf('<');
      if (left != -1) {
        ClassName typeClassName = ClassName.bestGuess(type.substring(0, left));
        List<TypeName> typeArguments = new ArrayList<>();
        do {
          typeArguments.add(WildcardTypeName.subtypeOf(Object.class));
          left = type.indexOf('<', left + 1);
        } while (left != -1);
        return ParameterizedTypeName.get(typeClassName,
            typeArguments.toArray(new TypeName[typeArguments.size()]));
      }
      return ClassName.bestGuess(type);
  }
}
 
Example 2
Source File: TypeUtil.java    From Intimate with Apache License 2.0 6 votes vote down vote up
public static String typeDefaultReturnCode(CName cName) {

        if (cName.isPrimitive) {
            if (cName.typeName == TypeName.VOID) return "";
            if (cName.typeName == TypeName.BOOLEAN) return "return false;";
            if (cName.typeName == TypeName.BYTE) return "return 0;";
            if (cName.typeName == TypeName.SHORT) return "return 0;";
            if (cName.typeName == TypeName.INT) return "return 0;";
            if (cName.typeName == TypeName.LONG) return "return 0;";
            if (cName.typeName == TypeName.CHAR) return "return 0;";
            if (cName.typeName == TypeName.FLOAT) return "return 0;";
            if (cName.typeName == TypeName.DOUBLE) return "return 0;";
        } else if (cName.fullName.equals("void")) {
            return "";
        }
        return "return null;";
    }
 
Example 3
Source File: PsiTypeUtils.java    From litho with Apache License 2.0 6 votes vote down vote up
private static TypeName getPrimitiveTypeName(PsiPrimitiveType type) {
  switch (type.getCanonicalText()) {
    case "void":
      return TypeName.VOID;
    case "boolean":
      return TypeName.BOOLEAN;
    case "byte":
      return TypeName.BYTE;
    case "short":
      return TypeName.SHORT;
    case "int":
      return TypeName.INT;
    case "long":
      return TypeName.LONG;
    case "char":
      return TypeName.CHAR;
    case "float":
      return TypeName.FLOAT;
    case "double":
      return TypeName.DOUBLE;
    default:
      throw new UnsupportedOperationException(
          "Unknown primitive type: " + type.getCanonicalText());
  }
}
 
Example 4
Source File: JTypeName.java    From gwt-jackson with Apache License 2.0 6 votes vote down vote up
private TypeName primitiveName( JPrimitiveType type, boolean boxed ) {
    if ( "boolean".equals( type.getSimpleSourceName() ) ) {
        return boxed ? BOOLEAN_NAME : TypeName.BOOLEAN;
    } else if ( "byte".equals( type.getSimpleSourceName() ) ) {
        return boxed ? BYTE_NAME : TypeName.BYTE;
    } else if ( "short".equals( type.getSimpleSourceName() ) ) {
        return boxed ? SHORT_NAME : TypeName.SHORT;
    } else if ( "int".equals( type.getSimpleSourceName() ) ) {
        return boxed ? INTEGER_NAME : TypeName.INT;
    } else if ( "long".equals( type.getSimpleSourceName() ) ) {
        return boxed ? LONG_NAME : TypeName.LONG;
    } else if ( "char".equals( type.getSimpleSourceName() ) ) {
        return boxed ? CHARACTER_NAME : TypeName.CHAR;
    } else if ( "float".equals( type.getSimpleSourceName() ) ) {
        return boxed ? FLOAT_NAME : TypeName.FLOAT;
    } else if ( "double".equals( type.getSimpleSourceName() ) ) {
        return boxed ? DOUBLE_NAME : TypeName.DOUBLE;
    } else {
        return boxed ? VOID_NAME : TypeName.VOID;
    }
}
 
Example 5
Source File: TypeUtility.java    From kripton with Apache License 2.0 6 votes vote down vote up
/**
 * Convert a TypeMirror in a typeName.
 *
 * @param typeMirror
 *            the type mirror
 * @return typeName
 */
public static TypeName typeName(TypeMirror typeMirror) {
	LiteralType literalType = LiteralType.of(typeMirror.toString());

	if (literalType.isArray()) {
		return ArrayTypeName.of(typeName(literalType.getRawType()));
	} else if (literalType.isCollection()) {
		return ParameterizedTypeName.get(TypeUtility.className(literalType.getRawType()),
				typeName(literalType.getTypeParameter()));
	}

	TypeName[] values = { TypeName.BOOLEAN, TypeName.BYTE, TypeName.CHAR, TypeName.DOUBLE, TypeName.FLOAT,
			TypeName.INT, TypeName.LONG, TypeName.SHORT, TypeName.VOID };

	for (TypeName item : values) {
		if (typeMirror.toString().equals(item.toString())) {
			return item;
		}
	}

	return TypeName.get(typeMirror);
}
 
Example 6
Source File: InsertRawHelper.java    From kripton with Apache License 2.0 6 votes vote down vote up
/**
 * Generate javadoc about return type of method.
 *
 * @param methodBuilder the method builder
 * @param returnType the return type
 */
public static void generateJavaDocReturnType(MethodSpec.Builder methodBuilder, TypeName returnType) {
	if (returnType == TypeName.VOID) {

	} else if (TypeUtility.isTypeIncludedIn(returnType, Boolean.TYPE, Boolean.class)) {
		methodBuilder.addJavadoc("\n");
		methodBuilder.addJavadoc("@return <code>true</code> if record is inserted, <code>false</code> otherwise");
	} else if (TypeUtility.isTypeIncludedIn(returnType, Long.TYPE, Long.class)) {
		methodBuilder.addJavadoc("\n");
		methodBuilder.addJavadoc("@return <strong>id</strong> of inserted record");
	} else if (TypeUtility.isTypeIncludedIn(returnType, Integer.TYPE, Integer.class)) {
		methodBuilder.addJavadoc("\n");
		methodBuilder.addJavadoc("@return <strong>id</strong> of inserted record");
	}
	methodBuilder.addJavadoc("\n");
}
 
Example 7
Source File: RouterProcessor.java    From JIMU with Apache License 2.0 5 votes vote down vote up
/**
 * create init map method
 */
private MethodSpec generateInitMapMethod() {
    TypeName returnType = TypeName.VOID;

    MethodSpec.Builder openUriMethodSpecBuilder = MethodSpec.methodBuilder("initMap")
            .returns(returnType)
            .addAnnotation(Override.class)
            .addModifiers(Modifier.PUBLIC);

    openUriMethodSpecBuilder.addStatement("super.initMap()");

    for (Node node : routerNodes) {
        openUriMethodSpecBuilder.addStatement(
                mRouteMapperFieldName + ".put($S,$T.class)",
                node.getPath(),
                ClassName.get((TypeElement) node.getRawType()));

        // Make map body for paramsType
        StringBuilder mapBodyBuilder = new StringBuilder();
        Map<String, Integer> paramsType = node.getParamsType();
        if (MapUtils.isNotEmpty(paramsType)) {
            for (Map.Entry<String, Integer> types : paramsType.entrySet()) {
                mapBodyBuilder.append("put(\"").append(types.getKey()).append("\", ").append(types.getValue()).append("); ");
            }
        }
        String mapBody = mapBodyBuilder.toString();
        logger.info(">>> mapBody: " + mapBody + " <<<");
        if (!StringUtils.isEmpty(mapBody)) {
            openUriMethodSpecBuilder.addStatement(
                    mParamsMapperFieldName + ".put($T.class,"
                            + "new java.util.HashMap<String, Integer>(){{" + mapBody + "}}" + ")",
                    ClassName.get((TypeElement) node.getRawType()));
        }
    }

    return openUriMethodSpecBuilder.build();
}
 
Example 8
Source File: EasyType.java    From RapidORM with Apache License 2.0 5 votes vote down vote up
/**
 * @param type
 * @return
 */
public static TypeName bestGuessDeep(String type) {
    switch (type) {
        case "void":
            return TypeName.VOID;
        case "boolean":
            return TypeName.BOOLEAN;
        case "byte":
            return TypeName.BYTE;
        case "char":
            return TypeName.CHAR;
        case "double":
            return TypeName.DOUBLE;
        case "float":
            return TypeName.FLOAT;
        case "int":
            return TypeName.INT;
        case "long":
            return TypeName.LONG;
        case "short":
            return TypeName.SHORT;
        default:
            int left = type.indexOf('<');
            int right = type.indexOf('>');
            if (-1 != left && -1 != right) {
                ClassName typeClassName = ClassName.bestGuess(type.substring(0, left));
                List<TypeName> typeArguments = new ArrayList<>();
                do {
                    typeArguments.add(WildcardTypeName.subtypeOf(bestGuess(type.substring(left + 1, right))));
                    left = type.indexOf('<', left + 1);
                    right = type.indexOf('>', right - 1);
                } while (left != -1);
                return ParameterizedTypeName.get(typeClassName,
                        typeArguments.toArray(new TypeName[typeArguments.size()]));
            }
            return ClassName.bestGuess(type);
    }
}
 
Example 9
Source File: EasyType.java    From RapidORM with Apache License 2.0 5 votes vote down vote up
public static TypeName bestGuess(String type) {
    switch (type) {
        case "void":
            return TypeName.VOID;
        case "boolean":
            return TypeName.BOOLEAN;
        case "byte":
            return TypeName.BYTE;
        case "char":
            return TypeName.CHAR;
        case "double":
            return TypeName.DOUBLE;
        case "float":
            return TypeName.FLOAT;
        case "int":
            return TypeName.INT;
        case "long":
            return TypeName.LONG;
        case "short":
            return TypeName.SHORT;
        default:
            int left = type.indexOf('<');
            if (left != -1) {
                ClassName typeClassName = ClassName.bestGuess(type.substring(0, left));
                List<TypeName> typeArguments = new ArrayList<>();
                do {
                    typeArguments.add(WildcardTypeName.subtypeOf(Object.class));
                    left = type.indexOf('<', left + 1);
                } while (left != -1);
                return ParameterizedTypeName.get(typeClassName,
                        typeArguments.toArray(new TypeName[typeArguments.size()]));
            }
            return ClassName.bestGuess(type);
    }
}
 
Example 10
Source File: ModifyBeanHelper.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * Builds the return code.
 *
 * @param methodBuilder
 *            the method builder
 * @param updateMode
 *            the update mode
 * @param method
 *            the method
 * @param returnType
 *            the return type
 */
public void buildReturnCode(MethodSpec.Builder methodBuilder, boolean updateMode, SQLiteModelMethod method, TypeName returnType) {
	if (returnType == TypeName.VOID) {

	} else if (isTypeIncludedIn(returnType, Boolean.TYPE, Boolean.class)) {
		methodBuilder.addJavadoc("\n");
		if (updateMode)
			methodBuilder.addJavadoc("@return <code>true</code> if record is updated, <code>false</code> otherwise");
		else
			methodBuilder.addJavadoc("@return <code>true</code> if record is deleted, <code>false</code> otherwise");
		methodBuilder.addJavadoc("\n");

		methodBuilder.addCode("return result!=0;\n");
	} else if (isTypeIncludedIn(returnType, Long.TYPE, Long.class, Integer.TYPE, Integer.class, Short.TYPE, Short.class)) {
		methodBuilder.addJavadoc("\n");
		if (updateMode) {
			methodBuilder.addJavadoc("@return number of updated records");
		} else {
			methodBuilder.addJavadoc("@return number of deleted records");
		}
		methodBuilder.addJavadoc("\n");

		methodBuilder.addCode("return result;\n");
	} else {
		// more than one listener found
		throw (new InvalidMethodSignException(method, "invalid return type"));
	}
}
 
Example 11
Source File: TypeUtils.java    From grouter-android with Apache License 2.0 5 votes vote down vote up
public static TypeName getTypeName(String name) {
    switch (name) {
        case "void":
            return TypeName.VOID;
        case "int":
            return TypeName.get(int.class);
        case "int[]":
            return TypeName.get(int[].class);
        case "long":
            return TypeName.get(long.class);
        case "long[]":
            return TypeName.get(long[].class);
        case "double":
            return TypeName.get(double.class);
        case "double[]":
            return TypeName.get(double[].class);
        case "float":
            return TypeName.get(float.class);
        case "float[]":
            return TypeName.get(float[].class);
        case "boolean":
            return TypeName.get(boolean.class);
        case "boolean[]":
            return TypeName.get(boolean[].class);
        case "short":
            return TypeName.get(short.class);
        case "short[]":
            return TypeName.get(short[].class);
        case "char":
            return TypeName.get(char.class);
        case "char[]":
            return TypeName.get(char[].class);
        default:
            if (name.endsWith("[]")) {
                return ArrayTypeName.of(ClassName.bestGuess(name.substring(0, name.length() - 2)));
            }
            return ClassName.bestGuess(name);
    }
}
 
Example 12
Source File: TypeUtils.java    From grouter-android with Apache License 2.0 5 votes vote down vote up
public static TypeName getTypeName(String name) {
    switch (name) {
        case "void":
            return TypeName.VOID;
        case "int":
            return TypeName.get(int.class);
        case "int[]":
            return TypeName.get(int[].class);
        case "long":
            return TypeName.get(long.class);
        case "long[]":
            return TypeName.get(long[].class);
        case "double":
            return TypeName.get(double.class);
        case "double[]":
            return TypeName.get(double[].class);
        case "float":
            return TypeName.get(float.class);
        case "float[]":
            return TypeName.get(float[].class);
        case "boolean":
            return TypeName.get(boolean.class);
        case "boolean[]":
            return TypeName.get(boolean[].class);
        case "short":
            return TypeName.get(short.class);
        case "short[]":
            return TypeName.get(short[].class);
        case "char":
            return TypeName.get(char.class);
        case "char[]":
            return TypeName.get(char[].class);
        default:
            if (name.endsWith("[]")) {
                return ArrayTypeName.of(ClassName.bestGuess(name.substring(0, name.length() - 2)));
            }
            return ClassName.bestGuess(name);
    }
}
 
Example 13
Source File: RouterProcessor.java    From DDComponentForAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * create init map method
 */
private MethodSpec generateInitMapMethod() {
    TypeName returnType = TypeName.VOID;

    MethodSpec.Builder openUriMethodSpecBuilder = MethodSpec.methodBuilder("initMap")
            .returns(returnType)
            .addAnnotation(Override.class)
            .addModifiers(Modifier.PUBLIC);

    openUriMethodSpecBuilder.addStatement("super.initMap()");

    for (Node node : routerNodes) {
        openUriMethodSpecBuilder.addStatement(
                mRouteMapperFieldName + ".put($S,$T.class)",
                node.getPath(),
                ClassName.get((TypeElement) node.getRawType()));

        // Make map body for paramsType
        StringBuilder mapBodyBuilder = new StringBuilder();
        Map<String, Integer> paramsType = node.getParamsType();
        if (MapUtils.isNotEmpty(paramsType)) {
            for (Map.Entry<String, Integer> types : paramsType.entrySet()) {
                mapBodyBuilder.append("put(\"").append(types.getKey()).append("\", ").append(types.getValue()).append("); ");
            }
        }
        String mapBody = mapBodyBuilder.toString();
        logger.info(">>> mapBody: " + mapBody + " <<<");
        if (!StringUtils.isEmpty(mapBody)) {
            openUriMethodSpecBuilder.addStatement(
                    mParamsMapperFieldName + ".put($T.class,"
                            + "new java.util.HashMap<String, Integer>(){{" + mapBody + "}}" + ")",
                    ClassName.get((TypeElement) node.getRawType()));
        }
    }

    return openUriMethodSpecBuilder.build();
}
 
Example 14
Source File: TypeUtils.java    From data-mediator with Apache License 2.0 5 votes vote down vote up
public static TypeName getTypeName(String type){
    switch (type){
        case NAME_int:
            return TypeName.INT;

        case NAME_long:
            return TypeName.LONG;

        case NAME_short:
            return TypeName.SHORT;

        case NAME_byte:
            return TypeName.BYTE;

        case NAME_boolean:
            return TypeName.BOOLEAN;

        case NAME_float:
            return TypeName.FLOAT;

        case NAME_double:
            return TypeName.DOUBLE;

        case NAME_char:
            return TypeName.CHAR;

        case NAME_void:
            return TypeName.VOID;
    }
    return ClassName.bestGuess(type);
}
 
Example 15
Source File: Scalars.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
public static TypeName classToTypeName(Class scalar) {
    if (scalar.isPrimitive()) {
        switch (scalar.getSimpleName()) {
            case "int":
                return TypeName.INT;

            case "boolean":
                return TypeName.BOOLEAN;

            case "double":
                return TypeName.DOUBLE;

            case "float":
                return TypeName.FLOAT;

            case "byte":
                return TypeName.BYTE;

            case "char":
                return TypeName.CHAR;

            case "short":
                return TypeName.SHORT;

            case "long":
                return TypeName.LONG;

            case "void":
                return TypeName.VOID; // ?

            default:
                throw new GenerationException("can't handle type: " + scalar);
        }
    } else {
        return ClassName.get(scalar);
    }
}
 
Example 16
Source File: EventDeclarationsExtractor.java    From litho with Apache License 2.0 5 votes vote down vote up
public static ImmutableList<EventDeclarationModel> getEventDeclarations(
    Elements elements, TypeElement element, Class<?> annotationType, EnumSet<RunMode> runMode) {
  final List<AnnotationValue> eventTypes =
      ProcessorUtils.getAnnotationParameter(
          elements, element, annotationType, "events", List.class);

  final List<EventDeclarationModel> eventDeclarations;
  if (eventTypes != null) {
    eventDeclarations = new ArrayList<>();
    for (AnnotationValue eventType : eventTypes) {
      final DeclaredType type = (DeclaredType) eventType.getValue();
      final TypeName returnType =
          runMode.contains(RunMode.ABI)
              ? TypeName.VOID
              : getReturnType(elements, type.asElement());
      final ImmutableList<FieldModel> fields =
          runMode.contains(RunMode.ABI)
              ? ImmutableList.of()
              : FieldsExtractor.extractFields(type.asElement());
      eventDeclarations.add(
          new EventDeclarationModel(
              ClassName.bestGuess(type.asElement().toString()),
              returnType,
              fields,
              type.asElement()));
    }
  } else {
    eventDeclarations = Collections.emptyList();
  }

  return ImmutableList.copyOf(eventDeclarations);
}
 
Example 17
Source File: EventCaseGeneratorTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicGeneratorCase() {
  final MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("method");
  final EventDeclarationModel model =
      new EventDeclarationModel(ClassName.OBJECT, TypeName.VOID, ImmutableList.of(), null);

  EventCaseGenerator.builder()
      .contextClass(ClassNames.COMPONENT_CONTEXT)
      .eventMethodModels(
          ImmutableList.of(
              SpecMethodModel.<EventMethod, EventDeclarationModel>builder()
                  .name("event")
                  .returnTypeSpec(new TypeSpec(TypeName.VOID))
                  .typeModel(model)
                  .build()))
      .writeTo(methodBuilder);

  assertThat(methodBuilder.build().toString())
      .isEqualTo(
          "void method() {\n"
              + "  case 96891546: {\n"
              + "    java.lang.Object _event = (java.lang.Object) eventState;\n"
              + "    event(\n"
              + "          eventHandler.mHasEventDispatcher);\n"
              + "    return null;\n"
              + "  }\n"
              + "}\n");
}
 
Example 18
Source File: ClassNameModel.java    From nalu with Apache License 2.0 4 votes vote down vote up
public TypeName getTypeName() {
  switch (className) {
    case "void":
      return TypeName.VOID;
    case "boolean":
      return TypeName.BOOLEAN;
    case "byte":
      return TypeName.BYTE;
    case "short":
      return TypeName.SHORT;
    case "int":
      return TypeName.INT;
    case "long":
      return TypeName.LONG;
    case "char":
      return TypeName.CHAR;
    case "float":
      return TypeName.FLOAT;
    case "double":
      return TypeName.DOUBLE;
    case "Object":
      return TypeName.OBJECT;
    case "Void":
      return ClassName.get("java.lang",
                           "Void");
    case "Boolean":
      return ClassName.get("java.lang",
                           "Boolean");
    case "Byte":
      return ClassName.get("java.lang",
                           "Byte");
    case "Short":
      return ClassName.get("java.lang",
                           "Short");
    case "Integer":
      return ClassName.get("java.lang",
                           "Integer");
    case "Long":
      return ClassName.get("java.lang",
                           "Long");
    case "Character":
      return ClassName.get("java.lang",
                           "Character");
    case "Float":
      return ClassName.get("java.lang",
                           "Float");
    case "Double":
      return ClassName.get("java.lang",
                           "Double");
    default:
      return ClassName.get(this.getPackage(),
                           this.getSimpleName());
  }
}
 
Example 19
Source File: ProcessUtils.java    From Rx.Observe with Apache License 2.0 4 votes vote down vote up
public boolean isVoid(TypeName returnType) {
    return returnType == TypeName.VOID || ClassName.get("java.lang", "Void").equals(returnType);
}
 
Example 20
Source File: ClassNameModel.java    From nalu with Apache License 2.0 4 votes vote down vote up
public TypeName getTypeName() {
  switch (className) {
    case "void":
      return TypeName.VOID;
    case "boolean":
      return TypeName.BOOLEAN;
    case "byte":
      return TypeName.BYTE;
    case "short":
      return TypeName.SHORT;
    case "int":
      return TypeName.INT;
    case "long":
      return TypeName.LONG;
    case "char":
      return TypeName.CHAR;
    case "float":
      return TypeName.FLOAT;
    case "double":
      return TypeName.DOUBLE;
    case "Object":
      return TypeName.OBJECT;
    case "Void":
      return ClassName.get("java.lang",
                           "Void");
    case "Boolean":
      return ClassName.get("java.lang",
                           "Boolean");
    case "Byte":
      return ClassName.get("java.lang",
                           "Byte");
    case "Short":
      return ClassName.get("java.lang",
                           "Short");
    case "Integer":
      return ClassName.get("java.lang",
                           "Integer");
    case "Long":
      return ClassName.get("java.lang",
                           "Long");
    case "Character":
      return ClassName.get("java.lang",
                           "Character");
    case "Float":
      return ClassName.get("java.lang",
                           "Float");
    case "Double":
      return ClassName.get("java.lang",
                           "Double");
    default:
      return ClassName.get(this.getPackage(),
                           this.getSimpleName());
  }
}