groovy.lang.MetaMethod Java Examples

The following examples show how to use groovy.lang.MetaMethod. 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: MetaClassHelper.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * @param list   a list of MetaMethods
 * @param method the MetaMethod of interest
 * @return true if a method of the same matching prototype was found in the
 *         list
 */
public static boolean containsMatchingMethod(List list, MetaMethod method) {
    for (Object aList : list) {
        MetaMethod aMethod = (MetaMethod) aList;
        CachedClass[] params1 = aMethod.getParameterTypes();
        CachedClass[] params2 = method.getParameterTypes();
        if (params1.length == params2.length) {
            boolean matches = true;
            for (int i = 0; i < params1.length; i++) {
                if (params1[i] != params2[i]) {
                    matches = false;
                    break;
                }
            }
            if (matches) {
                return true;
            }
        }
    }
    return false;
}
 
Example #2
Source File: MetaMethodIndex.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static boolean isMatchingMethod(MetaMethod aMethod, MetaMethod method) {
    if (aMethod==method) return true;
    CachedClass[] params1 = aMethod.getParameterTypes();
    CachedClass[] params2 = method.getParameterTypes();
    if (params1.length != params2.length) {
        return false;
    }

    boolean matches = true;
    for (int i = 0; i < params1.length; i++) {
        if (params1[i] != params2[i]) {
            matches = false;
            break;
        }
    }
    return matches;
}
 
Example #3
Source File: HandleMetaClass.java    From groovy with Apache License 2.0 6 votes vote down vote up
public GroovyObject replaceDelegate() {
    if (object == null) {
        if (!(delegate instanceof ExpandoMetaClass)) {
          delegate = new ExpandoMetaClass(delegate.getTheClass(), true, true);
          delegate.initialize();
        }
        DefaultGroovyMethods.setMetaClass(delegate.getTheClass(), delegate);
    }
    else {
      if (object != NONE) {
          final MetaClass metaClass = delegate;
          delegate = new ExpandoMetaClass(delegate.getTheClass(), false, true);
          if (metaClass instanceof ExpandoMetaClass) {
              ExpandoMetaClass emc = (ExpandoMetaClass) metaClass;
              for (MetaMethod method : emc.getExpandoMethods())
                ((ExpandoMetaClass)delegate).registerInstanceMethod(method);
          }
          delegate.initialize();
          MetaClassHelper.doSetMetaClass(object, delegate);
          object = NONE;
      }
    }
    return (GroovyObject)delegate;
}
 
Example #4
Source File: MetaElementsProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public Map<MethodSignature, CompletionItem> getStaticMethods(CompletionContext context) {
    final Map<MethodSignature, CompletionItem> result = new HashMap<MethodSignature, CompletionItem>();
    final Class clz = loadClass(context);

    if (clz != null) {
        final MetaClass metaClz = GroovySystem.getMetaClassRegistry().getMetaClass(clz);

        if (metaClz != null) {
            for (MetaMethod method : metaClz.getMetaMethods()) {
                if (method.isStatic()) {
                    populateProposal(clz, method, context.getPrefix(), context.getAnchor(), result, context.isNameOnly());
                }
            }
        }
        GroovySystem.getMetaClassRegistry().removeMetaClass(clz);
    }
    return result;
}
 
Example #5
Source File: MixinInstanceMetaProperty.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static MetaMethod createGetter(final MetaProperty property, final MixinInMetaClass mixinInMetaClass) {
    return new MetaMethod() {
        final String name = getGetterName(property.getName(), property.getType());
        {
            setParametersTypes (CachedClass.EMPTY_ARRAY);
        }

        public int getModifiers() {
            return Modifier.PUBLIC;
        }

        public String getName() {
            return name;
        }

        public Class getReturnType() {
            return property.getType();
        }

        public CachedClass getDeclaringClass() {
            return mixinInMetaClass.getInstanceClass();
        }

        public Object invoke(Object object, Object[] arguments) {
            return property.getProperty(mixinInMetaClass.getMixinInstance(object));
        }
    };
}
 
Example #6
Source File: CachedClass.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void updateSetNewMopMethods(List<MetaMethod> arr) {
    if (arr != null) {
        final MetaMethod[] metaMethods = arr.toArray(MetaMethod.EMPTY_ARRAY);
        classInfo.dgmMetaMethods = metaMethods;
        classInfo.newMetaMethods = metaMethods;
    }
    else
        classInfo.newMetaMethods = classInfo.dgmMetaMethods;
}
 
Example #7
Source File: MetaClassHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static GroovyRuntimeException createExceptionText(String init, MetaMethod method, Object object, Object[] args, Throwable reason, boolean setReason) {
    return new GroovyRuntimeException(
            init
                    + method
                    + " on: "
                    + object
                    + " with arguments: "
                    + InvokerHelper.toString(args)
                    + " reason: "
                    + reason,
            setReason ? reason : null);
}
 
