Search in sources :

Example 6 with Field

use of java.lang.reflect.Field in project camel by apache.

the class CamelPostProcessorHelperTest method testBeanInject.

public void testBeanInject() throws Exception {
    CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
    MyBeanInjectBean bean = new MyBeanInjectBean();
    Field field = bean.getClass().getField("foo");
    BeanInject beanInject = field.getAnnotation(BeanInject.class);
    Class<?> type = field.getType();
    Object value = helper.getInjectionBeanValue(type, beanInject.value());
    field.set(bean, value);
    String out = bean.doSomething("World");
    assertEquals("Hello World", out);
}
Also used : Field(java.lang.reflect.Field) BeanInject(org.apache.camel.BeanInject)

Example 7 with Field

use of java.lang.reflect.Field in project camel by apache.

the class CamelPostProcessorHelperTest method testBeanInjectByType.

public void testBeanInjectByType() throws Exception {
    CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
    MyBeanInjectByTypeBean bean = new MyBeanInjectByTypeBean();
    Field field = bean.getClass().getField("foo");
    BeanInject beanInject = field.getAnnotation(BeanInject.class);
    Class<?> type = field.getType();
    Object value = helper.getInjectionBeanValue(type, beanInject.value());
    field.set(bean, value);
    String out = bean.doSomething("Camel");
    assertEquals("Hello Camel", out);
}
Also used : Field(java.lang.reflect.Field) BeanInject(org.apache.camel.BeanInject)

Example 8 with Field

use of java.lang.reflect.Field in project camel by apache.

the class CamelPostProcessorHelperTest method testEndpointInjectProducerTemplateFieldUrlUnknown.

public void testEndpointInjectProducerTemplateFieldUrlUnknown() throws Exception {
    CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
    MyEndpointInjectProducerTemplateUrlUnknown bean = new MyEndpointInjectProducerTemplateUrlUnknown();
    Field field = bean.getClass().getField("producer");
    EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
    Class<?> type = field.getType();
    String propertyName = "producer";
    try {
        helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), endpointInject.property(), propertyName, bean, "foo");
        fail("Should throw exception");
    } catch (ResolveEndpointFailedException e) {
        assertEquals("Failed to resolve endpoint: xxx://foo due to: No component found with scheme: xxx", e.getMessage());
    }
}
Also used : EndpointInject(org.apache.camel.EndpointInject) Field(java.lang.reflect.Field) ResolveEndpointFailedException(org.apache.camel.ResolveEndpointFailedException)

Example 9 with Field

use of java.lang.reflect.Field in project camel by apache.

the class CamelPostProcessorHelperTest method testEndpointInjectBothUriAndRef.

public void testEndpointInjectBothUriAndRef() throws Exception {
    CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
    MyEndpointBothUriAndRef bean = new MyEndpointBothUriAndRef();
    Field field = bean.getClass().getField("producer");
    EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
    Class<?> type = field.getType();
    String propertyName = "producer";
    try {
        helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), endpointInject.property(), propertyName, bean, "foo");
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
        assertEquals("Both uri and name is provided, only either one is allowed: uri=seda:foo, ref=myEndpoint", e.getMessage());
    }
}
Also used : EndpointInject(org.apache.camel.EndpointInject) Field(java.lang.reflect.Field)

Example 10 with Field

use of java.lang.reflect.Field in project camel by apache.

the class AvroComponent method applyToConfiguration.

/**
     * Applies endpoint parameters to configuration & resolves protocol and other required configuration properties.
     */
private void applyToConfiguration(AvroConfiguration config, URI endpointUri, Map<String, Object> parameters) throws Exception {
    config.parseURI(endpointUri, parameters, this);
    setProperties(config, parameters);
    if (config.getProtocol() == null && config.getProtocolClassName() != null) {
        Class<?> protocolClass = getCamelContext().getClassResolver().resolveClass(config.getProtocolClassName());
        if (protocolClass != null) {
            try {
                Field f = protocolClass.getField("PROTOCOL");
                if (f != null) {
                    Protocol protocol = (Protocol) f.get(null);
                    config.setProtocol(protocol);
                }
            } catch (NoSuchFieldException e) {
                ReflectData reflectData = ReflectData.get();
                config.setProtocol(reflectData.getProtocol(protocolClass));
                config.setReflectionProtocol(true);
            }
        }
    }
    if (config.getProtocol() == null) {
        throw new IllegalArgumentException("Avro configuration does not contain protocol");
    }
    if (config.getMessageName() != null && !config.getProtocol().getMessages().containsKey(config.getMessageName())) {
        throw new IllegalArgumentException("Message " + config.getMessageName() + " is not defined in protocol");
    }
    if (config.isSingleParameter()) {
        Map<String, Protocol.Message> messageMap = config.getProtocol().getMessages();
        Iterable<Protocol.Message> messagesToCheck = config.getMessageName() == null ? messageMap.values() : Collections.singleton(messageMap.get(config.getMessageName()));
        for (Protocol.Message message : messagesToCheck) {
            if (message.getRequest().getFields().size() != 1) {
                throw new IllegalArgumentException("Single parameter option can't be used with message " + message.getName() + " because it has " + message.getRequest().getFields().size() + " parameters defined");
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) ReflectData(org.apache.avro.reflect.ReflectData) Protocol(org.apache.avro.Protocol)

Aggregations

Field (java.lang.reflect.Field)4517 Method (java.lang.reflect.Method)500 Test (org.junit.Test)475 ArrayList (java.util.ArrayList)434 IOException (java.io.IOException)276 HashMap (java.util.HashMap)263 Map (java.util.Map)253 List (java.util.List)146 InvocationTargetException (java.lang.reflect.InvocationTargetException)136 Pattern (java.util.regex.Pattern)121 Matcher (java.util.regex.Matcher)115 File (java.io.File)93 HashSet (java.util.HashSet)91 Support_Field (tests.support.Support_Field)82 Annotation (java.lang.annotation.Annotation)77 Collection (java.util.Collection)70 SienaException (siena.SienaException)65 Test (org.testng.annotations.Test)64 Before (org.junit.Before)61 Type (java.lang.reflect.Type)57