Java Code Examples for javax.lang.model.element.TypeParameterElement#asType()

The following examples show how to use javax.lang.model.element.TypeParameterElement#asType() . 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: ElementUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private DeclaredType getDeclaredType(TypeElement e, HashMap<? extends Element, ? extends TypeMirror> map, Types types) {
    List<? extends TypeParameterElement> tpes = e.getTypeParameters();
    TypeMirror[] targs = new TypeMirror[tpes.size()];
    int i = 0;
    for (Iterator<? extends TypeParameterElement> it = tpes.iterator(); it.hasNext();) {
        TypeParameterElement tpe = it.next();
        TypeMirror t = map.get(tpe);
        targs[i++] = t != null ? t : tpe.asType();
    }
    Element encl = e.getEnclosingElement();
    if ((encl.getKind().isClass() || encl.getKind().isInterface()) && !((TypeElement)encl).getTypeParameters().isEmpty())
            return types.getDeclaredType(getDeclaredType((TypeElement)encl, map, types), e, targs);
    return types.getDeclaredType(e, targs);
}
 
Example 2
Source File: JavadocCompletionQuery.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private DeclaredType getDeclaredType(TypeElement e, HashMap<? extends Element, ? extends TypeMirror> map, Types types) {
    List<? extends TypeParameterElement> tpes = e.getTypeParameters();
    TypeMirror[] targs = new TypeMirror[tpes.size()];
    int i = 0;
    for (Iterator<? extends TypeParameterElement> it = tpes.iterator(); it.hasNext();) {
        TypeParameterElement tpe = it.next();
        TypeMirror t = map.get(tpe);
        targs[i++] = t != null ? t : tpe.asType();
    }
    Element encl = e.getEnclosingElement();
    if ((encl.getKind().isClass() || encl.getKind().isInterface()) && !((TypeElement)encl).getTypeParameters().isEmpty())
            return types.getDeclaredType(getDeclaredType((TypeElement)encl, map, types), e, targs);
    return types.getDeclaredType(e, targs);
}
 
Example 3
Source File: MethodSpec.java    From JReFrameworker with MIT License 5 votes vote down vote up
/**
 * Returns a new method spec builder that overrides {@code method}.
 *
 * <p>This will copy its visibility modifiers, type parameters, return type, name, parameters, and
 * throws declarations. An {@link Override} annotation will be added.
 *
 * <p>Note that in JavaPoet 1.2 through 1.7 this method retained annotations from the method and
 * parameters of the overridden method. Since JavaPoet 1.8 annotations must be added separately.
 */
public static Builder overriding(ExecutableElement method) {
  checkNotNull(method, "method == null");

  Set<Modifier> modifiers = method.getModifiers();
  if (modifiers.contains(Modifier.PRIVATE)
      || modifiers.contains(Modifier.FINAL)
      || modifiers.contains(Modifier.STATIC)) {
    throw new IllegalArgumentException("cannot override method with modifiers: " + modifiers);
  }

  String methodName = method.getSimpleName().toString();
  MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(methodName);

  methodBuilder.addAnnotation(Override.class);

  modifiers = new LinkedHashSet<>(modifiers);
  modifiers.remove(Modifier.ABSTRACT);
  modifiers.remove(Util.DEFAULT); // LinkedHashSet permits null as element for Java 7
  methodBuilder.addModifiers(modifiers);

  for (TypeParameterElement typeParameterElement : method.getTypeParameters()) {
    TypeVariable var = (TypeVariable) typeParameterElement.asType();
    methodBuilder.addTypeVariable(TypeVariableName.get(var));
  }

  methodBuilder.returns(TypeName.get(method.getReturnType()));
  methodBuilder.addParameters(ParameterSpec.parametersOf(method));
  methodBuilder.varargs(method.isVarArgs());

  for (TypeMirror thrownType : method.getThrownTypes()) {
    methodBuilder.addException(TypeName.get(thrownType));
  }

  return methodBuilder;
}
 
Example 4
Source File: MethodSpec.java    From JReFrameworker with MIT License 5 votes vote down vote up
/**
 * Returns a new method spec builder that overrides {@code method}.
 *
 * <p>This will copy its visibility modifiers, type parameters, return type, name, parameters, and
 * throws declarations. An {@link Override} annotation will be added.
 *
 * <p>Note that in JavaPoet 1.2 through 1.7 this method retained annotations from the method and
 * parameters of the overridden method. Since JavaPoet 1.8 annotations must be added separately.
 */