Example #8
Source File: Inspector.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected String[] methodInfo(MetaMethod method) {
    String[] result = new String[MEMBER_EXCEPTIONS_IDX + 1];
    int mod = method.getModifiers();
    result[MEMBER_ORIGIN_IDX] = GROOVY;
    result[MEMBER_MODIFIER_IDX] = Modifier.toString(mod);
    result[MEMBER_DECLARER_IDX] = shortName(method.getDeclaringClass().getTheClass());
    result[MEMBER_TYPE_IDX] = shortName(method.getReturnType());
    result[MEMBER_NAME_IDX] = method.getName();
    result[MEMBER_PARAMS_IDX] = makeParamsInfo(method.getNativeParameterTypes());
    result[MEMBER_EXCEPTIONS_IDX] = NOT_APPLICABLE; // no exception info for Groovy MetaMethods
    return withoutNulls(result);
}
 
Example #9
Source File: MetaElementsProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private MethodSignature getSignature(MetaMethod method) {
    String[] parameters = new String[method.getParameterTypes().length];
    for (int i = 0; i < parameters.length; i++) {
        parameters[i] = Utilities.translateClassLoaderTypeName(method.getParameterTypes()[i].getName());
    }

    return new MethodSignature(method.getName(), parameters);
}
 
Example #10
Source File: ClosureMetaClass.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public MetaMethod pickMethod(String name, Class[] argTypes) {
    if (argTypes == null) argTypes = MetaClassHelper.EMPTY_CLASS_ARRAY;
    if (name.equals(CLOSURE_CALL_METHOD) || name.equals(CLOSURE_DO_CALL_METHOD)) {
        return pickClosureMethod(argTypes);
    }
    return CLOSURE_METACLASS.getMetaMethod(name, argTypes);
}
 
Example #11
Source File: CompletionItem.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public MetaMethodItem(Class clz, MetaMethod method, int anchorOffset, boolean isGDK, boolean nameOnly) {
    super(null, anchorOffset);
    this.method = method;
    this.isGDK = isGDK;
    this.nameOnly = nameOnly;

    // This is an artificial, new ElementHandle which has no real
    // equivalent in the AST. It's used to match the one passed to super.document()
    methodElement = new ASTMethod(new ASTNode(), clz, method, isGDK);
}
 
