Example usage for java.lang.reflect Parameter getType

List of usage examples for java.lang.reflect Parameter getType

Introduction

In this page you can find the example usage for java.lang.reflect Parameter getType.

Prototype

public Class<?> getType() 

Source Link

Document

Returns a Class object that identifies the declared type for the parameter represented by this Parameter object.

Usage

From source file:ch.ifocusit.plantuml.utils.ClassUtils.java

public static Set<Class> getConcernedTypes(Parameter parameter) {
    Set<Class> classes = new HashSet<>();
    classes.add(parameter.getType());
    classes.addAll(getGenericTypes(parameter));
    return classes;
}

From source file:com.phoenixnap.oss.ramlapisync.naming.SchemaHelper.java

/**
 * Converts a simple parameter, ie String, or Boxed Primitive into
 * /*from   w  ww . j  a  v a  2 s .c  o m*/
 * @param param The Java Parameter to convert
 * @param paramComment The associated Javadoc if any
 * @return A map of query parameters that map into the supplied type
 */
public static Map<String, QueryParameter> convertParameterToQueryParameter(final Parameter param,
        final String paramComment) {
    QueryParameter queryParam = new QueryParameter();
    ApiParameterMetadata parameterMetadata = new ApiParameterMetadata(param);

    ParamType type = mapSimpleType(param.getType());

    if (type == null) {
        throw new IllegalArgumentException("This method is only applicable to simple types or primitives");
    }

    if (StringUtils.hasText(paramComment)) {
        queryParam.setDescription(paramComment);
    }

    // Populate parameter model with data such as name, type and required/not

    queryParam.setDisplayName(parameterMetadata.getName());
    queryParam.setType(mapSimpleType(param.getType()));
    if (StringUtils.hasText(parameterMetadata.getExample())) {
        queryParam.setExample(parameterMetadata.getExample());
    }
    queryParam.setRequired(!parameterMetadata.isNullable());
    queryParam.setRepeat(param.getType().isArray()); // TODO we could add validation info
    // here - maybe hook into JSR303
    // annotations
    return Collections.singletonMap(parameterMetadata.getName(), queryParam);
}

From source file:com.adaptc.mws.plugins.testing.transformations.TestMixinTransformation.java

private static Parameter[] copyParameters(Parameter[] parameterTypes) {
    Parameter[] newParameterTypes = new Parameter[parameterTypes.length];
    for (int i = 0; i < parameterTypes.length; i++) {
        Parameter parameterType = parameterTypes[i];
        Parameter newParameter = new Parameter(nonGeneric(parameterType.getType()), parameterType.getName(),
                parameterType.getInitialExpression());
        newParameter.addAnnotations(parameterType.getAnnotations());
        newParameterTypes[i] = newParameter;
    }//w w  w . jav  a2 s .  c  o m
    return newParameterTypes;
}

From source file:com.phoenixnap.oss.ramlapisync.naming.SchemaHelper.java

/**
 * Breaks down a class into component fields which are mapped as Query Parameters. If Javadoc is supplied, this will
 * be injected as comments/*from   ww w .j a v  a 2  s  .  c  om*/
 * 
 * @param param The Parameter representing the class to be converted into query parameters
 * @param javaDocStore The associated JavaDoc (if any)
 * @return a Map of Parameter RAML models keyed by parameter name
 */
