当前位置: 首页>>代码示例>>Java>>正文


Java TypeParameterElement.asType方法代码示例

本文整理汇总了Java中javax.lang.model.element.TypeParameterElement.asType方法的典型用法代码示例。如果您正苦于以下问题:Java TypeParameterElement.asType方法的具体用法?Java TypeParameterElement.asType怎么用?Java TypeParameterElement.asType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.lang.model.element.TypeParameterElement的用法示例。


在下文中一共展示了TypeParameterElement.asType方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: overriding

import javax.lang.model.element.TypeParameterElement; //导入方法依赖的package包/类
static MethodSpec.Builder overriding(ExecutableElement method) {
  String methodName = method.getSimpleName().toString();

  MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName)
      .addAnnotation(Override.class);

  Set<Modifier> modifiers = method.getModifiers();
  modifiers = new LinkedHashSet<>(modifiers);
  modifiers.remove(Modifier.ABSTRACT);
  Modifier defaultModifier = null;
  // Modifier.DEFAULT doesn't exist until Java 8.
  try {
    defaultModifier = Modifier.valueOf("DEFAULT");
  } catch (IllegalArgumentException e) {
    // Ignored.
  }
  modifiers.remove(defaultModifier);

  builder = builder.addModifiers(modifiers);

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

  builder = builder.returns(TypeName.get(method.getReturnType()))
      .addParameters(getParameters(method))
      .varargs(method.isVarArgs());

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

  return builder;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:36,代码来源:ProcessorUtil.java

示例2: getDeclaredType

import javax.lang.model.element.TypeParameterElement; //导入方法依赖的package包/类
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);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:15,代码来源:ElementUtilities.java

示例3: overriding

import javax.lang.model.element.TypeParameterElement; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:JReFrameworker,项目名称:JReFrameworker,代码行数:45,代码来源:MethodSpec.java

示例4: buildMethod

import javax.lang.model.element.TypeParameterElement; //导入方法依赖的package包/类
/**
 * Create a method spec builder which copies {@code method} that is viewed as being a member of the specified {@code containing} class or
 * interface.
 *
 * This will copy its visibility modifiers, type parameters, return type, name, parameters, and throws declarations.
 */
public MethodSpec.Builder buildMethod(ExecutableElement method, DeclaredType containing) {
  Objects.requireNonNull(method, "method == null");
  Objects.requireNonNull(containing, "containing == null");
  ExecutableType executableType = (ExecutableType) types.asMemberOf(containing, method);
  String methodName = method.getSimpleName().toString();
  MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName);
  builder.addAnnotations(Poetry.annotations(method.getAnnotationMirrors()));
  builder.addModifiers(method.getModifiers());
  builder.returns(Poetry.annotated(executableType.getReturnType()));
  for (TypeParameterElement typeParameterElement : method.getTypeParameters()) {
    TypeVariable var = (TypeVariable) typeParameterElement.asType();
    builder.addTypeVariable(TypeVariableName.get(var));
  }
  List<? extends VariableElement> parameters = method.getParameters();
  List<? extends TypeMirror> parameterTypes = executableType.getParameterTypes();
  for (int index = 0; index < parameters.size(); index++) {
    VariableElement parameter = parameters.get(index);
    TypeName type = TypeName.get(parameterTypes.get(index)); // annotations are grabbed later
    String name = parameter.getSimpleName().toString();
    Modifier[] paramods = new Modifier[parameter.getModifiers().size()];
    parameter.getModifiers().toArray(paramods);
    ParameterSpec.Builder psb = ParameterSpec.builder(type, name, paramods);
    for (AnnotationMirror mirror : parameter.getAnnotationMirrors()) {
      psb.addAnnotation(AnnotationSpec.get(mirror));
    }
    builder.addParameter(psb.build());
  }
  builder.varargs(method.isVarArgs());
  for (TypeMirror thrownType : method.getThrownTypes()) {
    builder.addException(TypeName.get(thrownType));
  }
  return builder;
}
 
开发者ID:sormuras,项目名称:poetry,代码行数:40,代码来源:ModelPoetry.java

示例5: getBuilder

