Search in sources :

Example 11 with Field

use of java.lang.reflect.Field in project antlrworks by antlr.

the class BrowserLauncher method loadClasses.

/**
	 * Called by a static initializer to load any classes, fields, and methods required at runtime
	 * to locate the user's web browser.
	 * @return <code>true</code> if all intialization succeeded
	 *			<code>false</code> if any portion of the initialization failed
	 */
private static boolean loadClasses() {
    switch(jvm) {
        case MRJ_2_0:
            try {
                Class<?> aeTargetClass = Class.forName("com.apple.MacOS.AETarget");
                Class osUtilsClass = Class.forName("com.apple.MacOS.OSUtils");
                Class<?> appleEventClass = Class.forName("com.apple.MacOS.AppleEvent");
                Class aeClass = Class.forName("com.apple.MacOS.ae");
                aeDescClass = Class.forName("com.apple.MacOS.AEDesc");
                aeTargetConstructor = aeTargetClass.getDeclaredConstructor(new Class[] { int.class });
                appleEventConstructor = appleEventClass.getDeclaredConstructor(new Class[] { int.class, int.class, aeTargetClass, int.class, int.class });
                aeDescConstructor = aeDescClass.getDeclaredConstructor(new Class[] { String.class });
                makeOSType = osUtilsClass.getDeclaredMethod("makeOSType", new Class[] { String.class });
                putParameter = appleEventClass.getDeclaredMethod("putParameter", new Class[] { int.class, aeDescClass });
                sendNoReply = appleEventClass.getDeclaredMethod("sendNoReply", new Class[] {});
                Field keyDirectObjectField = aeClass.getDeclaredField("keyDirectObject");
                keyDirectObject = (Integer) keyDirectObjectField.get(null);
                Field autoGenerateReturnIDField = appleEventClass.getDeclaredField("kAutoGenerateReturnID");
                kAutoGenerateReturnID = (Integer) autoGenerateReturnIDField.get(null);
                Field anyTransactionIDField = appleEventClass.getDeclaredField("kAnyTransactionID");
                kAnyTransactionID = (Integer) anyTransactionIDField.get(null);
            } catch (ClassNotFoundException cnfe) {
                errorMessage = cnfe.getMessage();
                return false;
            } catch (NoSuchMethodException nsme) {
                errorMessage = nsme.getMessage();
                return false;
            } catch (NoSuchFieldException nsfe) {
                errorMessage = nsfe.getMessage();
                return false;
            } catch (IllegalAccessException iae) {
                errorMessage = iae.getMessage();
                return false;
            }
            break;
        case MRJ_2_1:
            try {
                mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
                mrjOSTypeClass = Class.forName("com.apple.mrj.MRJOSType");
                Field systemFolderField = mrjFileUtilsClass.getDeclaredField("kSystemFolderType");
                kSystemFolderType = systemFolderField.get(null);
                findFolder = mrjFileUtilsClass.getDeclaredMethod("findFolder", new Class[] { mrjOSTypeClass });
                getFileCreator = mrjFileUtilsClass.getDeclaredMethod("getFileCreator", new Class[] { File.class });
                getFileType = mrjFileUtilsClass.getDeclaredMethod("getFileType", new Class[] { File.class });
            } catch (ClassNotFoundException cnfe) {
                errorMessage = cnfe.getMessage();
                return false;
            } catch (NoSuchFieldException nsfe) {
                errorMessage = nsfe.getMessage();
                return false;
            } catch (NoSuchMethodException nsme) {
                errorMessage = nsme.getMessage();
                return false;
            } catch (SecurityException se) {
                errorMessage = se.getMessage();
                return false;
            } catch (IllegalAccessException iae) {
                errorMessage = iae.getMessage();
                return false;
            }
            break;
        case MRJ_3_0:
            try {
                Class<?> linker = Class.forName("com.apple.mrj.jdirect.Linker");
                Constructor<?> constructor = linker.getConstructor(new Class[] { Class.class });
                linkage = constructor.newInstance(new Object[] { BrowserLauncher.class });
            } catch (ClassNotFoundException cnfe) {
                errorMessage = cnfe.getMessage();
                return false;
            } catch (NoSuchMethodException nsme) {
                errorMessage = nsme.getMessage();
                return false;
            } catch (InvocationTargetException ite) {
                errorMessage = ite.getMessage();
                return false;
            } catch (InstantiationException ie) {
                errorMessage = ie.getMessage();
                return false;
            } catch (IllegalAccessException iae) {
                errorMessage = iae.getMessage();
                return false;
            }
            break;
        case MRJ_3_1:
            try {
                mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
                openURL = mrjFileUtilsClass.getDeclaredMethod("openURL", new Class[] { String.class });
            } catch (ClassNotFoundException cnfe) {
                errorMessage = cnfe.getMessage();
                return false;
            } catch (NoSuchMethodException nsme) {
                errorMessage = nsme.getMessage();
                return false;
            }
            break;
        default:
            break;
    }
    return true;
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) File(java.io.File)

Example 12 with Field

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

the class TransformerBuilderTest method testDataFormatTransformer.

