private ImagePlus openImage(String directory, String name, String path) { Object o = tryOpen(directory, name, path); // if an image was returned, assume success if (o instanceof ImagePlus) return (ImagePlus) o; // try opening the file with LOCI Bio-Formats plugin - always check this last! // Do not call Bio-Formats if File>Import>Image Sequence is opening this file. if (o == null && (IJ.getVersion().compareTo("1.38j") < 0 || !IJ.redirectingErrorMessages()) && (new File(path).exists())) { Object loci = IJ.runPlugIn("loci.plugins.LociImporter", path); if (loci != null) { // plugin exists and was launched try { // check whether plugin was successful Class c = loci.getClass(); boolean success = c.getField("success").getBoolean(loci); boolean canceled = c.getField("canceled").getBoolean(loci); if (success || canceled) { width = IMAGE_OPENED; return null; } } catch (Exception exc) { } } } return null; } // openImage
static { if (!Boolean.getBoolean(GridSystemProperties.GG_JETTY_LOG_NO_OVERRIDE)) { String ctgrJetty = "org.eclipse.jetty"; // WARN for this category. String ctgrJettyUtil = "org.eclipse.jetty.util.log"; // ERROR for this... String ctgrJettyUtilComp = "org.eclipse.jetty.util.component"; // ...and this. try { Class<?> logCls = Class.forName("org.apache.log4j.Logger"); Object logJetty = logCls.getMethod("getLogger", String.class).invoke(logCls, ctgrJetty); Object logJettyUtil = logCls.getMethod("getLogger", String.class).invoke(logCls, ctgrJettyUtil); Object logJettyUtilComp = logCls.getMethod("getLogger", String.class).invoke(logCls, ctgrJettyUtilComp); Class<?> lvlCls = Class.forName("org.apache.log4j.Level"); Object warnLvl = lvlCls.getField("WARN").get(null); Object errLvl = lvlCls.getField("ERROR").get(null); logJetty.getClass().getMethod("setLevel", lvlCls).invoke(logJetty, warnLvl); logJettyUtil.getClass().getMethod("setLevel", lvlCls).invoke(logJetty, errLvl); logJettyUtilComp.getClass().getMethod("setLevel", lvlCls).invoke(logJetty, errLvl); } catch (Exception ignored) { // No-op. } } }
public static void main(String[] args) throws FileNotFoundException, IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { Class<Foobar> cls = Foobar.class; System.out.println(cls.getField("i").get(null)); Foobar.i = 6; System.out.println(cls.getField("i").get(null)); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("foobar.out")); out.writeObject(cls); }
/** * Prints one Unicode property value per line, along with its aliases, if any, for the given * unicodeVersion. * * @param unicodeVersion The Unicode version to print property values and aliases for * @throws UnicodeProperties.UnsupportedUnicodeVersionException if unicodeVersion is not supported */ private static void printUnicodePropertyValuesAndAliases(String unicodeVersion) throws UnicodeProperties.UnsupportedUnicodeVersionException { Pattern versionPattern = Pattern.compile("(\\d+)(?:\\.(\\d+))?(?:\\.\\d+)?"); Matcher matcher = versionPattern.matcher(unicodeVersion); if (!matcher.matches()) { throw new UnicodeProperties.UnsupportedUnicodeVersionException(); } String underscoreVersion = matcher.group(1) + (null == matcher.group(2) ? "_0" : "_" + matcher.group(2)); String[] propertyValues; String[] propertyValueAliases; try { Class<?> clazz = Class.forName("jflex.unicode.data.Unicode_" + underscoreVersion); Field field = clazz.getField("propertyValues"); propertyValues = (String[]) field.get(null); field = clazz.getField("propertyValueAliases"); propertyValueAliases = (String[]) field.get(null); } catch (Exception e) { throw new UnicodeProperties.UnsupportedUnicodeVersionException(); } SortedMap<String, SortedSet<String>> propertyValuesToAliases = new TreeMap<String, SortedSet<String>>(); for (String value : propertyValues) { propertyValuesToAliases.put(value, new TreeSet<String>()); } for (int i = 0; i < propertyValueAliases.length; i += 2) { String alias = propertyValueAliases[i]; String value = propertyValueAliases[i + 1]; SortedSet<String> aliases = propertyValuesToAliases.get(value); if (null == aliases) { aliases = new TreeSet<String>(); propertyValuesToAliases.put(value, aliases); } aliases.add(alias); } for (Map.Entry<String, SortedSet<String>> entry : propertyValuesToAliases.entrySet()) { String value = entry.getKey(); SortedSet<String> aliases = entry.getValue(); Out.print(value); if (aliases.size() > 0) { for (String alias : aliases) { Out.print(", " + alias); } } Out.println(""); } }
public void testChainedConstructor() throws Exception { Class cls = loadAndPatchClass("TestChainedConstructor.form", "ChainedConstructorTest"); Field scrollPaneField = cls.getField("myScrollPane"); Object instance = cls.newInstance(); JScrollPane scrollPane = (JScrollPane) scrollPaneField.get(instance); assertNotNull(scrollPane.getViewport().getView()); }
public boolean isAvailable() { try { // Diagnostic was introduced in the Java 1.6 compiler Class<?> diagnostic = Class.forName("javax.tools.Diagnostic"); diagnostic.getMethod("getKind"); // javax.lang.model.SourceVersion.RELEASE_7 field introduced in Java 7 Class<?> sourceVersion = Class.forName("javax.lang.model.SourceVersion"); sourceVersion.getField("RELEASE_7"); // only exists in Java 7 and later releases // javax.tools.Diagnostic and javax.lang.model.SourceVersion are also found in rt.jar; // to test if tools.jar is available, we need to test for a class only found in tools.jar Class.forName("com.sun.tools.javac.main.JavaCompiler"); // This is the class that javax.tools.ToolProvider.getSystemJavaCompiler() uses // We create an instance of that class directly, bypassing ToolProvider, because ToolProvider // returns null // if DrJava is started with just the JRE, instead of with the JDK, even if tools.jar is later // made available // to the class loader. JavaCompiler compiler = (JavaCompiler) (Class.forName("com.sun.tools.javac.api.JavacTool").newInstance()); return (compiler != null); } catch (Exception e) { return false; } catch (LinkageError e) { return false; } }
private JComponent getInstrumentedRootComponent(final String formFileName, final String className) throws Exception { Class cls = loadAndPatchClass(formFileName, className); Field rootComponentField = cls.getField("myRootComponent"); rootComponentField.setAccessible(true); Object instance = cls.newInstance(); return (JComponent) rootComponentField.get(instance); }
// Use reflection to get version since early versions // of ImageJ do not have the IJ.getVersion() method. String version() { String version = ""; try { Class ijClass = ImageJ.class; Field field = ijClass.getField("VERSION"); version = (String) field.get(ijClass); } catch (Exception ex) { } return version; }
/** * Initializes the fields of this class based on the first two lines of a case which include the * class name and parameter types. * * @return true is end of file is reached. */ private static boolean initFields(LineNumberReader commands, boolean generatingCommands) { results = new StringBuffer(); String className = getNextRealLine(commands); // End of file reached if (className == null) return true; // Load the class from file Class<? extends Invariant> classToTest = asInvClass(getClass(className)); try { classToTest.getField("dkconfig_enabled"); // Enable if needs to be done InvariantAddAndCheckTester.config.apply(className + ".enabled", "true"); } catch (NoSuchFieldException e) { // Otherwise do nothing } if (generatingCommands) { results.append(className + lineSep); } // Instantiate variables to be used as the names in the // invariants, variables are labeled a,b,c and so on as they // appear String typeString = getNextRealLine(commands); types = getTypes(typeString); VarInfo[] vars = getVarInfos(classToTest, types); PptSlice sl = createSlice(vars, daikon.test.Common.makePptTopLevel("Test:::OBJECT", vars)); // Create an actual instance of the class invariantToTest = instantiateClass(classToTest, sl); addModified = getAddModified(invariantToTest.getClass()); checkModified = getCheckModified(invariantToTest.getClass()); outputProducer = getOutputProducer(invariantToTest.getClass()); assert getArity(invariantToTest.getClass()) == types.length; if (generatingCommands) { results.append(typeString + lineSep); } return false; }
/** * Get the protocol version from protocol class. If the protocol class has a ProtocolAnnotation, * then get the protocol name from the annotation; otherwise the class name is the protocol name. */ public static long getProtocolVersion(Class<?> protocol) { if (protocol == null) { throw new IllegalArgumentException("Null protocol"); } long version; ProtocolInfo anno = protocol.getAnnotation(ProtocolInfo.class); if (anno != null) { version = anno.protocolVersion(); if (version != -1) return version; } try { Field versionField = protocol.getField("versionID"); versionField.setAccessible(true); return versionField.getLong(protocol); } catch (NoSuchFieldException ex) { throw new RuntimeException(ex); } catch (IllegalAccessException ex) { throw new RuntimeException(ex); } }
// Needed by NativeJavaObject serializer public static void writeAdapterObject(Object javaObject, ObjectOutputStream out) throws IOException { Class<?> cl = javaObject.getClass(); out.writeObject(cl.getSuperclass().getName()); Class<?>[] interfaces = cl.getInterfaces(); String[] interfaceNames = new String[interfaces.length]; for (int i = 0; i < interfaces.length; i++) interfaceNames[i] = interfaces[i].getName(); out.writeObject(interfaceNames); try { Object delegee = cl.getField("delegee").get(javaObject); out.writeObject(delegee); return; } catch (IllegalAccessException e) { } catch (NoSuchFieldException e) { } throw new IOException(); }
/** * Returns a compressing output stream using JZLib (http://www.jcraft.com/jzlib/). If JZLib is not * available on your system, this method will return null. */ public static OutputStream makeCompressingOutputStream(OutputStream out) { // to do this, we're going to use reflection. But here's the equivalent code: /* com.jcraft.jzlib.ZOutputStream stream = new com.jcraft.jzlib.ZOutputStream(out, com.jcraft.jzlib.JZlib.Z_BEST_SPEED); stream.setFlushMode(com.jcraft.jzlib.JZlib.Z_SYNC_FLUSH); return stream; */ try { Class outz = Class.forName("com.jcraft.jzlib.JZlib"); int Z_BEST_SPEED = outz.getField("Z_BEST_SPEED").getInt(null); int Z_SYNC_FLUSH = outz.getField("Z_SYNC_FLUSH").getInt(null); Class outc = Class.forName("com.jcraft.jzlib.ZOutputStream"); Object outi = outc.getConstructor(new Class[] {OutputStream.class, Integer.TYPE}) .newInstance(new Object[] {out, new Integer(Z_BEST_SPEED)}); outc.getMethod("setFlushMode", new Class[] {Integer.TYPE}) .invoke(outi, new Object[] {new Integer(Z_SYNC_FLUSH)}); return (OutputStream) outi; } catch (Exception e) { return null; } // failed, probably doesn't have JZLib on the system }