Example usage for java.lang.reflect Method isVarArgs

List of usage examples for java.lang.reflect Method isVarArgs

Introduction

In this page you can find the example usage for java.lang.reflect Method isVarArgs.

Prototype

@Override
public boolean isVarArgs() 

Source Link

Usage

From source file:org.xenei.jena.entities.impl.ResourceEntityProxy.java

private Object interceptNonAbstract(final Object obj, final Method m, final Object[] args,
        final MethodProxy proxy) throws Throwable {
    // handle the special case methods
    if (m.getName().equals("toString") && !m.isVarArgs() && (m.getParameterTypes().length == 0)) {
        return String.format("%s[%s]", subjectInfo.getClass(), resource);
    }/*from www  . j a v  a2s  .com*/
    if (m.getName().equals("hashCode")) {
        return resource.hashCode();
    }
    if (m.getName().equals("equals")) {
        if (args[0] instanceof ResourceWrapper) {
            return resource.equals(((ResourceWrapper) args[0]).getResource());
        }
        if (args[0] instanceof Resource) {
            return resource.equals(args[0]);
        }
        return false;
    }

    final Predicate p = m.getAnnotation(Predicate.class);
    if (p != null) {
        return interceptAnnotatedNonAbstract(obj, m, args, proxy, p);
    }
    return proxy.invokeSuper(obj, args);
}

From source file:org.vosao.rest.RestManager.java

/**
 * Match method to list of parameters by types.
 * @param restService/* w  ww  . ja v  a 2  s . c om*/
 * @param methodName
 * @param params
 * @return
 */
private MatchResult matchPathMethod(Object restService, String methodName, List<String> pathParams) {
    MatchResult result = null;
    for (Method m : restService.getClass().getMethods()) {
        if (!m.getName().equals(methodName))
            continue;
        if (m.isVarArgs())
            throw new IllegalArgumentException("Varargs are not supported");

        Class<?>[] paramTypes = m.getParameterTypes();
        if (paramTypes.length != pathParams.size())
            continue;
        Object[] convertedArgs = null;
        try {
            convertedArgs = convertArgs(paramTypes, pathParams);
        } catch (Exception e) {
        }
        if (convertedArgs == null)
            continue;
        return new MatchResult(m, convertedArgs);
    }
    return result;
}

From source file:org.vosao.rest.RestManager.java

private MatchResult matchQueryMethod(Object restService, String methodName, Map<String, String> queryParams) {
    MatchResult result = null;/*from   w  w  w.j a va 2  s .  c  o m*/
    for (Method m : restService.getClass().getMethods()) {
        if (!m.getName().equals(methodName))
            continue;
        if (m.isVarArgs())
            throw new IllegalArgumentException("Varargs are not supported");

        Class<?>[] paramTypes = m.getParameterTypes();
        if (paramTypes.length != queryParams.size())
            continue;
        List<String> paramsList = new ArrayList<String>();
        for (int i = 0; i < paramTypes.length; i++) {
            String paramName = getParamNameFromArgAnnotation(m.getParameterAnnotations()[i]);
            if (paramName != null && queryParams.containsKey(paramName)) {
                paramsList.add(queryParams.get(paramName));
            }
        }
        if (paramsList.size() != queryParams.size())
            continue;
        Object[] convertedArgs = null;
        try {
            convertedArgs = convertArgs(paramTypes, paramsList);
        } catch (Exception e) {
        }
        if (convertedArgs == null)
            continue;
        return new MatchResult(m, convertedArgs);
    }
    return result;
}

From source file:com.vilt.minium.impl.BaseWebElementsImpl.java

public Object invoke(Method method, Object... args) {
    if (method.isVarArgs()) {
        args = expandVarArgs(args);/*from   w  ww  . j av a 2  s  .c  om*/
    }
    String expression = computeExpression(this, isAsyncMethod(method), method.getName(), args);

    if (method.getReturnType() != Object.class && method.getReturnType().isAssignableFrom(this.getClass())) {
        T webElements = WebElementsFactoryHelper.createExpressionWebElements(factory, myself, expression);
        return webElements;
    } else {
        Object result = null;

        boolean async = isAsyncMethod(method);

        Iterable<WebElementsDriver<T>> webDrivers = candidateWebDrivers();

        if (method.getReturnType() == Void.TYPE) {
            for (WebElementsDriver<T> wd : webDrivers) {
                factory.getInvoker().invokeExpression(wd, async, expression);
            }
        } else {
            if (Iterables.size(webDrivers) == 0) {
                throw new WebElementsException("The expression has no frame or window to be evaluated to");
            } else if (Iterables.size(webDrivers) == 1) {
                WebElementsDriver<T> wd = Iterables.get(webDrivers, 0);
                result = factory.getInvoker().invokeExpression(wd, async, expression);
            } else {
                String sizeExpression = computeExpression(this, false, "size");
                WebElementsDriver<T> webDriverWithResults = null;

                for (WebElementsDriver<T> wd : webDrivers) {
                    long size = (Long) factory.getInvoker().invokeExpression(wd, async, sizeExpression);
                    if (size > 0) {
                        if (webDriverWithResults == null) {
                            webDriverWithResults = wd;
                        } else {
                            throw new WebElementsException(
                                    "Several frames or windows match the same expression, so value cannot be computed");
                        }
                    }
                }

                if (webDriverWithResults != null) {
                    result = factory.getInvoker().invokeExpression(webDriverWithResults, async, expression);
                }
            }
        }

        if (logger.isDebugEnabled()) {
            String val;
            if (method.getReturnType() == Void.TYPE) {
                val = "void";
            } else {
                val = StringUtils.abbreviate(argToStringFunction.apply(result), 40);
                if (val.startsWith("'") && !val.endsWith("'"))
                    val += "(...)'";
            }
            logger.debug("[Value: {}] {}", argToStringFunction.apply(result), expression);
        }

        // let's handle numbers when return type is int
        if (method.getReturnType() == Integer.TYPE) {
            return result == null ? 0 : ((Number) result).intValue();
        } else {
            return result;
        }
    }
}

