Java Code Examples for groovy.lang.MetaMethod

The following examples show how to use groovy.lang.MetaMethod. These examples are extracted from open source projects. 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 Project: netbeans   Source File: MetaElementsProvider.java    License: 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 2
Source Project: groovy   Source File: HandleMetaClass.java    License: 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 3
Source Project: groovy   Source File: MetaClassHelper.java    License: 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 4
Source Project: groovy   Source File: MetaMethodIndex.java    License: 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 5
Source Project: netbeans   Source File: CompletionHandler.java    License: 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 6
Source Project: netbeans   Source File: CompletionHandler.java    License: 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 7
Source Project: netbeans   Source File: CompletionItem.java    License: 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 8
Source Project: groovy   Source File: MixinInstanceMetaProperty.java    License: 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 9
Source Project: netbeans   Source File: MetaElementsProvider.java    License: 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 Project: groovy   Source File: CachedClass.java    License: 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 11
Source Project: groovy   Source File: ClosureMetaClass.java    License: 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 12
Source Project: groovy   Source File: Inspector.java    License: 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 13
Source Project: groovy   Source File: MetaClassHelper.java    License: 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 14
Source Project: groovy   Source File: ThreadManagedMetaBeanProperty.java    License: Apache License 2.0 4 votes vote down vote up
public MetaMethod getGetter() {
    return this.getter;
}
 
Example 15
Source Project: groovy   Source File: NumberNumberMultiply.java    License: Apache License 2.0 4 votes vote down vote up
public LongDouble(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 Project: netbeans   Source File: ASTMethod.java    License: Apache License 2.0 4 votes vote down vote up
public ASTMethod(ASTNode node, Class clz, MetaMethod method, boolean isGDK) {
    super(node);
    this.clz = clz;
    this.method = method;
    this.isGDK = isGDK;
}
 
Example 17
Source Project: groovy   Source File: NumberNumberMinus.java    License: Apache License 2.0 4 votes vote down vote up
public IntegerLong(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) {
    super(site, metaClass, metaMethod, params, (Number) receiver, (Number) args[0]);
}
 
Example 18
Source Project: groovy   Source File: NumberNumberMultiply.java    License: Apache License 2.0 4 votes vote down vote up
public CallSite createFloatFloat(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) {
    return new FloatFloat(site, metaClass, metaMethod, params, receiver, args);
}
 
Example 19
Source Project: groovy   Source File: NumberNumberMinus.java    License: Apache License 2.0 4 votes vote down vote up
public LongFloat(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) {
    super(site, metaClass, metaMethod, params, (Number) receiver, (Number) args[0]);
}
 
Example 20
Source Project: groovy   Source File: NumberNumberDiv.java    License: Apache License 2.0 4 votes vote down vote up
public DoubleDouble(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) {
    super(site, metaClass, metaMethod, params, (Number) receiver, (Number) args[0]);
}
 
Example 21
Source Project: groovy   Source File: MetaMethodIndex.java    License: Apache License 2.0 4 votes vote down vote up
public CacheEntry(final Class[] params, final MetaMethod method) {
    this.params = params;
    this.method = method;
}
 
Example 22
Source Project: groovy   Source File: NumberNumberMultiply.java    License: 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 23
Source Project: groovy   Source File: NumberNumberMinus.java    License: Apache License 2.0 4 votes vote down vote up
public CallSite createIntegerInteger(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) {
    return new IntegerInteger(site, metaClass, metaMethod, params, receiver, args);
}
 
Example 24
Source Project: groovy   Source File: ClosureMetaClass.java    License: Apache License 2.0 4 votes vote down vote up
public MetaMethod getStaticMetaMethod(String name, Object[] args) {
    return CLOSURE_METACLASS.getStaticMetaMethod(name, args);
}
 
Example 25
Source Project: groovy   Source File: MetaClass.java    License: Apache License 2.0 4 votes vote down vote up
public MetaMethod getMethod(String name, Class[] parameters) {
    return implRef.getPayload().getMethod(name, parameters);
}
 
Example 26
Source Project: groovy   Source File: NumberNumberMultiply.java    License: Apache License 2.0 4 votes vote down vote up
public CallSite createLongInteger(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) {
    return new LongInteger(site, metaClass, metaMethod, params, receiver, args);
}
 
Example 27
Source Project: groovy   Source File: NumberNumberMinus.java    License: Apache License 2.0 4 votes vote down vote up
public LongLong(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) {
    super(site, metaClass, metaMethod, params, (Number) receiver, (Number) args[0]);
}
 
Example 28
Source Project: groovy   Source File: NumberNumberDiv.java    License: Apache License 2.0 4 votes vote down vote up
public IntegerFloat(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) {
    super(site, metaClass, metaMethod, params, (Number) receiver, (Number) args[0]);
}
 
Example 29
Source Project: groovy   Source File: CharacterArrayGetAtMetaMethod.java    License: 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 30
Source Project: groovy   Source File: NumberNumberDiv.java    License: Apache License 2.0 4 votes vote down vote up
public DoubleInteger(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) {
    super(site, metaClass, metaMethod, params, (Number) receiver, (Number) args[0]);
}