Java Code Examples for javax.lang.model.type.DeclaredType#toString()

The following examples show how to use javax.lang.model.type.DeclaredType#toString() . 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: TypeUtility.java    From kripton with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a string with type parameters replaced with wildcards. This is slightly different from {@link Types#erasure(javax.lang.model.type.TypeMirror)}, which removes all
 * type parameter data.
 * 
 * For instance, if there is a field with type List<String>, this returns a string List<?>.
 *
 * @param declaredType
 *            the declared type
 * @return the canonical type name
 */
private static String getCanonicalTypeName(DeclaredType declaredType) {
	List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
	if (!typeArguments.isEmpty()) {
		StringBuilder typeString = new StringBuilder(declaredType.asElement().toString());
		typeString.append('<');
		for (int i = 0; i < typeArguments.size(); i++) {
			if (i > 0) {
				typeString.append(',');
			}
			typeString.append('?');
		}
		typeString.append('>');

		return typeString.toString();
	} else {
		return declaredType.toString();
	}
}
 
Example 2
Source File: TypeUtils.java    From ig-json-parser with MIT License 6 votes vote down vote up
/**
 * Returns a string with type parameters replaced with wildcards. This is slightly different from
 * {@link Types#erasure(javax.lang.model.type.TypeMirror)}, which removes all type parameter data.
 *
 * <p>For instance, if there is a field with type List&lt;String&gt;, this returns a string
 * List&lt;?&gt;.
 */
private String getCanonicalTypeName(DeclaredType declaredType) {
  List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
  if (!typeArguments.isEmpty()) {
    StringBuilder typeString = new StringBuilder(declaredType.asElement().toString());
    typeString.append('<');
    for (int i = 0; i < typeArguments.size(); i++) {
      if (i > 0) {
        typeString.append(',');
      }
      typeString.append('?');
    }
    typeString.append('>');

    return typeString.toString();
  } else {
    return declaredType.toString();
  }
}
 
Example 3
Source File: ModuleModel.java    From vertx-codegen with Apache License 2.0 6 votes vote down vote up
private void isLegalJsonMapper(Elements elementUtils, Types typeUtils, DeclaredType mapperType) {
  TypeElement mapperDeclaredElement = (TypeElement) mapperType.asElement();
  if (elementUtils
    .getAllMembers(mapperDeclaredElement)
    .stream()
    .noneMatch(e ->
      e.getKind() == ElementKind.FIELD &&
        e.getModifiers().containsAll(Arrays.asList(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)) &&
        e.getSimpleName().contentEquals("INSTANCE") &&
        typeUtils.isSameType(e.asType(), mapperType)
    )) {
    throw new GenException(mapperDeclaredElement, "The json mapper " + mapperType.toString() + " must have a public static final INSTANCE field of the mapper type");
  }
  TypeElement mapperElt = elementUtils.getTypeElement("io.vertx.core.spi.json.JsonMapper");
  TypeParameterElement jsonElt = mapperElt.getTypeParameters().get(1);
  TypeMirror jsonType = Helper.resolveTypeParameter(typeUtils, mapperType, jsonElt);
  if (jsonType != null && !isLegalJsonType(jsonType)) {
    throw new GenException(
      mapperType.asElement(),
      "The specified json type in mapper " + mapperType.toString() + " is not a valid json type. Allowed types are java.lang.Boolean, java.lang.Number, java.lang.String and BOXED_PRIMITIVE"
    );
  }
}
 
Example 4
Source File: InjectedDependencies.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public String use(ProcessingEnvironment env, DeclaredType type) {
    for (WellKnownDependency wellKnown : WellKnownDependency.values()) {
        if (env.getTypeUtils().isAssignable(wellKnown.getType(env), type)) {
            return use(wellKnown);
        }
    }

    String typeName = type.toString();
    Dependency ret = deps.get(typeName);
    if (ret == null) {
        ret = new InjectedDependency("injected" + type.asElement().getSimpleName(), typeName);
        deps.put(typeName, ret);
    }
    return ret.name;
}