org.uiautomation.ios.mobileSafari.events.EventFactory.java Source code

Java tutorial

Introduction

Here is the source code for org.uiautomation.ios.mobileSafari.events.EventFactory.java

Source

/*
 * Copyright 2012 ios-driver committers.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package org.uiautomation.ios.mobileSafari.events;

import org.json.JSONObject;
import org.openqa.selenium.WebDriverException;

import java.lang.reflect.Constructor;
import java.util.logging.Level;
import java.util.logging.Logger;

public class EventFactory {

    private static final Logger log = Logger.getLogger(EventFactory.class.getName());

    public static Event createEvent(JSONObject message) {
        try {
            Event e = createObject(message);
            return e;
        } catch (Exception e) {
            log.log(Level.WARNING, "TODO", e);
            throw new WebDriverException("Error creating an event generated by the webview:" + message, e);
        }
    }

    private static Event createObject(JSONObject message) throws Exception {
        Class<? extends Event> clazz = new MessageTypeFinder(message).getAssociatedEvent();

        Class<?>[] parameterTypes = new Class[] { JSONObject.class };
        Object[] parameters = new JSONObject[] { message };

        Constructor<? extends Event> constructor = clazz.getConstructor(parameterTypes);
        return constructor.newInstance(parameters);
    }
}