/** * Factory method, equivalent to a "fromXML" for step creation. Looks for a class with the same * name as the XML tag, with the first letter capitalized. For example, <call /> is * abbot.script.Call. */ public static Step createStep(Resolver resolver, Element el) throws InvalidScriptException { String tag = el.getName(); Map attributes = createAttributeMap(el); String name = tag.substring(0, 1).toUpperCase() + tag.substring(1); if (tag.equals(TAG_WAIT)) { attributes.put(TAG_WAIT, "true"); name = "Assert"; } try { name = "abbot.script." + name; Log.debug("Instantiating " + name); Class cls = Class.forName(name); try { // Steps with contents require access to the XML element Class[] argTypes = new Class[] {Resolver.class, Element.class, Map.class}; Constructor ctor = cls.getConstructor(argTypes); return (Step) ctor.newInstance(new Object[] {resolver, el, attributes}); } catch (NoSuchMethodException nsm) { // All steps must support this ctor Class[] argTypes = new Class[] {Resolver.class, Map.class}; Constructor ctor = cls.getConstructor(argTypes); return (Step) ctor.newInstance(new Object[] {resolver, attributes}); } } catch (ClassNotFoundException cnf) { String msg = Strings.get("step.unknown_tag", new Object[] {tag}); throw new InvalidScriptException(msg); } catch (InvocationTargetException ite) { Log.warn(ite); throw new InvalidScriptException(ite.getTargetException().getMessage()); } catch (Exception exc) { Log.warn(exc); throw new InvalidScriptException(exc.getMessage()); } }
/** * This method is called when 'Finish' button is pressed in the wizard. We will create an * operation and run it using wizard as execution context. */ public boolean performFinish() { final String containerName = page.getContainerName(); final String fileName = page.getFileName(); IRunnableWithProgress op = new IRunnableWithProgress() { public void run(IProgressMonitor monitor) throws InvocationTargetException { try { doFinish(containerName, fileName, monitor); } catch (CoreException e) { throw new InvocationTargetException(e); } finally { monitor.done(); } } }; try { getContainer().run(true, false, op); } catch (InterruptedException e) { return false; } catch (InvocationTargetException e) { Throwable realException = e.getTargetException(); MessageDialog.openError(getShell(), "Error", realException.getMessage()); return false; } return true; }
/** * This creates a new <code>{@link Document}</code> from an existing <code>InputStream</code> by * letting a DOM parser handle parsing using the supplied stream. * * @param in <code>InputStream</code> to parse. * @param validate <code>boolean</code> to indicate if validation should occur. * @return <code>Document</code> - instance ready for use. * @throws IOException when I/O error occurs. * @throws JDOMException when errors occur in parsing. */ public Document getDocument(InputStream in, boolean validate) throws IOException, JDOMException { try { // Load the parser class Class parserClass = Class.forName("org.apache.xerces.parsers.DOMParser"); Object parser = parserClass.newInstance(); // Set validation Method setFeature = parserClass.getMethod("setFeature", new Class[] {java.lang.String.class, boolean.class}); setFeature.invoke( parser, new Object[] {"http://xml.org/sax/features/validation", new Boolean(validate)}); // Set namespaces true setFeature.invoke( parser, new Object[] {"http://xml.org/sax/features/namespaces", new Boolean(true)}); // Set the error handler if (validate) { Method setErrorHandler = parserClass.getMethod("setErrorHandler", new Class[] {ErrorHandler.class}); setErrorHandler.invoke(parser, new Object[] {new BuilderErrorHandler()}); } // Parse the document Method parse = parserClass.getMethod("parse", new Class[] {org.xml.sax.InputSource.class}); parse.invoke(parser, new Object[] {new InputSource(in)}); // Get the Document object Method getDocument = parserClass.getMethod("getDocument", null); Document doc = (Document) getDocument.invoke(parser, null); return doc; } catch (InvocationTargetException e) { Throwable targetException = e.getTargetException(); if (targetException instanceof org.xml.sax.SAXParseException) { SAXParseException parseException = (SAXParseException) targetException; throw new JDOMException( "Error on line " + parseException.getLineNumber() + " of XML document: " + parseException.getMessage(), e); } else if (targetException instanceof IOException) { IOException ioException = (IOException) targetException; throw ioException; } else { throw new JDOMException(targetException.getMessage(), e); } } catch (Exception e) { throw new JDOMException(e.getClass().getName() + ": " + e.getMessage(), e); } }
/** * Try to execute given method on given object. Handles invocation target exceptions. XXX nearly * duplicates tryMethod in JGEngine. * * @return null means method does not exist or returned null/void */ static Object tryMethod(Object o, String name, Object[] args) { try { Method met = JREEngine.getMethod(o.getClass(), name, args); if (met == null) return null; return met.invoke(o, args); } catch (InvocationTargetException ex) { Throwable ex_t = ex.getTargetException(); ex_t.printStackTrace(); return null; } catch (IllegalAccessException ex) { System.err.println("Unexpected exception:"); ex.printStackTrace(); return null; } }
public void loadAndRun(String className) throws Throwable { // System.out.println("Loading " + className + "..."); Class testClass = new VerifyClassLoader().loadClass(className); // System.out.println("Loaded " + className); try { Method main = testClass.getMethod("main", new Class[] {String[].class}); // System.out.println("Running " + className); main.invoke(null, new Object[] {new String[] {}}); // System.out.println("Finished running " + className); } catch (NoSuchMethodException e) { return; } catch (InvocationTargetException e) { throw e.getTargetException(); } }
// Lookup the value corresponding to a key found in catalog.getKeys(). // Here we assume that the catalog returns a non-inherited value for // these keys. FIXME: Not true. Better see whether handleGetObject is // public - it is in ListResourceBundle and PropertyResourceBundle. private Object lookup(String key) { Object value = null; if (lookupMethod != null) { try { value = lookupMethod.invoke(catalog, new Object[] {key}); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.getTargetException().printStackTrace(); } } else { try { value = catalog.getObject(key); } catch (MissingResourceException e) { } } return value; }
private void dump() throws IOException { lookupMethod = null; try { lookupMethod = catalog.getClass().getMethod("lookup", new Class[] {java.lang.String.class}); } catch (NoSuchMethodException e) { } catch (SecurityException e) { } Method pluralMethod = null; try { pluralMethod = catalog.getClass().getMethod("get_msgid_plural_table", new Class[0]); } catch (NoSuchMethodException e) { } catch (SecurityException e) { } Field pluralField = null; try { pluralField = catalog.getClass().getField("plural"); } catch (NoSuchFieldException e) { } catch (SecurityException e) { } // Search for the header entry. { Object header_entry = null; Enumeration keys = catalog.getKeys(); while (keys.hasMoreElements()) if ("".equals(keys.nextElement())) { header_entry = lookup(""); break; } // If there is no header entry, fake one. // FIXME: This is not needed; right after po_lex_charset_init set // the PO charset to UTF-8. if (header_entry == null) header_entry = "Content-Type: text/plain; charset=UTF-8\n"; dumpMessage("", null, header_entry); } // Now the other messages. { Enumeration keys = catalog.getKeys(); Object plural = null; if (pluralMethod != null) { // msgfmt versions > 0.13.1 create a static get_msgid_plural_table() // method. try { plural = pluralMethod.invoke(catalog, new Object[0]); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.getTargetException().printStackTrace(); } } else if (pluralField != null) { // msgfmt versions <= 0.13.1 create a static plural field. try { plural = pluralField.get(catalog); } catch (IllegalAccessException e) { e.printStackTrace(); } } if (plural instanceof String[]) { // A GNU gettext created class with plural handling, Java2 format. int i = 0; while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); Object value = lookup(key); String key_plural = (value instanceof String[] ? ((String[]) plural)[i++] : null); if (!"".equals(key)) dumpMessage(key, key_plural, value); } if (i != ((String[]) plural).length) throw new RuntimeException("wrong plural field length"); } else if (plural instanceof Hashtable) { // A GNU gettext created class with plural handling, Java format. while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); if (!"".equals(key)) { Object value = lookup(key); String key_plural = (value instanceof String[] ? (String) ((Hashtable) plural).get(key) : null); dumpMessage(key, key_plural, value); } } } else if (plural == null) { // No plural handling. while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); if (!"".equals(key)) dumpMessage(key, null, lookup(key)); } } else throw new RuntimeException("wrong plural field value"); } }