javax.lang.model.element.Parameterizable Java Examples

The following examples show how to use javax.lang.model.element.Parameterizable. 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: PrintingProcessor.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
private void printFormalTypeParameters(Parameterizable e,
                                       boolean pad) {
    List<? extends TypeParameterElement> typeParams = e.getTypeParameters();
    if (typeParams.size() > 0) {
        writer.print("<");

        boolean first = true;
        for(TypeParameterElement tpe: typeParams) {
            if (!first)
                writer.print(", ");
            printAnnotationsInline(tpe);
            writer.print(tpe.toString());
            first = false;
        }

        writer.print(">");
        if (pad)
            writer.print(" ");
    }
}
 
Example #2
Source File: PrintingProcessor.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
private void printFormalTypeParameters(Parameterizable e,
                                       boolean pad) {
    List<? extends TypeParameterElement> typeParams = e.getTypeParameters();
    if (typeParams.size() > 0) {
        writer.print("<");

        boolean first = true;
        for(TypeParameterElement tpe: typeParams) {
            if (!first)
                writer.print(", ");
            printAnnotationsInline(tpe);
            writer.print(tpe.toString());
            first = false;
        }

        writer.print(">");
        if (pad)
            writer.print(" ");
    }
}
 
Example #3
Source File: JavaEnvironment.java    From j2cl with Apache License 2.0 6 votes vote down vote up
private static List<TypeMirror> getTypeArguments(DeclaredType declaredType) {
  List<TypeMirror> typeArguments = new ArrayList<>();
  DeclaredType currentType = declaredType;
  do {
    typeArguments.addAll(currentType.getTypeArguments());
    Element enclosingElement = currentType.asElement().getEnclosingElement();
    if (enclosingElement.getKind() == ElementKind.METHOD
        || enclosingElement.getKind() == ElementKind.CONSTRUCTOR) {
      typeArguments.addAll(
          ((Parameterizable) enclosingElement)
              .getTypeParameters().stream().map(Element::asType).collect(toImmutableList()));
    }
    currentType =
        currentType.getEnclosingType() instanceof DeclaredType
            ? (DeclaredType) currentType.getEnclosingType()
            : null;
  } while (currentType != null);
  return typeArguments;
}
 
Example #4
Source File: JavaEnvironment.java    From j2cl with Apache License 2.0 6 votes vote down vote up
private static List<TypeParameterElement> getTypeParameters(TypeElement typeElement) {
  List<TypeParameterElement> typeParameterElements =
      new ArrayList<>(typeElement.getTypeParameters());
  Element currentElement = typeElement;
  Element enclosingElement = typeElement.getEnclosingElement();
  while (enclosingElement != null) {
    if (isStatic(currentElement)) {
      break;
    }

    if (enclosingElement.getKind() != ElementKind.STATIC_INIT
        && enclosingElement.getKind() != ElementKind.INSTANCE_INIT
        && enclosingElement instanceof Parameterizable) {
      // Add the enclosing element type variables, skip STATIC_INIT and INSTANCE_INIT since they
      // never define type variables, and throw NPE if getTypeParameters is called on them.
      typeParameterElements.addAll(((Parameterizable) enclosingElement).getTypeParameters());
    }
    currentElement = enclosingElement;
    enclosingElement = enclosingElement.getEnclosingElement();
  }
  return typeParameterElements;
}
 
Example #5
Source File: AccessorAttributesCollector.java    From immutables with Apache License 2.0 6 votes vote down vote up
void collect() {
  collectGeneratedCandidateMethods(getTypeElement());

  Instantiator encodingInstantiator = protoclass.encodingInstantiator();
  @Nullable InstantiationCreator instantiationCreator =
      encodingInstantiator.creatorFor((Parameterizable) type.element);

  for (ValueAttribute attribute : attributes) {
    attribute.initAndValidate(instantiationCreator);
  }

  if (instantiationCreator != null) {
    type.additionalImports(instantiationCreator.imports);
  }

  type.attributes.addAll(attributes);
  type.accessorMapping = accessorMapping;
}
 
Example #6
Source File: MoreElements.java    From doma with Apache License 2.0 5 votes vote down vote up
public TypeParametersDef getTypeParametersDef(Parameterizable element) {
  assertNotNull(element);

  List<? extends TypeParameterElement> typeParameters = element.getTypeParameters();
  List<String> typeParameterNames = getTypeParameterNames(typeParameters);
  Iterator<String> values = typeParameterNames.iterator();

  Iterator<? extends TypeParameterElement> keys = typeParameters.iterator();

  LinkedHashMap<TypeParameterElement, String> map = new LinkedHashMap<>();
  while (keys.hasNext() && values.hasNext()) {
    map.put(keys.next(), values.next());
  }
  return new TypeParametersDef(map);
}
 
Example #7
Source File: CompilerTreeApiTest.java    From buck with Apache License 2.0 5 votes vote down vote up
protected TypeParameterElement findTypeParameter(String name, Parameterizable element) {
  for (TypeParameterElement parameter : element.getTypeParameters()) {
    if (parameter.getSimpleName().contentEquals(name)) {
      return parameter;
    }
  }

  throw new IllegalArgumentException(
      String.format("No such parameter on %s: %s", element.getSimpleName(), name));
}
 
Example #8
Source File: ValueType.java    From immutables with Apache License 2.0 5 votes vote down vote up
private TypeExtractor getTypeExtractor() {
  if (typeExtractor == null) {
    this.typeExtractor = new TypeExtractor(
        Proto.TYPE_FACTORY,
        (Parameterizable) element);
  }
  return typeExtractor;
}
 
Example #9
Source File: TypeExtractor.java    From immutables with Apache License 2.0 5 votes vote down vote up
private Type.Parameters initParameters(Parameterizable context) {
  Type.Parameters parameters = factory.parameters();

  for (TypeParameterElement p : context.getTypeParameters()) {
    String name = p.getSimpleName().toString();
    // <T extends Cls<T>>: when parsing bounds for T, T should be already defined for recursion
    Type.Parameters parameterForRecursion = parameters.recursive(name);
    List<Type.Defined> bounds = getBounds(parameterForRecursion, p);
    parameters = parameters.introduce(name, bounds);
  }

  return parameters;
}
 
Example #10
Source File: Encodings.java    From immutables with Apache License 2.0 5 votes vote down vote up
private List<String> getTypeParameterNames(Parameterizable element) {
  List<String> names = new ArrayList<>();
  for (TypeParameterElement p : element.getTypeParameters()) {
    names.add(p.getSimpleName().toString());
  }
  return names;
}
 
Example #11
Source File: TypeExtractor.java    From immutables with Apache License 2.0 4 votes vote down vote up
public TypeExtractor(Type.Factory factory, Parameterizable context) {
  this.factory = factory;
  this.parameters = initParameters(context);
  this.parser = new Type.Parser(factory, parameters);
}
 
Example #12
Source File: Instantiator.java    From immutables with Apache License 2.0 4 votes vote down vote up
public @Nullable InstantiationCreator creatorFor(Parameterizable element) {
  return !isEmpty() ? new InstantiationCreator(element) : null;
}
 
Example #13
Source File: Instantiator.java    From immutables with Apache License 2.0 4 votes vote down vote up
InstantiationCreator(Parameterizable parameterizable) {
  this.typeExtractor = new TypeExtractor(typeFactory, parameterizable);
}