Example #12
Source File: CompletionHandler.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * create the signature-string of this method usable as a
 * Javadoc URL suffix (behind the # )
 *
 * This was needed, since from groovy 1.5.4 to
 * 1.5.5 the MetaMethod.getSignature() changed from
 * human-readable to Class.getName() output.
 *
 * To make matters worse, we have some subtle
 * differences between JDK and GDK MetaMethods
 *
 * method.getSignature for the JDK gives the return-
 * value right behind the method and encodes like Class.getName():
 *
 * codePointCount(II)I
 *
 * GDK-methods look like this:
 * java.lang.String center(java.lang.Number, java.lang.String)
 *
 * TODO: if groovy folks ever change this (again), we're falling
 * flat on our face.
 *
 */
public static String getMethodSignature(MetaMethod method, boolean forURL, boolean isGDK) {
    String methodSignature = method.getSignature();
    methodSignature = methodSignature.trim();

    if (isGDK) {
        // remove return value
        int firstSpace = methodSignature.indexOf(" ");

        if (firstSpace != -1) {
            methodSignature = methodSignature.substring(firstSpace + 1);
        }

        if (forURL) {
            methodSignature = methodSignature.replaceAll(", ", ",%20");
        }

        return methodSignature;

    } else {
        String parts[] = methodSignature.split("[()]");

        if (parts.length < 2) {
            return "";
        }

        String paramsBody = decodeTypes(parts[1], forURL);

        return parts[0] + "(" + paramsBody + ")";
    }
}
 
Example #13
Source File: CompletionHandler.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void printMethod(MetaMethod mm) {

        LOG.log(Level.FINEST, "--------------------------------------------------");
        LOG.log(Level.FINEST, "getName()           : {0}", mm.getName());
        LOG.log(Level.FINEST, "toString()          : {0}", mm.toString());
        LOG.log(Level.FINEST, "getDescriptor()     : {0}", mm.getDescriptor());
        LOG.log(Level.FINEST, "getSignature()      : {0}", mm.getSignature());
        // LOG.log(Level.FINEST, "getParamTypes()     : " + mm.getParameterTypes());
        LOG.log(Level.FINEST, "getDeclaringClass() : {0}", mm.getDeclaringClass());
    }
 
Example #14
Source File: NumberNumberMinus.java    From groovy with Apache License 2.0 4 votes vote down vote up
public DoubleLong(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) {
    super(site, metaClass, metaMethod, params, (Number) receiver, (Number) args[0]);
}
 
Example #15
Source File: NumberNumberPlus.java    From groovy with Apache License 2.0 4 votes vote down vote up
public FloatDouble(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) {
    super(site, metaClass, metaMethod, params, (Number) receiver, (Number) args[0]);
}
 
Example #16
Source File: CharacterArrayPutAtMetaMethod.java    From groovy with Apache License 2.0 4 votes vote down vote up
public CallSite createPojoCallSite(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) {
    if (!(args[0] instanceof Integer) || !(args[1] instanceof Character))
        return PojoMetaMethodSite.createNonAwareCallSite(site, metaClass, metaMethod, params, args);
    else
        return new MyPojoMetaMethodSite(site, metaClass, metaMethod, params);
}
 
Example #17
Source File: DoubleArrayGetAtMetaMethod.java    From groovy with Apache License 2.0 4 votes vote down vote up
public MyPojoMetaMethodSite(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params) {
    super(site, metaClass, metaMethod, params);
}
 
Example #18
Source File: NumberNumberPlus.java    From groovy with Apache License 2.0 4 votes vote down vote up
public CallSite createDoubleFloat(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) {
    return new DoubleFloat(site, metaClass, metaMethod, params, receiver, args);
}
 
Example #19
Source File: ObjectArrayGetAtMetaMethod.java    From groovy with Apache License 2.0 4 votes vote down vote up
public MyPojoMetaMethodSite(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params) {
    super(site, metaClass, metaMethod, params);
}
 
Example #20
Source File: ClosureMetaClass.java    From groovy with Apache License 2.0 4 votes vote down vote up
public MetaMethod retrieveStaticMethod(String methodName, Class[] arguments) {
    return null;
}
 
Example #21
Source File: NumberNumberMultiply.java    From groovy with Apache License 2.0 4 votes vote down vote up
public DoubleLong(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) {
    super(site, metaClass, metaMethod, params, (Number) receiver, (Number) args[0]);
}
 
Example #22
Source File: FloatArrayPutAtMetaMethod.java    From groovy with Apache License 2.0 4 votes vote down vote up
public CallSite createPojoCallSite(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) {
    if (!(args[0] instanceof Integer) || !(args[1] instanceof Float))
        return PojoMetaMethodSite.createNonAwareCallSite(site, metaClass, metaMethod, params, args);
    else
        return new MyPojoMetaMethodSite(site, metaClass, metaMethod, params);
}
 
Example #23
Source File: NumberNumberMinus.java    From groovy with Apache License 2.0 4 votes vote down vote up
public FloatFloat(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) {
    super(site, metaClass, metaMethod, params, (Number) receiver, (Number) args[0]);
}
 
Example #24
Source File: NumberNumberPlus.java    From groovy with Apache License 2.0 4 votes vote down vote up
public CallSite createFloatDouble(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) {
    return new FloatDouble(site, metaClass, metaMethod, params, receiver, args);
}
 
Example #25
Source File: ClosureMetaClass.java    From groovy with Apache License 2.0 4 votes vote down vote up
StandardClosureChooser(MetaMethod m0, MetaMethod m1) {
    doCall0 = m0;
    doCall1 = m1;
}
 
Example #26
Source File: PogoMetaMethodSite.java    From groovy with Apache License 2.0 4 votes vote down vote up
public PogoMetaMethodSiteNoUnwrapNoCoerce(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params) {
    super(site, metaClass, metaMethod, params);
}
 
Example #27
Source File: PogoMetaMethodSite.java    From groovy with Apache License 2.0 4 votes vote down vote up
public PogoMetaMethodSiteNoUnwrap(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params) {
    super(site, metaClass, metaMethod, params);
}
 
Example #28
Source File: PogoMetaMethodSite.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static CallSite createPogoMetaMethodSite(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object[] args) {
    if (metaMethod.getClass() == CachedMethod.class)
      return createCachedMethodSite (site, metaClass, (CachedMethod) metaMethod, params, args);

    return createNonAwareCallSite(site, metaClass, metaMethod, params, args);
}
 
Example #29
Source File: NumberNumberMultiply.java    From groovy with Apache License 2.0 4 votes vote down vote up
public CallSite createIntegerFloat(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) {
    return new IntegerFloat(site, metaClass, metaMethod, params, receiver, args);
}
 
Example #30
Source File: ClosureMetaClass.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void addMetaMethod(MetaMethod method) {
    throw new UnsupportedOperationException();
}