Example #1
0
 boolean resolveInPlace(MemberName m, boolean searchSupers, Class<?> lookupClass) {
   if (m.name == null || m.type == null) { // find unique non-overloaded name
     Class<?> defc = m.getDeclaringClass();
     List<MemberName> choices = null;
     if (m.isMethod())
       choices = getMethods(defc, searchSupers, m.name, (MethodType) m.type, lookupClass);
     else if (m.isConstructor()) choices = getConstructors(defc, lookupClass);
     else if (m.isField())
       choices = getFields(defc, searchSupers, m.name, (Class<?>) m.type, lookupClass);
     // System.out.println("resolving "+m+" to "+choices);
     if (choices == null || choices.size() != 1) return false;
     if (m.name == null) m.name = choices.get(0).name;
     if (m.type == null) m.type = choices.get(0).type;
   }
   MethodHandleNatives.resolve(m, lookupClass);
   if (m.isResolved()) return true;
   int matchFlags = m.flags | (searchSupers ? SEARCH_ALL_SUPERS : 0);
   String matchSig = m.getSignature();
   MemberName[] buf = {m};
   int n =
       MethodHandleNatives.getMembers(
           m.getDeclaringClass(), m.getName(), matchSig, matchFlags, lookupClass, 0, buf);
   if (n != 1) return false;
   return m.isResolved();
 }
 private static DirectMethodHandle makeAllocator(MemberName ctor) {
   assert (ctor.isConstructor() && ctor.getName().equals("<init>"));
   Class<?> instanceClass = ctor.getDeclaringClass();
   ctor = ctor.asConstructor();
   assert (ctor.isConstructor() && ctor.getReferenceKind() == REF_newInvokeSpecial) : ctor;
   MethodType mtype = ctor.getMethodType().changeReturnType(instanceClass);
   LambdaForm lform = preparedLambdaForm(ctor);
   MemberName init = ctor.asSpecial();
   assert (init.getMethodType().returnType() == void.class);
   return new Constructor(mtype, lform, ctor, init, instanceClass);
 }
  // Constructors and factory methods in this class *must* be package scoped or private.
  private DirectMethodHandle(MethodType mtype, LambdaForm form, MemberName member) {
    super(mtype, form);
    if (!member.isResolved()) throw new InternalError();

    if (member.getDeclaringClass().isInterface() && member.isMethod() && !member.isAbstract()) {
      // Check for corner case: invokeinterface of Object method
      MemberName m =
          new MemberName(
              Object.class, member.getName(), member.getMethodType(), member.getReferenceKind());
      m = MemberName.getFactory().resolveOrNull(m.getReferenceKind(), m, null);
      if (m != null && m.isPublic()) {
        assert (member.getReferenceKind() == m.getReferenceKind()); // else this.form is wrong
        member = m;
      }
    }

    this.member = member;
  }