com.sun.tools.javac.code.Types.isFunctionalInterface()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(12.3k)|赞(0)|评价(0)|浏览(118)

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

Types.isFunctionalInterface介绍

[英]Is given type a functional interface?
[中]给定的类型是功能接口吗?

代码示例

代码示例来源:origin: google/error-prone

private static boolean maybeFunctionalInterface(Type type, Types types, VisitorState state) {
  try {
   return types.isFunctionalInterface(type);
  } catch (CompletionFailure e) {
   // Report completion errors to avoid e.g. https://github.com/bazelbuild/bazel/issues/4105
   Check.instance(state.context)
     .completionError((DiagnosticPosition) state.getPath().getLeaf(), e);
   return false;
  }
 }
}

代码示例来源:origin: konsoletyper/teavm-javac

/** Whether {@code t} and {@code s} are unrelated functional interface types. */
private boolean unrelatedFunctionalInterfaces(Type t, Type s) {
  return types.isFunctionalInterface(t.tsym) &&
      types.isFunctionalInterface(s.tsym) &&
      types.asSuper(t, s.tsym) == null &&
      types.asSuper(s, t.tsym) == null;
}

代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac

@Override
public boolean isFunctionalInterface(TypeElement element) {
  if (element.getKind() != ElementKind.INTERFACE)
    return false;
  else {
    TypeSymbol tsym = cast(TypeSymbol.class, element);
    return types.isFunctionalInterface(tsym);
  }
}

代码示例来源:origin: konsoletyper/teavm-javac

@Override
public boolean isFunctionalInterface(TypeElement element) {
  if (element.getKind() != ElementKind.INTERFACE)
    return false;
  else {
    TypeSymbol tsym = cast(TypeSymbol.class, element);
    return types.isFunctionalInterface(tsym);
  }
}

代码示例来源:origin: com.google.errorprone/error_prone_core

private static boolean maybeFunctionalInterface(Type type, Types types, VisitorState state) {
 try {
  return types.isFunctionalInterface(type);
 } catch (CompletionFailure e) {
  // Report completion errors to avoid e.g. https://github.com/bazelbuild/bazel/issues/4105
  Check.instance(state.context)
    .completionError((DiagnosticPosition) state.getPath().getLeaf(), e);
  return false;
 }
}

代码示例来源:origin: konsoletyper/teavm-javac

@Override
public void visitReference(JCMemberReference tree) {
  scan(tree.expr);
  if (inferenceContext.inferenceVars().contains(pt)) {
    stuckVars.add(pt);
    return;
  }
  if (!types.isFunctionalInterface(pt)) {
    return;
  }
  Type descType = types.findDescriptorType(pt);
  List<Type> freeArgVars = inferenceContext.freeVarsIn(descType.getParameterTypes());
  if (freeArgVars.nonEmpty() &&
      tree.overloadKind == JCMemberReference.OverloadKind.OVERLOADED) {
    stuckVars.addAll(freeArgVars);
    depVars.addAll(inferenceContext.freeVarsIn(descType.getReturnType()));
  }
}

代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac

@Override
public void visitLambda(JCLambda tree) {
  if (inferenceContext.inferenceVars().contains(pt)) {
    stuckVars.add(pt);
  }
  if (!types.isFunctionalInterface(pt)) {
    return;
  }
  Type descType = types.findDescriptorType(pt);
  List<Type> freeArgVars = inferenceContext.freeVarsIn(descType.getParameterTypes());
  if (tree.paramKind == JCLambda.ParameterKind.IMPLICIT &&
      freeArgVars.nonEmpty()) {
    stuckVars.addAll(freeArgVars);
    depVars.addAll(inferenceContext.freeVarsIn(descType.getReturnType()));
  }
  scanLambdaBody(tree, descType.getReturnType());
}

代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac

