com.sun.tools.javac.code.Type.ForAll Java Examples

The following examples show how to use com.sun.tools.javac.code.Type.ForAll. 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: HandleExtensionMethod.java    From EasyMPermission with MIT License 5 votes vote down vote up
private void handleMethodCall(final JCMethodInvocation methodCall) {
	JavacNode methodCallNode = annotationNode.getAst().get(methodCall);
	
	if (methodCallNode == null) {
		// This should mean the node does not exist in the source at all. This is the case for generated nodes, such as implicit super() calls.
		return;
	}
	
	JavacNode surroundingType = upToTypeNode(methodCallNode);
	
	TypeSymbol surroundingTypeSymbol = ((JCClassDecl)surroundingType.get()).sym;
	JCExpression receiver = receiverOf(methodCall);
	String methodName = methodNameOf(methodCall);
	
	if ("this".equals(methodName) || "super".equals(methodName)) return;
	Type resolvedMethodCall = CLASS_AND_METHOD.resolveMember(methodCallNode, methodCall);
	if (resolvedMethodCall == null) return;
	if (!suppressBaseMethods && !(resolvedMethodCall instanceof ErrorType)) return;
	Type receiverType = CLASS_AND_METHOD.resolveMember(methodCallNode, receiver);
	if (receiverType == null) return;
	if (receiverType.tsym.toString().endsWith(receiver.toString())) return;
	
	Types types = Types.instance(annotationNode.getContext());
	for (Extension extension : extensions) {
		TypeSymbol extensionProvider = extension.extensionProvider;
		if (surroundingTypeSymbol == extensionProvider) continue;
		for (MethodSymbol extensionMethod : extension.extensionMethods) {
			if (!methodName.equals(extensionMethod.name.toString())) continue;
			Type extensionMethodType = extensionMethod.type;
			if (!MethodType.class.isInstance(extensionMethodType) && !ForAll.class.isInstance(extensionMethodType)) continue;
			Type firstArgType = types.erasure(extensionMethodType.asMethodType().argtypes.get(0));
			if (!types.isAssignable(receiverType, firstArgType)) continue;
			methodCall.args = methodCall.args.prepend(receiver);
			methodCall.meth = chainDotsString(annotationNode, extensionProvider.toString() + "." + methodName);
			return;
		}
	}
}
 
Example #2
Source File: UForAll.java    From Refaster with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public Unifier visitForAll(ForAll target, @Nullable Unifier unifier) {
  Types types = unifier.types();
  try {
    Type myType = inline(new Inliner(unifier.getContext(), Bindings.create()));
    if (types.overrideEquivalent(types.erasure(myType), types.erasure(target))) {
      return unifier;
    }
  } catch (CouldNotResolveImportException e) {
    // fall through
  }
  return null;
}
 
Example #3
Source File: Printer.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
@Override
public String visitForAll(ForAll t, Locale locale) {
    return "<" + visitTypes(t.tvars, locale) + ">" + visit(t.qtype, locale);
}
 
Example #4
Source File: Check.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
  * Report warnings for potentially ambiguous method declarations. Two declarations
  * are potentially ambiguous if they feature two unrelated functional interface
  * in same argument position (in which case, a call site passing an implicit
  * lambda would be ambiguous).
  */