public static Map<String, QueryParameter> convertClassToQueryParameters(final Parameter param,
        final JavaDocStore javaDocStore) {
    final Map<String, QueryParameter> outParams = new TreeMap<>();

    if (param == null || param.equals(Void.class)) {
        return outParams;
    }
    final ApiParameterMetadata parameterMetadata = new ApiParameterMetadata(param);

    if (mapSimpleType(param.getType()) != null) {
        throw new IllegalArgumentException(
                "This method should only be called on non primitive classes which will be broken down into query parameters");
    }

    try {
        for (Field field : param.getType().getDeclaredFields()) {
            if (!java.lang.reflect.Modifier.isStatic(field.getModifiers())
                    && !java.lang.reflect.Modifier.isTransient(field.getModifiers())
                    && !java.lang.reflect.Modifier.isVolatile(field.getModifiers())) {
                QueryParameter queryParam = new QueryParameter();

                // Check if we have comments
                JavaDocEntry paramComment = javaDocStore == null ? null
                        : javaDocStore.getJavaDoc(field.getName());
                if (paramComment != null && StringUtils.hasText(paramComment.getComment())) {
                    queryParam.setDescription(paramComment.getComment());
                }

                // Populate parameter model with data such as name, type and
                // required/not
                queryParam.setDisplayName(field.getName());
                ParamType simpleType = mapSimpleType(field.getType());
                queryParam.setType(simpleType == null ? ParamType.STRING : simpleType);
                queryParam.setRequired(parameterMetadata.isNullable());
                queryParam.setRepeat(false); // TODO we could add validation
                // info
                // here - maybe hook into
                // JSR303
                // annotations
                outParams.put(field.getName(), queryParam);
            }
        }
        return outParams;

    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.teradata.tempto.internal.ReflectionInjectorHelper.java

private Object createInstanceWithFields(Parameter[] parameters) {
    DynamicType.Builder<Object> objectBuilder = new ByteBuddy().subclass(Object.class).modifiers(PUBLIC);
    for (Parameter parameter : parameters) {
        objectBuilder = objectBuilder.defineField(parameter.getName(), parameter.getType(), PUBLIC)
                .annotateField(ArrayUtils.add(parameter.getAnnotations(), INJECT_ANNOTATION));
    }/*from  w ww .  jav a2s . co  m*/
    try {
        Class<?> createdClass = objectBuilder.make()
                .load(getSystemClassLoader(), ClassLoadingStrategy.Default.INJECTION).getLoaded();
        return createdClass.getConstructor().newInstance();
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException(e);
    }
}

From source file:koper.event.DataEventMessageDispatcher.java

/**
 * ? (?)/*from w ww  .j  a va2  s.c om*/
 *
 * @param method    
 * @param dataEvent ?
 * @return ?(Object[])
 * @throws InstantiationException
 * @throws IllegalAccessException
 */
private Object[] getDataEventMethodParameters(Method method, DataEvent dataEvent)
        throws InstantiationException, IllegalAccessException {

    final List<?> args = dataEvent.getArgs();
    final int length = method.getParameters().length;

    Object[] parameters = new Object[length];
    for (int i = 0; i < length; i++) {
        final Parameter parameter = method.getParameters()[i];

        final Class<?> parameterTypeClass = parameter.getType();

        Object o1;
        // ?args?, ?? DataEvent
        if (i == args.size()) {
            o1 = dataEvent;
        } else {
            Object o = args.get(i);

            if (parameterTypeClass.getName().equals(DataEvent.class.getName())) {
                o1 = dataEvent;
            } else {
                o1 = ConverterCenter.convertValue(parameterTypeClass, o);
            }
        }
        parameters[i] = o1;
    }
    return parameters;
}

From source file:com.zhaimi.message.event.DataEventMessageDispatcher.java

/**
 * ? (?)/*from  w  ww. j av  a2 s.c  o m*/
 *
 * @param method    
 * @param dataEvent ?
 * @return ?(Object[])
 * @throws InstantiationException
 * @throws IllegalAccessException
 */
private Object[] getDataEventMethodParameters(Method method, DataEvent dataEvent)
        throws InstantiationException, IllegalAccessException {

    final List<?> args = dataEvent.getArgs();
    final int length = method.getParameters().length;

    Object[] parameters = new Object[length];
    for (int i = 0; i < length; i++) {
        final Parameter parameter = method.getParameters()[i];

        final Class<?> parameterTypeClass = parameter.getType();

        Object o1;
        // ?args?, ?? DataEvent
        if (i == args.size()) {
            o1 = dataEvent;
        } else {
            Object o = args.get(i);

            if (parameterTypeClass.getName().equals("com.zhaimi.message.event.DataEvent")) {
                o1 = dataEvent;
            } else {
                o1 = ConverterCenter.convertValue(parameterTypeClass, o);
            }
        }
        parameters[i] = o1;
    }
    return parameters;
}

From source file:org.springframework.binding.method.MethodInvoker.java

/**
 * Invoke the method on the bean provided. Argument values are pulled from the provided argument source.
 * @param signature the definition of the method to invoke, including the method name and the method argument types
 * @param bean the bean to invoke/*from  w  ww  .  j a v  a  2  s. c o m*/
 * @param argumentSource the source for method arguments
 * @return the invoked method's return value
 * @throws MethodInvocationException the method could not be invoked
 */
public Object invoke(MethodSignature signature, Object bean, Object argumentSource)
        throws MethodInvocationException {
    Parameters parameters = signature.getParameters();
    Object[] arguments = new Object[parameters.size()];
    for (int i = 0; i < parameters.size(); i++) {
        Parameter parameter = parameters.getParameter(i);
        Object argument = parameter.evaluateArgument(argumentSource);
        arguments[i] = applyTypeConversion(argument, parameter.getType());
    }
    Class<?>[] parameterTypes = parameters.getTypesArray();
    for (int i = 0; i < parameterTypes.length; i++) {
        if (parameterTypes[i] == null) {
            Object argument = arguments[i];
            if (argument != null) {
                parameterTypes[i] = argument.getClass();
            }
        }
    }
    MethodKey key = new MethodKey(bean.getClass(), signature.getMethodName(), parameterTypes);
    try {
        Method method = methodCache.get(key);
        if (logger.isDebugEnabled()) {
            logger.debug("Invoking method with signature [" + key + "] with arguments "
                    + StylerUtils.style(arguments) + " on bean [" + bean + "]");

        }
        Object returnValue = method.invoke(bean, arguments);
        if (logger.isDebugEnabled()) {
            logger.debug("Invoked method with signature [" + key + "] returned value [" + returnValue + "]");
        }
        return returnValue;
    } catch (InvocationTargetException e) {
        throw new MethodInvocationException(signature, arguments, e.getTargetException());
    } catch (Exception e) {
        throw new MethodInvocationException(signature, arguments, e);
    }
}

From source file:ijfx.ui.widgets.PluginInfoPane.java

private Map<String, String> convert(Parameter item) {

    return Maps.newHashMap(ImmutableMap.<String, String>builder().put("name", item.getName())
            .put("type", item.getType().getSimpleName()).build());

}

From source file:guru.qas.martini.runtime.harness.MartiniCallable.java

protected Object[] getArguments(Step step, Method method, StepImplementation implementation) {
    Parameter[] parameters = method.getParameters();
    Object[] arguments = new Object[parameters.length];

    Map<String, String> exampleValues = null;

    Recipe recipe = martini.getRecipe();
    ScenarioDefinition definition = recipe.getScenarioDefinition();
    if (ScenarioOutline.class.isInstance(definition)) {
        exampleValues = new HashMap<>();
        ScenarioOutline outline = ScenarioOutline.class.cast(definition);

        int exampleLine = recipe.getLocation().getLine();

        List<Examples> examples = outline.getExamples();
        TableRow header = null;//from  w  w  w .  jav a  2s. c  o m
        TableRow match = null;
        for (Iterator<Examples> i = examples.iterator(); null == match && i.hasNext();) {
            Examples nextExamples = i.next();
            List<TableRow> rows = nextExamples.getTableBody();
            for (Iterator<TableRow> j = rows.iterator(); null == match && j.hasNext();) {
                TableRow row = j.next();
                if (row.getLocation().getLine() == exampleLine) {
                    match = row;
                    header = nextExamples.getTableHeader();
                }
            }
        }

        checkState(null != header, "unable to locate matching Examples table");
        List<TableCell> headerCells = header.getCells();
        List<TableCell> rowCells = match.getCells();
        checkState(headerCells.size() == rowCells.size(), "Examples header to row size mismatch");
        for (int i = 0; i < headerCells.size(); i++) {
            String headerValue = headerCells.get(i).getValue();
            String rowValue = rowCells.get(i).getValue();
            exampleValues.put(headerValue, rowValue);
        }
    }

    if (parameters.length > 0) {
        String text = step.getText();
        Pattern pattern = implementation.getPattern();
        Matcher matcher = pattern.matcher(text);
        checkState(matcher.find(), "unable to locate substitution parameters for pattern %s with input %s",
                pattern.pattern(), text);

        int groupCount = matcher.groupCount();
        for (int i = 0; i < groupCount; i++) {
            String parameterAsString = matcher.group(i + 1);
            Parameter parameter = parameters[i];
            Class<?> parameterType = parameter.getType();

            Object converted;
            if (null == exampleValues) {
                converted = conversionService.convert(parameterAsString, parameterType);
            } else {
                Matcher tableMatcher = OUTLINE_PATTERN.matcher(parameterAsString);
                checkState(tableMatcher.find(), "Example table keys must be in the format <key>");
                String key = tableMatcher.group(1);
                String tableValue = exampleValues.get(key);
                converted = conversionService.convert(tableValue, parameterType);
            }

            arguments[i] = converted;
        }
    }

    return arguments;
}