@Override
public void visitReference(JCMemberReference tree) {
  scan(tree.expr);
  if (inferenceContext.inferenceVars().contains(pt)) {
    stuckVars.add(pt);
    return;
  }
  if (!types.isFunctionalInterface(pt)) {
    return;
  }
  Type descType = types.findDescriptorType(pt);
  List<Type> freeArgVars = inferenceContext.freeVarsIn(descType.getParameterTypes());
  if (freeArgVars.nonEmpty() &&
      tree.overloadKind == JCMemberReference.OverloadKind.OVERLOADED) {
    stuckVars.addAll(freeArgVars);
    depVars.addAll(inferenceContext.freeVarsIn(descType.getReturnType()));
  }
}

代码示例来源:origin: org.netbeans.api/org-netbeans-modules-java-source-base

/**
 * Find the element of the method descriptor associated to the functional interface.
 * 
 * @param origin functional interface element
 * @return associated method descriptor element or <code>null</code> if the <code>origin</code> is not a functional interface.
 * @since 2.14
 */
public ExecutableElement getDescriptorElement(TypeElement origin) {
  com.sun.tools.javac.code.Types types = com.sun.tools.javac.code.Types.instance(info.impl.getJavacTask().getContext());
  if (types.isFunctionalInterface((TypeSymbol)origin)) {
    Symbol sym = types.findDescriptorSymbol((TypeSymbol)origin);
    if (sym != null && sym.getKind() == ElementKind.METHOD) {
      return (ExecutableElement)sym;
    }
  }
  return null;
}

代码示例来源:origin: konsoletyper/teavm-javac

@Override
public void visitLambda(JCLambda tree) {
  if (inferenceContext.inferenceVars().contains(pt)) {
    stuckVars.add(pt);
  }
  if (!types.isFunctionalInterface(pt)) {
    return;
  }
  Type descType = types.findDescriptorType(pt);
  List<Type> freeArgVars = inferenceContext.freeVarsIn(descType.getParameterTypes());
  if (tree.paramKind == JCLambda.ParameterKind.IMPLICIT &&
      freeArgVars.nonEmpty()) {
    stuckVars.addAll(freeArgVars);
    depVars.addAll(inferenceContext.freeVarsIn(descType.getReturnType()));
  }
  scanLambdaBody(tree, descType.getReturnType());
}

代码示例来源:origin: org.netbeans.api/org-netbeans-modules-java-source-base

/**
 * Find the type of the method descriptor associated to the functional interface.
 * 
 * @param origin functional interface type
 * @return associated method descriptor type or <code>null</code> if the <code>origin</code> is not a functional interface.
 * @since 0.112
 */
public ExecutableType getDescriptorType(DeclaredType origin) {
  Types types = Types.instance(info.impl.getJavacTask().getContext());
  if (types.isFunctionalInterface(((Type)origin).tsym)) {
    Type dt = types.findDescriptorType((Type)origin);
    if (dt != null && dt.getKind() == TypeKind.EXECUTABLE) {
      return (ExecutableType)dt;
    }
  }
  return null;
}

代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac

@Override
public void visitLambda(JCLambda tree) {
  if (types.isFunctionalInterface(t.tsym) &&
      types.isFunctionalInterface(s.tsym)) {
    Type desc_t = types.findDescriptorType(t);
    Type desc_s = types.findDescriptorType(s);
    if (types.isSameTypes(desc_t.getParameterTypes(),
        inferenceContext().asFree(desc_s.getParameterTypes()))) {
      if (types.asSuper(t, s.tsym) != null ||
        types.asSuper(s, t.tsym) != null) {
        result &= MostSpecificCheckContext.super.compatible(t, s, warn);
      } else if (!desc_s.getReturnType().hasTag(VOID)) {
        //perform structural comparison
        Type ret_t = desc_t.getReturnType();
        Type ret_s = desc_s.getReturnType();
        scanLambdaBody(tree, ret_t, ret_s);
      } else {
        return;
      }
    }
  } else {
    result &= false;
  }
}
//where