void checkPotentiallyAmbiguousOverloads(DiagnosticPosition pos, Type site,
        MethodSymbol msym1, MethodSymbol msym2) {
    if (msym1 != msym2 &&
            allowDefaultMethods &&
            lint.isEnabled(LintCategory.OVERLOADS) &&
            (msym1.flags() & POTENTIALLY_AMBIGUOUS) == 0 &&
            (msym2.flags() & POTENTIALLY_AMBIGUOUS) == 0) {
        Type mt1 = types.memberType(site, msym1);
        Type mt2 = types.memberType(site, msym2);
        //if both generic methods, adjust type variables
        if (mt1.hasTag(FORALL) && mt2.hasTag(FORALL) &&
                types.hasSameBounds((ForAll)mt1, (ForAll)mt2)) {
            mt2 = types.subst(mt2, ((ForAll)mt2).tvars, ((ForAll)mt1).tvars);
        }
        //expand varargs methods if needed
        int maxLength = Math.max(mt1.getParameterTypes().length(), mt2.getParameterTypes().length());
        List<Type> args1 = rs.adjustArgs(mt1.getParameterTypes(), msym1, maxLength, true);
        List<Type> args2 = rs.adjustArgs(mt2.getParameterTypes(), msym2, maxLength, true);
        //if arities don't match, exit
        if (args1.length() != args2.length()) return;
        boolean potentiallyAmbiguous = false;
        while (args1.nonEmpty() && args2.nonEmpty()) {
            Type s = args1.head;
            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.findDescriptorType(t).getParameterTypes().length()) {
                    potentiallyAmbiguous = true;
                } else {
                    break;
                }
            }
            args1 = args1.tail;
            args2 = args2.tail;
        }
        if (potentiallyAmbiguous) {
            //we found two incompatible functional interfaces with same arity
            //this means a call site passing an implicit lambda would be ambigiuous
            msym1.flags_field |= POTENTIALLY_AMBIGUOUS;
            msym2.flags_field |= POTENTIALLY_AMBIGUOUS;
            log.warning(LintCategory.OVERLOADS, pos,
                        Warnings.PotentiallyAmbiguousOverload(msym1, msym1.location(),
                                                              msym2, msym2.location()));
            return;
        }
    }
}
 
Example #5
Source File: MemberEnter.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/** Construct method type from method signature.
 *  @param typarams    The method's type parameters.
 *  @param params      The method's value parameters.
 *  @param res             The method's result type,
 *                 null if it is a constructor.
 *  @param recvparam       The method's receiver parameter,
 *                 null if none given; TODO: or already set here?
 *  @param thrown      The method's thrown exceptions.
 *  @param env             The method's (local) environment.
 */
Type signature(MethodSymbol msym,
               List<JCTypeParameter> typarams,
               List<JCVariableDecl> params,
               JCTree res,
               JCVariableDecl recvparam,
               List<JCExpression> thrown,
               Env<AttrContext> env) {

    // Enter and attribute type parameters.
    List<Type> tvars = enter.classEnter(typarams, env);
    attr.attribTypeVariables(typarams, env);

    // Enter and attribute value parameters.
    ListBuffer<Type> argbuf = new ListBuffer<>();
    for (List<JCVariableDecl> l = params; l.nonEmpty(); l = l.tail) {
        memberEnter(l.head, env);
        argbuf.append(l.head.vartype.type);
    }

    // Attribute result type, if one is given.
    Type restype = res == null ? syms.voidType : attr.attribType(res, env);

    // Attribute receiver type, if one is given.
    Type recvtype;
    if (recvparam!=null) {
        memberEnter(recvparam, env);
        recvtype = recvparam.vartype.type;
    } else {
        recvtype = null;
    }

    // Attribute thrown exceptions.
    ListBuffer<Type> thrownbuf = new ListBuffer<>();
    for (List<JCExpression> l = thrown; l.nonEmpty(); l = l.tail) {
        Type exc = attr.attribType(l.head, env);
        if (!exc.hasTag(TYPEVAR)) {
            exc = chk.checkClassType(l.head.pos(), exc);
        } else if (exc.tsym.owner == msym) {
            //mark inference variables in 'throws' clause
            exc.tsym.flags_field |= THROWS;
        }
        thrownbuf.append(exc);
    }
    MethodType mtype = new MethodType(argbuf.toList(),
                                restype,
                                thrownbuf.toList(),
                                syms.methodClass);
    mtype.recvtype = recvtype;

    return tvars.isEmpty() ? mtype : new ForAll(tvars, mtype);
}
 
Example #6
Source File: Printer.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String visitForAll(ForAll t, Locale locale) {
    return "<" + visitTypes(t.tvars, locale) + ">" + visit(t.qtype, locale);
}
 
Example #7
Source File: UForAll.java    From Refaster with Apache License 2.0 4 votes vote down vote up
@Override
public Type inline(Inliner inliner) throws CouldNotResolveImportException {
  return new ForAll(
      inliner.<Type, UTypeVar>inlineList(getTypeVars()), getQuantifiedType().inline(inliner));
}
 
Example #8
Source File: UTemplater.java    From Refaster with Apache License 2.0 4 votes vote down vote up
@Override
public UForAll visitForAll(ForAll type, Void v) {
  List<UTypeVar> vars = cast(templateTypes(type.getTypeVariables()), UTypeVar.class);
  return UForAll.create(vars, type.qtype.accept(this, null));
}