Package org.eclipse.jdt.internal.compiler.ast

Examples of org.eclipse.jdt.internal.compiler.ast.LambdaExpression


* findLocalElement() cannot find lambdas.
*/
protected IJavaElement findLocalElement(int pos, MethodScope scope) {
  if (scope != null && scope.isLambdaScope()) {
    IJavaElement parent = findLocalElement(pos, scope.enclosingMethodScope());
    LambdaExpression expression = (LambdaExpression) scope.originalReferenceContext();
    if (expression != null && expression.resolvedType != null && expression.resolvedType.isValidBinding()) {
      org.eclipse.jdt.internal.core.LambdaExpression lambdaElement = LambdaFactory.createLambdaExpression((JavaElement) parent, expression);
      return lambdaElement.getMethod();
    }
    return parent;
View Full Code Here


    }
  }
  if (parsedUnit.functionalExpressions != null) {
    for (int i = 0; i < parsedUnit.functionalExpressionsCount; i++) {
      if (parsedUnit.functionalExpressions[i] instanceof LambdaExpression) {
        final LambdaExpression expression = (LambdaExpression) parsedUnit.functionalExpressions[i];
        if (expression.resolvedType != null && expression.resolvedType.isValidBinding()) {
          IType typeHandle = (IType)factory.createLambdaTypeElement(expression, cu, existingElements, knownScopes);
          remember(typeHandle, expression.getTypeBinding());
        }
      }
    }
  }
}
View Full Code Here

  private boolean checkExpression(Expression expri, TypeBinding[] u, TypeBinding r1, TypeBinding[] v, TypeBinding r2)
      throws InferenceFailureException {
    if (expri instanceof LambdaExpression && !((LambdaExpression)expri).argumentsTypeElided()) {
      if (r2.id == TypeIds.T_void)
        return true;
      LambdaExpression lambda = (LambdaExpression) expri;
      Expression[] results = lambda.resultExpressions();
      if (r1.isFunctionalInterface(this.scope) && r2.isFunctionalInterface(this.scope)
          && !(r1.isCompatibleWith(r2) || r2.isCompatibleWith(r1))) {
        // "these rules are applied recursively to R1 and R2, for each result expression in expi."
        // (what does "applied .. to R1 and R2" mean? Why mention R1/R2 and not U/V?)
        for (int i = 0; i < results.length; i++) {
View Full Code Here

              typeParameter.getAllAnnotationContexts(AnnotationTargetTypeConstants.METHOD_TYPE_PARAMETER, i, allTypeAnnotationContexts);
            }
          }
        }
      } else if (binding.sourceLambda() != null) { // SyntheticMethodBinding, purpose : LambdaMethod.
        LambdaExpression lambda = binding.sourceLambda();
        if ((lambda.bits & ASTNode.HasTypeAnnotations) != 0) {
          if (lambda.arguments != null)
            completeArgumentAnnotationInfo(lambda.arguments, allTypeAnnotationContexts);
        }
      }