代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac

@Override
public void visitReference(JCMemberReference tree) {
  if (types.isFunctionalInterface(t.tsym) &&
      types.isFunctionalInterface(s.tsym)) {
    Type desc_t = types.findDescriptorType(t);
    Type desc_s = types.findDescriptorType(s);
    if (types.isSameTypes(desc_t.getParameterTypes(),
        inferenceContext().asFree(desc_s.getParameterTypes()))) {
      if (types.asSuper(t, s.tsym) != null ||
        types.asSuper(s, t.tsym) != null) {
        result &= MostSpecificCheckContext.super.compatible(t, s, warn);
      } else if (!desc_s.getReturnType().hasTag(VOID)) {
        //perform structural comparison
        Type ret_t = desc_t.getReturnType();
        Type ret_s = desc_s.getReturnType();
        result &= ((tree.refPolyKind == PolyKind.STANDALONE)
            ? standaloneMostSpecific(ret_t, ret_s, tree.sym.type.getReturnType(), warn)
            : polyMostSpecific(ret_t, ret_s, warn));
      } else {
        return;
      }
    }
  } else {
    result &= false;
  }
}

代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac

private void checkLambdaCandidate(JCNewClass tree, ClassSymbol csym, Type clazztype) {
  if (allowLambda &&
      identifyLambdaCandidate &&
      clazztype.hasTag(CLASS) &&
      !pt().hasTag(NONE) &&
      types.isFunctionalInterface(clazztype.tsym)) {
    Symbol descriptor = types.findDescriptorSymbol(clazztype.tsym);
    int count = 0;
    boolean found = false;
    for (Symbol sym : csym.members().getElements()) {
      if ((sym.flags() & SYNTHETIC) != 0 ||
          sym.isConstructor()) continue;
      count++;
      if (sym.kind != MTH ||
          !sym.name.equals(descriptor.name)) continue;
      Type mtype = types.memberType(clazztype, sym);
      if (types.overrideEquivalent(mtype, types.memberType(clazztype, descriptor))) {
        found = true;
      }
    }
    if (found && count == 1) {
      log.note(tree.def, "potential.lambda.found");
    }
  }
}

代码示例来源:origin: konsoletyper/teavm-javac

private void checkLambdaCandidate(JCNewClass tree, ClassSymbol csym, Type clazztype) {
  if (allowLambda &&
      identifyLambdaCandidate &&
      clazztype.hasTag(CLASS) &&
      !pt().hasTag(NONE) &&
      types.isFunctionalInterface(clazztype.tsym)) {
    Symbol descriptor = types.findDescriptorSymbol(clazztype.tsym);
    int count = 0;
    boolean found = false;
    for (Symbol sym : csym.members().getElements()) {
      if ((sym.flags() & SYNTHETIC) != 0 ||
          sym.isConstructor()) continue;
      count++;
      if (sym.kind != MTH ||
          !sym.name.equals(descriptor.name)) continue;
      Type mtype = types.memberType(clazztype, sym);
      if (types.overrideEquivalent(mtype, types.memberType(clazztype, descriptor))) {
        found = true;
      }
    }
    if (found && count == 1) {
      log.note(tree.def, "potential.lambda.found");
    }
  }
}

代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac

/**
 * Create a symbol for a class that implements a given functional interface
 * and overrides its functional descriptor. This routine is used for two
 * main purposes: (i) checking well-formedness of a functional interface;
 * (ii) perform functional interface bridge calculation.
 */