public static Builder overriding(ExecutableElement method) {
  checkNotNull(method, "method == null");

  Set<Modifier> modifiers = method.getModifiers();
  if (modifiers.contains(Modifier.PRIVATE)
      || modifiers.contains(Modifier.FINAL)
      || modifiers.contains(Modifier.STATIC)) {
    throw new IllegalArgumentException("cannot override method with modifiers: " + modifiers);
  }

  String methodName = method.getSimpleName().toString();
  MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(methodName);

  methodBuilder.addAnnotation(Override.class);

  modifiers = new LinkedHashSet<>(modifiers);
  modifiers.remove(Modifier.ABSTRACT);
  modifiers.remove(Util.DEFAULT); // LinkedHashSet permits null as element for Java 7
  methodBuilder.addModifiers(modifiers);

  for (TypeParameterElement typeParameterElement : method.getTypeParameters()) {
    TypeVariable var = (TypeVariable) typeParameterElement.asType();
    methodBuilder.addTypeVariable(TypeVariableName.get(var));
  }

  methodBuilder.returns(TypeName.get(method.getReturnType()));
  methodBuilder.addParameters(ParameterSpec.parametersOf(method));
  methodBuilder.varargs(method.isVarArgs());

  for (TypeMirror thrownType : method.getThrownTypes()) {
    methodBuilder.addException(TypeName.get(thrownType));
  }

  return methodBuilder;
}
 
Example 5
Source File: StandaloneTypeVariableTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsElement() throws IOException {
  compile("class Foo<T> { }");

  TypeParameterElement tElement = elements.getTypeElement("Foo").getTypeParameters().get(0);
  TypeVariable tVar = (TypeVariable) tElement.asType();

  assertSame(tElement, tVar.asElement());
}
 
Example 6
Source File: CompilerTreeApiTest.java    From buck with Apache License 2.0 5 votes vote down vote up
protected TypeMirror getTypeParameterUpperBound(String typeName, int typeParameterIndex) {
  TypeParameterElement typeParameter =
      elements.getTypeElement(typeName).getTypeParameters().get(typeParameterIndex);
  TypeVariable typeVariable = (TypeVariable) typeParameter.asType();

  return typeVariable.getUpperBound();
}
 
Example 7
Source File: MethodSpec.java    From javapoet with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a new method spec builder that overrides {@code method}.
 *
 * <p>This will copy its visibility modifiers, type parameters, return type, name, parameters, and
 * throws declarations. An {@link Override} annotation will be added.
 *
 * <p>Note that in JavaPoet 1.2 through 1.7 this method retained annotations from the method and
 * parameters of the overridden method. Since JavaPoet 1.8 annotations must be added separately.
 */
public static Builder overriding(ExecutableElement method) {
  checkNotNull(method, "method == null");

  Element enclosingClass = method.getEnclosingElement();
  if (enclosingClass.getModifiers().contains(Modifier.FINAL)) {
    throw new IllegalArgumentException("Cannot override method on final class " + enclosingClass);
  }

  Set<Modifier> modifiers = method.getModifiers();
  if (modifiers.contains(Modifier.PRIVATE)
      || modifiers.contains(Modifier.FINAL)
      || modifiers.contains(Modifier.STATIC)) {
    throw new IllegalArgumentException("cannot override method with modifiers: " + modifiers);
  }

  String methodName = method.getSimpleName().toString();
  MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(methodName);

  methodBuilder.addAnnotation(Override.class);

  modifiers = new LinkedHashSet<>(modifiers);
  modifiers.remove(Modifier.ABSTRACT);
  modifiers.remove(Modifier.DEFAULT);
  methodBuilder.addModifiers(modifiers);

  for (TypeParameterElement typeParameterElement : method.getTypeParameters()) {
    TypeVariable var = (TypeVariable) typeParameterElement.asType();
    methodBuilder.addTypeVariable(TypeVariableName.get(var));
  }

  methodBuilder.returns(TypeName.get(method.getReturnType()));
  methodBuilder.addParameters(ParameterSpec.parametersOf(method));
  methodBuilder.varargs(method.isVarArgs());

  for (TypeMirror thrownType : method.getThrownTypes()) {
    methodBuilder.addException(TypeName.get(thrownType));
  }

  return methodBuilder;
}