javax.lang.model.util.Types.asMemberOf()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(9.9k)|赞(0)|评价(0)|浏览(139)

本文整理了Java中javax.lang.model.util.Types.asMemberOf()方法的一些代码示例,展示了Types.asMemberOf()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Types.asMemberOf()方法的具体详情如下:
包路径:javax.lang.model.util.Types
类名称:Types
方法名:asMemberOf

Types.asMemberOf介绍

[英]Returns the type of an element when that element is viewed as a member of, or otherwise directly contained by, a given type. For example, when viewed as a member of the parameterized type Set, the Set.add method is an ExecutableTypewhose parameter is of type String.
[中]当元素被视为给定类型的成员或直接包含在给定类型中时,返回该元素的类型。例如,当作为参数化类型集的成员查看时,该集。add方法是一个可执行类型,其参数为String类型。

代码示例

代码示例来源:origin: immutables/immutables

static TypeMirror asInheritedMemberReturnType(
  ProcessingEnvironment processing,
  TypeElement typeElement,
  ExecutableElement method) {
 ExecutableType asMethodOfType =
   (ExecutableType) processing.getTypeUtils()
     .asMemberOf((DeclaredType) typeElement.asType(), method);
 return asMethodOfType.getReturnType();
}

代码示例来源:origin: immutables/immutables

final BoundAccessor bind(TypeMirror target) {
 // asMemberOf wrongly implemented in ECJ,
 // we use it for fields only in Javac
 // but we expect it is already resolved
 TypeMirror type = !inEclipseCompiler
   ? types.asMemberOf((DeclaredType) target, element)
   : element.asType();
 if (type instanceof ExecutableType) {
  type = ((ExecutableType) type).getReturnType();
 }
 return new BoundAccessor(this, target, type);
}

代码示例来源:origin: square/javapoet

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

代码示例来源:origin: inferred/FreeBuilder

@Override public boolean test(ExecutableElement element) {
  if (element.getParameters().size() != 1) {
   return false;
  }
  if (!element.getSimpleName().contentEquals("mergeFrom")) {
   return false;
  }
  ExecutableType method = (ExecutableType) types.asMemberOf(builder, element);
  if (!types.isSubtype(parameter, method.getParameterTypes().get(0))) {
   return false;
  }
  return true;
 }
}

代码示例来源:origin: com.google.gwt/gwt-servlet

protected static ExecutableType viewIn(TypeElement lookIn, ExecutableElement methodElement,
  State state) {
 // Do not use state.types.getDeclaredType, as we really want a
 // "prototypical" type, and not a raw type.
 // This is important when a proxy maps to a generic domain type:
 // state.types.getDeclaredType without typeArgs would return the raw type,
 // and asMemberOf would then return an ExecutableType using raw types too.
 // For instance, if a class Foo<T> contains a method whose return type is
 // List<String> (note it doesn't even make use of the T type parameter),
 // then if we were to use raw types, the returned type of the ExecutableType
 // would be the raw type java.util.List, and not List<String>. Using
 // asType(), we'd get the expected List<String> though; and for a List<T>,
 // we'd get a List<Object> (or whichever upper bound for the T type
 // parameter).
 try {
  return (ExecutableType) state.types.asMemberOf((DeclaredType) lookIn.asType(), methodElement);
 } catch (IllegalArgumentException e) {
  return (ExecutableType) methodElement.asType();
 }
}

代码示例来源:origin: inferred/FreeBuilder

@Override public boolean test(ExecutableElement element) {
  if (element.getParameters().size() != 0) {
   return false;
  }
  if (!element.getSimpleName().contentEquals("toBuilder")) {
   return false;
  }
  ExecutableType method = (ExecutableType) types.asMemberOf(datatype, element);
  if (!types.isSubtype(method.getReturnType(), builder)) {
   return false;
  }
  return true;
 }
}

代码示例来源:origin: inferred/FreeBuilder

/**
 * Determines the return type of {@code method}, if called on an instance of type {@code type}.
 *
 * <p>For instance, in this example, myY.getProperty() returns List&lt;T&gt;, not T:<pre><code>
 *    interface X&lt;T&gt; {
 *      T getProperty();
 *    }
 *    &#64;FreeBuilder interface Y&lt;T&gt; extends X&lt;List&lt;T&gt;&gt; { }</code></pre>
 *
 * <p>(Unfortunately, a bug in Eclipse prevents us handling these cases correctly at the moment.
 * javac works fine.)
 */
public static TypeMirror getReturnType(TypeElement type, ExecutableElement method, Types types) {
 try {
  ExecutableType executableType = (ExecutableType)
    types.asMemberOf((DeclaredType) type.asType(), method);
  return executableType.getReturnType();
 } catch (IllegalArgumentException e) {
  // Eclipse incorrectly throws an IllegalArgumentException here:
  //    "element is not valid for the containing declared type"
  // As a workaround for the common case, fall back to the declared return type.
  return method.getReturnType();
 }
}

代码示例来源:origin: inferred/FreeBuilder

/** Returns the functional types accepted by {@code methodName} on {@code type}. */
public static List<FunctionalType> functionalTypesAcceptedByMethod(
  DeclaredType type,
  String methodName,
  Elements elements,
  Types types) {
 TypeElement typeElement = asElement(type);
 return methodsOn(typeElement, elements, errorType -> { })
   .stream()
   .filter(method -> method.getSimpleName().contentEquals(methodName)
     && method.getParameters().size() == 1)
   .flatMap(method -> {
    ExecutableType methodType = (ExecutableType) types.asMemberOf(type, method);
    TypeMirror parameter = getOnlyElement(methodType.getParameterTypes());
    return maybeFunctionalType(parameter, elements, types)
      .map(Stream::of).orElse(Stream.of());
   })
   .collect(toList());
}