public void testDataFormatTransformer() throws Exception {
    CamelContext ctx = new DefaultCamelContext();
    RouteBuilder builder = new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            transformer().fromType("xml:foo").toType("json:bar").withDataFormat(new StringDataFormat());
            from("direct:input").log("test");
        }
    };
    ctx.addRoutes(builder);
    ctx.start();
    Transformer transformer = ctx.resolveTransformer(new DataType("xml:foo"), new DataType("json:bar"));
    assertNotNull(transformer);
    assertEquals(DataFormatTransformer.class, transformer.getClass());
    DataFormatTransformer dft = (DataFormatTransformer) transformer;
    Field f = DataFormatTransformer.class.getDeclaredField("dataFormatType");
    f.setAccessible(true);
    Object dataFormatType = f.get(dft);
    assertEquals(StringDataFormat.class, dataFormatType.getClass());
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) Field(java.lang.reflect.Field) ProcessorTransformer(org.apache.camel.impl.transformer.ProcessorTransformer) Transformer(org.apache.camel.spi.Transformer) DataFormatTransformer(org.apache.camel.impl.transformer.DataFormatTransformer) StringDataFormat(org.apache.camel.model.dataformat.StringDataFormat) DataType(org.apache.camel.spi.DataType) DataFormatTransformer(org.apache.camel.impl.transformer.DataFormatTransformer) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext)

Example 13 with Field

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

the class BindyAbstractDataFormat method createLinkedFieldsModel.

protected void createLinkedFieldsModel(Object model, Map<String, Object> row) throws IllegalAccessException {
    for (Field field : model.getClass().getDeclaredFields()) {
        Link linkField = field.getAnnotation(Link.class);
        if (linkField != null) {
            boolean accessible = field.isAccessible();
            field.setAccessible(true);
            if (!row.containsKey(field.getType().getName())) {
                row.put(field.getType().getName(), field.get(model));
            }
            field.setAccessible(accessible);
        }
    }
}
Also used : Field(java.lang.reflect.Field) Link(org.apache.camel.dataformat.bindy.annotation.Link)

Example 14 with Field

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

the class BindyAbstractFactory method link.

/**
     * Link objects together
     */
public void link(Map<String, Object> model) throws Exception {
    // Iterate class by class
    for (String link : annotatedLinkFields.keySet()) {
        List<Field> linkFields = annotatedLinkFields.get(link);
        // Iterate through Link fields list
        for (Field field : linkFields) {
            // Change protection for private field
            field.setAccessible(true);
            // Retrieve linked object
            String toClassName = field.getType().getName();
            Object to = model.get(toClassName);
            ObjectHelper.notNull(to, "No @link annotation has been defined for the object to link");
            field.set(model.get(field.getDeclaringClass().getName()), to);
        }
    }
}
Also used : Field(java.lang.reflect.Field)

Example 15 with Field

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

the class BindyCsvFactory method generateCsvPositionMap.

/**
     * Generate a table containing the data formatted and sorted with their position/offset
     * If the model is Ordered than a key is created combining the annotation @Section and Position of the field
     * If a relation @OneToMany is defined, than we iterate recursively through this function
     * The result is placed in the Map<Integer, List> results
     */
private void generateCsvPositionMap(Class<?> clazz, Object obj, Map<Integer, List<String>> results) throws Exception {
    String result = "";
    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        DataField datafield = field.getAnnotation(DataField.class);
        if (datafield != null) {
            if (obj != null) {
                // Retrieve the format, pattern and precision associated to the type
                Class<?> type = field.getType();
                // Create format
                FormattingOptions formattingOptions = ConverterUtils.convert(datafield, field.getType(), field.getAnnotation(BindyConverter.class), getLocale());
                Format<?> format = formatFactory.getFormat(formattingOptions);
                // Get field value
                Object value = field.get(obj);
                // If the field value is empty, populate it with the default value
                if (ObjectHelper.isNotEmpty(datafield.defaultValue()) && ObjectHelper.isEmpty(value)) {
                    value = datafield.defaultValue();
                }
                result = formatString(format, value);
                if (datafield.trim()) {
                    result = result.trim();
                }
                if (datafield.clip() && result.length() > datafield.length()) {
                    result = result.substring(0, datafield.length());
                }
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Value to be formatted: {}, position: {}, and its formatted value: {}", new Object[] { value, datafield.pos(), result });
                }
            } else {
                result = "";
            }
            Integer key;
            if (isMessageOrdered() && obj != null) {
                // Generate a key using the number of the section
                // and the position of the field
                Integer key1 = sections.get(obj.getClass().getName());
                Integer key2 = datafield.position();
                Integer keyGenerated = generateKey(key1, key2);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Key generated: {}, for section: {}", String.valueOf(keyGenerated), key1);
                }
                key = keyGenerated;
            } else {
                key = datafield.pos();
            }
            if (!results.containsKey(key)) {
                List<String> list = new LinkedList<String>();
                list.add(result);
                results.put(key, list);
            } else {
                List<String> list = results.get(key);
                list.add(result);
            }
        }
        OneToMany oneToMany = field.getAnnotation(OneToMany.class);
        if (oneToMany != null) {
            // Set global variable
            // Will be used during generation of CSV
            isOneToMany = true;
            List<?> list = (List<?>) field.get(obj);
            if (list != null) {
                Iterator<?> it = list.iterator();
                while (it.hasNext()) {
                    Object target = it.next();
                    generateCsvPositionMap(target.getClass(), target, results);
                }
            } else {
                // Call this function to add empty value
                // in the table
                generateCsvPositionMap(field.getClass(), null, results);
            }
        }
    }
}
Also used : BindyConverter(org.apache.camel.dataformat.bindy.annotation.BindyConverter) OneToMany(org.apache.camel.dataformat.bindy.annotation.OneToMany) LinkedList(java.util.LinkedList) Field(java.lang.reflect.Field) DataField(org.apache.camel.dataformat.bindy.annotation.DataField) DataField(org.apache.camel.dataformat.bindy.annotation.DataField) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList)

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