From source file:com.espertech.esper.dataflow.core.DataFlowServiceImpl.java

private LogicalChannelBindingMethodDesc findMatchingMethod(String operatorName, Class target,
        LogicalChannel channelDesc, boolean isPunctuation) throws ExprValidationException {
    if (isPunctuation) {
        for (Method method : target.getMethods()) {
            if (method.getName().equals("onSignal")) {
                return new LogicalChannelBindingMethodDesc(method, LogicalChannelBindingTypePassAlong.INSTANCE);
            }//from   ww  w. jav  a 2  s . co m
        }
        return null;
    }

    LogicalChannelProducingPortCompiled outputPort = channelDesc.getOutputPort();

    Class[] expectedIndividual;
    Class expectedUnderlying;
    EventType expectedUnderlyingType;
    GraphTypeDesc typeDesc = outputPort.getGraphTypeDesc();

    if (typeDesc.isWildcard()) {
        expectedIndividual = new Class[0];
        expectedUnderlying = null;
        expectedUnderlyingType = null;
    } else {
        expectedIndividual = new Class[typeDesc.getEventType().getPropertyNames().length];
        int i = 0;
        for (EventPropertyDescriptor descriptor : typeDesc.getEventType().getPropertyDescriptors()) {
            expectedIndividual[i] = descriptor.getPropertyType();
            i++;
        }
        expectedUnderlying = typeDesc.getEventType().getUnderlyingType();
        expectedUnderlyingType = typeDesc.getEventType();
    }

    String channelSpecificMethodName = null;
    if (channelDesc.getConsumingOptStreamAliasName() != null) {
        channelSpecificMethodName = "on" + channelDesc.getConsumingOptStreamAliasName();
    }

    for (Method method : target.getMethods()) {

        boolean eligible = method.getName().equals("onInput");
        if (!eligible && method.getName().equals(channelSpecificMethodName)) {
            eligible = true;
        }

        if (!eligible) {
            continue;
        }

        // handle Object[]
        int numParams = method.getParameterTypes().length;
        Class[] paramTypes = method.getParameterTypes();

        if (expectedUnderlying != null) {
            if (numParams == 1
                    && JavaClassHelper.isSubclassOrImplementsInterface(paramTypes[0], expectedUnderlying)) {
                return new LogicalChannelBindingMethodDesc(method, LogicalChannelBindingTypePassAlong.INSTANCE);
            }
            if (numParams == 2 && JavaClassHelper.getBoxedType(paramTypes[0]) == Integer.class
                    && JavaClassHelper.isSubclassOrImplementsInterface(paramTypes[1], expectedUnderlying)) {
                return new LogicalChannelBindingMethodDesc(method,
                        new LogicalChannelBindingTypePassAlongWStream(channelDesc.getConsumingOpStreamNum()));
            }
        }

        if (numParams == 1
                && (paramTypes[0] == Object.class || (paramTypes[0] == Object[].class && method.isVarArgs()))) {
            return new LogicalChannelBindingMethodDesc(method, LogicalChannelBindingTypePassAlong.INSTANCE);
        }
        if (numParams == 2 && paramTypes[0] == int.class
                && (paramTypes[1] == Object.class || (paramTypes[1] == Object[].class && method.isVarArgs()))) {
            return new LogicalChannelBindingMethodDesc(method,
                    new LogicalChannelBindingTypePassAlongWStream(channelDesc.getConsumingOpStreamNum()));
        }

        // if exposing a method that exactly matches each property type in order, use that, i.e. "onInut(String p0, int p1)"
        if (expectedUnderlyingType instanceof ObjectArrayEventType
                && JavaClassHelper.isSignatureCompatible(expectedIndividual, method.getParameterTypes())) {
            return new LogicalChannelBindingMethodDesc(method, LogicalChannelBindingTypeUnwind.INSTANCE);
        }
    }

    Set<String> choices = new LinkedHashSet<String>();
    choices.add(Object.class.getSimpleName());
    choices.add("Object[]");
    if (expectedUnderlying != null) {
        choices.add(expectedUnderlying.getSimpleName());
    }
    throw new ExprValidationException("Failed to find onInput method on for operator '" + operatorName
            + "' class " + target.getName() + ", expected an onInput method that takes any of {"
            + CollectionUtil.toString(choices) + "}");
}