import javax.lang.model.element.TypeParameterElement; //导入方法依赖的package包/类
/**
 * This is a copy of {@link MethodSpec#overriding(ExecutableElement, DeclaredType, Types)}
 * but without adding an {@link Override} annotation
 * @param method method
 * @param enclosing enclosing type
 * @param env annotation processor environment
 * @return Builder
 */
public static MethodSpec.Builder getBuilder(ExecutableElement method, DeclaredType enclosing, ProcessingEnvironment env) {
    checkNotNull(method, "method == null");

    Types types = env.getTypeUtils();
    Elements elements = env.getElementUtils();

    ExecutableType executableType = (ExecutableType) types.asMemberOf(enclosing, method);
    List<? extends TypeMirror> resolvedParameterTypes = executableType.getParameterTypes();
    TypeMirror resolvedReturnType = executableType.getReturnType();

    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);

    modifiers = new LinkedHashSet<Modifier>(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(resolvedReturnType));

    for (int i = 0; i < resolvedParameterTypes.size(); i++) {
        TypeMirror paramTypeMirror = resolvedParameterTypes.get(i);
        VariableElement parameter = method.getParameters().get(i);

        TypeName name = TypeName.get(paramTypeMirror);
        ParameterSpec spec = ParameterSpec.builder(name, parameter.getSimpleName().toString())
                // Compiler clashes with addModifiers(Modifiers...) and addModifiers(Iterable<Modifiers>)
                // Convert it to array
                .addModifiers(modifiersToArray(parameter.getModifiers()))
                .build();

        methodBuilder.addParameter(spec);
    }
    methodBuilder.varargs(method.isVarArgs());

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

    String javaDoc = elements.getDocComment(method);
    if (javaDoc != null) {
        methodBuilder.addJavadoc(javaDoc, "");
    }

    return methodBuilder;
}
 
开发者ID:trollsoftware,项目名称:jcomposition,代码行数:67,代码来源:MethodSpecUtils.java

示例6: overriding

import javax.lang.model.element.TypeParameterElement; //导入方法依赖的package包/类
/**
 * Returns a new method spec builder that overrides {@code method}.
 * <p>
 * <p>This will copy its visibility modifiers, type parameters, return type, name, parameters, and
 * throws declarations. An {@link Override} annotation will be added.
 * <p>
 * <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);
    methodBuilder.addModifiers(modifiers);

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

    methodBuilder.returns(TypeName.get(method.getReturnType()));

    List<? extends VariableElement> parameters = method.getParameters();
    for (VariableElement parameter : parameters) {
        TypeName type = TypeName.get(parameter.asType());
        String name = parameter.getSimpleName().toString();
        Set<Modifier> parameterModifiers = parameter.getModifiers();
        ParameterSpec.Builder parameterBuilder = ParameterSpec.builder(type, name)
                .addModifiers(parameterModifiers.toArray(new Modifier[parameterModifiers.size()]));
        methodBuilder.addParameter(parameterBuilder.build());
    }
    methodBuilder.varargs(method.isVarArgs());

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

    return methodBuilder;
}
 
开发者ID:wrmsr,项目名称:wava,代码行数:55,代码来源:MethodSpec.java

示例7: overriding

import javax.lang.model.element.TypeParameterElement; //导入方法依赖的package包/类
/**
 * 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()));

  List<? extends VariableElement> parameters = method.getParameters();
  for (VariableElement parameter : parameters) {
    TypeName type = TypeName.get(parameter.asType());
    String name = parameter.getSimpleName().toString();
    Set<Modifier> parameterModifiers = parameter.getModifiers();
    ParameterSpec.Builder parameterBuilder = ParameterSpec.builder(type, name)
        .addModifiers(parameterModifiers.toArray(new Modifier[parameterModifiers.size()]));
    methodBuilder.addParameter(parameterBuilder.build());
  }
  methodBuilder.varargs(method.isVarArgs());

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

  return methodBuilder;
}
 
开发者ID:Ufkoku,项目名称:AndroidMVPHelper,代码行数:54,代码来源:MethodSpec.java


注:本文中的javax.lang.model.element.TypeParameterElement.asType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。