代码示例来源:origin: inferred/FreeBuilder

public static Optional<FunctionalType> maybeFunctionalType(
  DeclaredType type,
  Elements elements,
  Types types) {
 TypeElement typeElement = asElement(type);
 if (!typeElement.getKind().isInterface()) {
  return Optional.empty();
 }
 Set<ExecutableElement> abstractMethods =
   only(ABSTRACT, methodsOn(typeElement, elements, errorType -> { }));
 if (abstractMethods.size() != 1) {
  return Optional.empty();
 }
 ExecutableElement method = getOnlyElement(abstractMethods);
 ExecutableType methodType = (ExecutableType) types.asMemberOf(type, method);
 return Optional.of(new FunctionalType(
   Type.from(type),
   method.getSimpleName().toString(),
   methodType.getParameterTypes(),
   methodType.getReturnType()));
}

代码示例来源:origin: org.uberfire/uberfire-workbench-processors

@Override
public TypeMirror asMemberOf(DeclaredType arg0,
               Element arg1) {
  return realImpl.asMemberOf(arg0,
                arg1);
}

代码示例来源:origin: org.mapstruct/mapstruct-processor

/**
 * Get the Type for given method as part of usedMapper. Possibly parameterized types in method declaration will be
 * evaluated to concrete types then.
 *
 * @param includingType the type on which's scope the method type shall be evaluated
 * @param method the method
 *
 * @return the ExecutableType representing the method as part of usedMapper
 */
public TypeMirror getMethodType(DeclaredType includingType, Element method) {
  return typeUtils.asMemberOf( includingType, method );
}

代码示例来源:origin: com.google.dagger/dagger-compiler

@Override
 public TypeMirror asMemberOf(DeclaredType containing, Element element) {
  return types.asMemberOf(containing, element);
 }
}

代码示例来源:origin: kiegroup/appformer

@Override
public TypeMirror asMemberOf(DeclaredType arg0,
               Element arg1) {
  return realImpl.asMemberOf(arg0,
                arg1);
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-web-beans

private TypeMirror getParameterType( VariableElement var,
    ExecutableElement element, TypeElement parent , 
    CompilationController controller )
{
  ExecutableType method = (ExecutableType)controller.getTypes().asMemberOf(
      (DeclaredType)parent.asType(), element);
  List<? extends TypeMirror> parameterTypes = method.getParameterTypes();
  List<? extends VariableElement> parameters = element.getParameters();
  int paramIndex = parameters.indexOf(var);
  return parameterTypes.get(paramIndex);
}

代码示例来源:origin: net.digitalid.utility/utility-generator

protected ExecutableInformation(@Nonnull Element element, @Nonnull DeclaredType containingType, @Nonnull TypeInformation typeInformation) {
  super(element, StaticProcessingEnvironment.getTypeUtils().asMemberOf(containingType, element), containingType);
  
  final @Nonnull List<MethodParameterInformation> parameters = new ArrayList<>(getElement().getParameters().size());
  for (@Nonnull VariableElement variableElement : getElement().getParameters()) {
    parameters.add(new MethodParameterInformation(variableElement, getContainingType(), typeInformation));
  }
  this.parameters = FiniteIterable.of(parameters);
}

代码示例来源:origin: fr.xebia.extras/selma-processor

public MethodWrapper(ExecutableElement method, DeclaredType declaredType, MapperGeneratorContext context) {
  this.method = method;
  this.parentType = declaredType;
  this.context = context;
  // Try to resolve any inherited or declared generic type using asMemberOf processed class
  this.executableType = (ExecutableType) context.type.asMemberOf(parentType, method);
  AnnotationWrapper annotationWrapper = AnnotationWrapper.buildFor(context, method, Mapper.class);
  if (annotationWrapper != null) {
    ignoreMissingProperties = annotationWrapper.getAsBoolean("ignoreMissingProperties");
  }
}

代码示例来源:origin: stoicflame/enunciate

public TypeMirror asMemberOf(DeclaredType containing, Element element) {
 while (containing instanceof DecoratedDeclaredType) {
  containing = ((DecoratedDeclaredType) containing).getDelegate();
 }
 while (element instanceof DecoratedElement) {
  element = ((DecoratedElement) element).getDelegate();
 }
 return TypeMirrorDecorator.decorate(delegate.asMemberOf(containing, element), this.env);
}

代码示例来源:origin: com.webcohesion.enunciate/enunciate-javac-support

public TypeMirror asMemberOf(DeclaredType containing, Element element) {
 while (containing instanceof DecoratedDeclaredType) {
  containing = ((DecoratedDeclaredType) containing).getDelegate();
 }
 while (element instanceof DecoratedElement) {
  element = ((DecoratedElement) element).getDelegate();
 }
 return TypeMirrorDecorator.decorate(delegate.asMemberOf(containing, element), this.env);
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-web-beans

@Override
public TypeMirror getType( WebBeansModelImplementation model, 
    DeclaredType parent, VariableElement element ) 
{
  return model.getHelper().getCompilationController().getTypes().
    asMemberOf(parent, element );
}

代码示例来源:origin: net.digitalid.utility/utility-generator

public @Nullable TypeMirror getReturnType() {
  if (!hasReturnType()) {
    return null;
  }
  final @Nonnull TypeMirror typeMirror = StaticProcessingEnvironment.getTypeUtils().asMemberOf(getContainingType(), getElement());
  Require.that(typeMirror instanceof ExecutableType).orThrow("Expected ExecutableType, but got $", getContainingType());

  final @Nonnull ExecutableType executableType = (ExecutableType) typeMirror;
  return executableType.getReturnType();
}

相关文章