View Full Code Here

      if (functional instanceof LambdaExpression &&
           (((markerInterfaces=((LambdaExpression)functional).getMarkerInterfaces()) != null) ||
             ((LambdaExpression)functional).isSerializable) ||
             bridges != null) {
       
        LambdaExpression lambdaEx = (LambdaExpression)functional;
        // may need even more space
        int extraSpace = 2; // at least 2 more than when the normal metafactory is used, for the bitflags entry
        if (markerInterfaces != null) {
          // 2 for the marker interface list size then 2 per marker interface index
          extraSpace += (2 + 2 * markerInterfaces.length);
 
View Full Code Here

          if (arguments != null) {
            attributesNumber += generateRuntimeAnnotationsForParameters(arguments);
          }
        }
      } else {
        LambdaExpression lambda = methodBinding.sourceLambda();
        if (lambda != null) {
          if ((methodBinding.tagBits & TagBits.HasParameterAnnotations) != 0) {
            Argument[] arguments = lambda.arguments();
            if (arguments != null) {
              int parameterCount = methodBinding.parameters.length;
              int argumentCount = arguments.length;
              if (parameterCount > argumentCount) { // synthetics prefixed
                int redShift = parameterCount - argumentCount;
View Full Code Here

    SyntheticMethodBinding syntheticMethodBinding = (SyntheticMethodBinding)syntheticsForSerializableLambdas.get(i);
    switchLabels[i].place();
   
    // Compare ImplMethodKind
    aload_0();
    LambdaExpression lambdaEx = syntheticMethodBinding.lambda;
    MethodBinding mb = lambdaEx.binding;
    invoke(Opcodes.OPC_invokevirtual, 1, 1, ConstantPool.JavaLangInvokeSerializedLambdaConstantPoolName,
        ConstantPool.GetImplMethodKind, ConstantPool.GetImplMethodKindSignature);
    byte methodKind = 0;
    if (mb.isStatic()) {
      methodKind = ClassFileConstants.MethodHandleRefKindInvokeStatic;
    } else if (mb.isPrivate()) {
      methodKind = ClassFileConstants.MethodHandleRefKindInvokeSpecial;
    } else {
      methodKind = ClassFileConstants.MethodHandleRefKindInvokeVirtual;
    }
    bipush(methodKind);// TODO see table below
    if_icmpne(errorLabel);

    // Compare FunctionalInterfaceClass
    aload_0();
    invoke(Opcodes.OPC_invokevirtual, 1, 1, ConstantPool.JavaLangInvokeSerializedLambdaConstantPoolName,
        ConstantPool.GetFunctionalInterfaceClass, ConstantPool.GetFunctionalInterfaceClassSignature);
    String functionalInterface = null;
    final TypeBinding expectedType = lambdaEx.expectedType();
    if (expectedType instanceof IntersectionCastTypeBinding) {
      functionalInterface = new String(((IntersectionCastTypeBinding)expectedType).getSAMType(scope).constantPoolName());
    } else {
      functionalInterface = new String(expectedType.constantPoolName());
    }
View Full Code Here

}
public void shouldReturn(TypeBinding returnType, ASTNode location) {
  int sourceStart = location.sourceStart;
  int sourceEnd = location.sourceEnd;
  if (location instanceof LambdaExpression) {
    LambdaExpression exp = (LambdaExpression) location;
    sourceStart = exp.sourceStart;
    sourceEnd = exp.diagnosticsSourceEnd();
  }
  this.handle(
    methodHasMissingSwitchDefault() ? IProblem.ShouldReturnValueHintMissingDefault : IProblem.ShouldReturnValue,
    new String[] { new String (returnType.readableName())},
    new String[] { new String (returnType.shortReadableName())},
View Full Code Here

        // if we get here for some kinds of poly expressions (incl. ConditionalExpression),
        // then other ways for checking compatibility are needed:
                if (this.left instanceof FunctionalExpression) {
                    if (this.left instanceof LambdaExpression) {
                        // cf. NegativeLambdaExpressionTest.test412453()
                        LambdaExpression copy = ((LambdaExpression) this.left).getResolvedCopyForInferenceTargeting(this.right);
                        return (copy != null && copy.resolvedType != null && copy.resolvedType.isValidBinding()) ? TRUE : FALSE;
                    }
                }
                return this.left.isCompatibleWith(this.right, inferenceContext.scope) ? TRUE : FALSE;
      } else if (!exprType.isValidBinding()) {
        return FALSE;
      }
      if (isCompatibleWithInLooseInvocationContext(exprType, this.right, inferenceContext)) {
        return TRUE;
      } else if (this.left instanceof AllocationExpression && this.left.isPolyExpression()) {
        // half-resolved diamond has a resolvedType, but that may not be the final word, try one more step of resolution:
              MethodBinding binding = ((AllocationExpression) this.left).binding(this.right, false, null);
              return (binding != null && binding.declaringClass.isCompatibleWith(this.right, inferenceContext.scope)) ? TRUE : FALSE;
            } else if (this.left instanceof Invocation && this.left.isPolyExpression()) {
              Invocation invoc = (Invocation) this.left;
              MethodBinding binding = invoc.binding(this.right, false, null);
              if (binding instanceof ParameterizedGenericMethodBinding) {
                ParameterizedGenericMethodBinding method = (ParameterizedGenericMethodBinding) binding;
          InferenceContext18 leftCtx = invoc.getInferenceContext(method);
                if (leftCtx.stepCompleted < InferenceContext18.TYPE_INFERRED) {
                  break proper; // fall through into nested inference below (not explicit in the spec!)
                }
              }
            }
      return FALSE;
    }
    if (!canBePolyExpression(this.left)) {
      TypeBinding exprType = this.left.resolvedType;
      if (exprType == null || !exprType.isValidBinding())
        return FALSE;
      return ConstraintTypeFormula.create(exprType, this.right, COMPATIBLE, this.isSoft);
    } else {
      // shapes of poly expressions (18.2.1)
      // - parenthesized expression : these are transparent in our AST
      if (this.left instanceof Invocation) {
        Invocation invocation = (Invocation) this.left;
        MethodBinding previousMethod = invocation.binding(this.right, false, null);
        if (previousMethod == null)    // can happen, e.g., if inside a copied lambda with ignored errors
          return null;         // -> proceed with no new constraints
        MethodBinding method = previousMethod;
        // ignore previous (inner) inference result and do a fresh start:
        // avoid original(), since we only want to discard one level of instantiation
        // (method type variables - not class type variables)!
        method = previousMethod.shallowOriginal();
        SuspendedInferenceRecord prevInvocation = inferenceContext.enterPolyInvocation(invocation, invocation.arguments());

        // Invocation Applicability Inference: 18.5.1 & Invocation Type Inference: 18.5.2
        try {
          Expression[] arguments = invocation.arguments();
          TypeBinding[] argumentTypes = arguments == null ? Binding.NO_PARAMETERS : new TypeBinding[arguments.length];
          for (int i = 0; i < argumentTypes.length; i++)
            argumentTypes[i] = arguments[i].resolvedType;
          if (previousMethod instanceof ParameterizedGenericMethodBinding) {
            // find the previous inner inference context to see what inference kind this invocation needs:
            InferenceContext18 innerCtx = invocation.getInferenceContext((ParameterizedGenericMethodBinding) previousMethod);
            if (innerCtx == null) { // no inference -> assume it wasn't really poly after all
              TypeBinding exprType = this.left.resolvedType;
              if (exprType == null || !exprType.isValidBinding())
                return FALSE;
              return ConstraintTypeFormula.create(exprType, this.right, COMPATIBLE, this.isSoft);
            }
            inferenceContext.inferenceKind = innerCtx.inferenceKind;
            innerCtx.outerContext = inferenceContext;
          }
          boolean isDiamond = method.isConstructor() && this.left.isPolyExpression(method);
          inferInvocationApplicability(inferenceContext, method, argumentTypes, isDiamond, inferenceContext.inferenceKind);
          if (!inferPolyInvocationType(inferenceContext, invocation, this.right, method))
            return FALSE;
          return null; // already incorporated
        } finally {
          inferenceContext.resumeSuspendedInference(prevInvocation);
        }
      } else if (this.left instanceof ConditionalExpression) {
        ConditionalExpression conditional = (ConditionalExpression) this.left;
        return new ConstraintFormula[] {
          new ConstraintExpressionFormula(conditional.valueIfTrue, this.right, this.relation, this.isSoft),
          new ConstraintExpressionFormula(conditional.valueIfFalse, this.right, this.relation, this.isSoft)
        };
      } else if (this.left instanceof LambdaExpression) {
        LambdaExpression lambda = (LambdaExpression) this.left;
        BlockScope scope = lambda.enclosingScope;
        if (!this.right.isFunctionalInterface(scope))
          return FALSE;
       
        ReferenceBinding t = (ReferenceBinding) this.right;
        ParameterizedTypeBinding withWildCards = InferenceContext18.parameterizedWithWildcard(t);
        if (withWildCards != null) {
          t = findGroundTargetType(inferenceContext, scope, lambda, withWildCards);
        }
        if (t == null)
          return FALSE;
        MethodBinding functionType = t.getSingleAbstractMethod(scope, true);
        if (functionType == null)
          return FALSE;
        TypeBinding[] parameters = functionType.parameters;
        if (parameters.length != lambda.arguments().length)
          return FALSE;
        if (lambda.argumentsTypeElided())
          for (int i = 0; i < parameters.length; i++)
            if (!parameters[i].isProperType(true))
              return FALSE;
        lambda = lambda.getResolvedCopyForInferenceTargeting(t);
        if (lambda == null)
          return FALSE; // not strictly unreduceable, but proceeding with TRUE would likely produce secondary errors
        if (functionType.returnType == TypeBinding.VOID) {
          if (!lambda.isVoidCompatible())
            return FALSE;
        } else {
          if (!lambda.isValueCompatible())
            return FALSE;
        }
        List<ConstraintFormula> result = new ArrayList<ConstraintFormula>();
        if (!lambda.argumentsTypeElided()) {
          Argument[] arguments = lambda.arguments();
          for (int i = 0; i < parameters.length; i++)
            result.add(ConstraintTypeFormula.create(parameters[i], arguments[i].type.resolveType(lambda.enclosingScope), SAME));
          // in addition, ⟨T' <: T⟩:
          if (lambda.resolvedType != null)
            result.add(ConstraintTypeFormula.create(lambda.resolvedType, this.right, SUBTYPE));
        }
        if (functionType.returnType != TypeBinding.VOID) {
          TypeBinding r = functionType.returnType;
          Expression[] exprs;
          if (lambda.body() instanceof Expression) {
            exprs = new Expression[] {(Expression)lambda.body()};
          } else {
            exprs = lambda.resultExpressions();
          }
          for (int i = 0; i < exprs.length; i++) {
            Expression expr = exprs[i];
            if (r.isProperType(true) && expr.resolvedType != null) {
              TypeBinding exprType = expr.resolvedType;
View Full Code Here

    if (this.left instanceof LambdaExpression) {
      if (this.right instanceof InferenceVariable) {
        return Collections.singletonList((InferenceVariable)this.right);
      }
      if (this.right.isFunctionalInterface(context.scope)) {
        LambdaExpression lambda = (LambdaExpression) this.left;
        MethodBinding sam = this.right.getSingleAbstractMethod(context.scope, true); // TODO derive with target type?
        final Set<InferenceVariable> variables = new HashSet<InferenceVariable>();
        if (lambda.argumentsTypeElided()) {
          // i)
          int len = sam.parameters.length;
          for (int i = 0; i < len; i++) {
            sam.parameters[i].collectInferenceVariables(variables);
          }
        }
        if (sam.returnType != TypeBinding.VOID) {
          // ii)
          final TypeBinding r = sam.returnType;
          Statement body = lambda.body();
          if (body instanceof Expression) {
            variables.addAll(new ConstraintExpressionFormula((Expression) body, r, COMPATIBLE).inputVariables(context));
          } else {
            // TODO: should I use LambdaExpression.resultExpressions? (is currently private).
            body.traverse(new ASTVisitor() {
View Full Code Here

TOP

Related Classes of org.eclipse.jdt.internal.compiler.ast.LambdaExpression

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.