public ClassSymbol makeFunctionalInterfaceClass(Env<AttrContext> env, Name name, List<Type> targets, long cflags) {
  if (targets.isEmpty() || !isFunctionalInterface(targets.head)) {
    return null;
  }
  Symbol descSym = findDescriptorSymbol(targets.head.tsym);
  Type descType = findDescriptorType(targets.head);
  ClassSymbol csym = new ClassSymbol(cflags, name, env.enclClass.sym.outermostClass());
  csym.completer = null;
  csym.members_field = new Scope(csym);
  MethodSymbol instDescSym = new MethodSymbol(descSym.flags(), descSym.name, descType, csym);
  csym.members_field.enter(instDescSym);
  Type.ClassType ctype = new Type.ClassType(Type.noType, List.<Type>nil(), csym);
  ctype.supertype_field = syms.objectType;
  ctype.interfaces_field = targets;
  csym.type = ctype;
  csym.sourcefile = ((ClassSymbol)csym.owner).sourcefile;
  return csym;
}

代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac

/**
 * Find the minimal set of methods that are overridden by the functional
 * descriptor in 'origin'. All returned methods are assumed to have different
 * erased signatures.
 */
public List<Symbol> functionalInterfaceBridges(TypeSymbol origin) {
  Assert.check(isFunctionalInterface(origin));
  Symbol descSym = findDescriptorSymbol(origin);
  CompoundScope members = membersClosure(origin.type, false);
  ListBuffer<Symbol> overridden = new ListBuffer<>();
  outer: for (Symbol m2 : members.getElementsByName(descSym.name, bridgeFilter)) {
    if (m2 == descSym) continue;
    else if (descSym.overrides(m2, origin, Types.this, false)) {
      for (Symbol m3 : overridden) {
        if (isSameType(m3.erasure(Types.this), m2.erasure(Types.this)) ||
            (m3.overrides(m2, origin, Types.this, false) &&
            (pendingBridges((ClassSymbol)origin, m3.enclClass()) ||
            (((MethodSymbol)m2).binaryImplementation((ClassSymbol)m3.owner, Types.this) != null)))) {
          continue outer;
        }
      }
      overridden.add(m2);
    }
  }
  return overridden.toList();
}
//where

代码示例来源:origin: konsoletyper/teavm-javac

/**
 * Find the minimal set of methods that are overridden by the functional
 * descriptor in 'origin'. All returned methods are assumed to have different
 * erased signatures.
 */
public List<Symbol> functionalInterfaceBridges(TypeSymbol origin) {
  Assert.check(isFunctionalInterface(origin));
  Symbol descSym = findDescriptorSymbol(origin);
  CompoundScope members = membersClosure(origin.type, false);
  ListBuffer<Symbol> overridden = new ListBuffer<>();
  outer: for (Symbol m2 : members.getElementsByName(descSym.name, bridgeFilter)) {
    if (m2 == descSym) continue;
    else if (descSym.overrides(m2, origin, Types.this, false)) {
      for (Symbol m3 : overridden) {
        if (isSameType(m3.erasure(Types.this), m2.erasure(Types.this)) ||
            (m3.overrides(m2, origin, Types.this, false) &&
            (pendingBridges((ClassSymbol)origin, m3.enclClass()) ||
            (((MethodSymbol)m2).binaryImplementation((ClassSymbol)m3.owner, Types.this) != null)))) {
          continue outer;
        }
      }
      overridden.add(m2);
    }
  }
  return overridden.toList();
}
//where

代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac

Type t = args2.head;
if (!types.isSubtype(t, s) && !types.isSubtype(s, t)) {
  if (types.isFunctionalInterface(s) && types.isFunctionalInterface(t) &&
      types.findDescriptorType(s).getParameterTypes().length() > 0 &&
      types.findDescriptorType(s).getParameterTypes().length() ==

代码示例来源:origin: konsoletyper/teavm-javac

Type t = args2.head;
if (!types.isSubtype(t, s) && !types.isSubtype(s, t)) {
  if (types.isFunctionalInterface(s) && types.isFunctionalInterface(t) &&
      types.findDescriptorType(s).getParameterTypes().length() > 0 &&
      types.findDescriptorType(s).getParameterTypes().length() ==

相关文章

Types类方法