Java Class.getField Examples

Java Class.getField - 30 examples found. These are the top rated real world Java examples of Class.getField extracted from open source projects. You can rate examples to help us improve the quality of examples.
 /**
  * Returns a {@link Font} based on its name, height and style. Windows-specific strikeout and
  * underline flags are also supported.
  *
  * @param name the name of the font
  * @param size the size of the font
  * @param style the style of the font
  * @param strikeout the strikeout flag (warning: Windows only)
  * @param underline the underline flag (warning: Windows only)
  * @return {@link Font} The font matching the name, height, style, strikeout and underline
  */
 public static Font getFont(
     String name, int size, int style, boolean strikeout, boolean underline) {
   String fontName = name + '|' + size + '|' + style + '|' + strikeout + '|' + underline;
   Font font = m_fontMap.get(fontName);
   if (font == null) {
     FontData fontData = new FontData(name, size, style);
     if (strikeout || underline) {
       try {
         Class<?> logFontClass =
             Class.forName("org.eclipse.swt.internal.win32.LOGFONT"); // $NON-NLS-1$
         Object logFont = FontData.class.getField("data").get(fontData); // $NON-NLS-1$
         if (logFont != null && logFontClass != null) {
           if (strikeout) {
             logFontClass
                 .getField("lfStrikeOut")
                 .set(logFont, Byte.valueOf((byte) 1)); // $NON-NLS-1$
           }
           if (underline) {
             logFontClass
                 .getField("lfUnderline")
                 .set(logFont, Byte.valueOf((byte) 1)); // $NON-NLS-1$
           }
         }
       } catch (Throwable e) {
         System.err.println(
             "Unable to set underline or strikeout"
                 + " (probably on a non-Windows platform). "
                 + e); //$NON-NLS-1$ //$NON-NLS-2$
       }
     }
     font = new Font(Display.getCurrent(), fontData);
     m_fontMap.put(fontName, font);
   }
   return font;
 }
  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.
      }
    }
  }
Example #3
0
 String getClassForParameter(String object, String field, boolean type) {
   for (KVP def : comps.entries) {
     if (object.equals(def.getKey())) {
       String clname = def.getValue().toString();
       clname = getComponentClassName(clname);
       Class c;
       try {
         c = getClassLoader().loadClass(clname);
       } catch (ClassNotFoundException ex) {
         throw new ComponentException("Class not found: '" + clname + "'");
       }
       try {
         if (type) {
           String canName = c.getField(field).getType().getCanonicalName();
           if (canName == null) {
             throw new ComponentException("No canonical type name for : " + field);
           }
           return canName;
         } else {
           Role r = c.getField(field).getAnnotation(Role.class);
           if (r != null) {
             return r.value();
           }
           return Role.VARIABLE;
         }
       } catch (NoSuchFieldException ex) {
         throw new ComponentException("No such field: " + field);
       } catch (SecurityException ex) {
         throw new ComponentException("Cannot access : " + field);
       }
     }
   }
   throw new ComponentException(
       "Cannot find component '" + object + "'. in '" + object + "." + field + "'");
 }
 /** Is c1 an inner class of c2? */
 protected boolean isInnerClass(Class c1, final Class c2) {
   Class c = c1.getDeclaringClass();
   if (c == null) {
     try {
       final Field f = c1.getField("declaring$Class$Reference$0");
       c = (Class) f.get(null);
     } catch (final Exception e) {
     }
   }
   c1 = c;
   while (c != null) {
     if (c == c2) {
       return true;
     }
     c = c.getDeclaringClass();
     if (c == null) {
       try {
         final Field f = c1.getField("declaring$Class$Reference$0");
         c = (Class) f.get(null);
       } catch (final Exception e) {
       }
     }
     c1 = c;
   }
   return false;
 }
Example #5
0
  private void fillActivity() {
    ItemInfo item = mDbAdapter.getContentItem(mCategoryId, mIndex);
    Class res = R.drawable.class;

    int imageId = 0;
    try {
      imageId = res.getField(item.icon).getInt(null);
    } catch (Exception ex) {
      Log.w("ContentActivity", ex.getMessage());
    }
    ImageView img = (ImageView) findViewById(R.id.list_view_item_icon);
    img.setImageResource(imageId);

    TextView text = (TextView) findViewById(R.id.list_view_item_text);
    text.setText(item.name);

    text = (TextView) findViewById(R.id.list_view_like_num);
    text.setText(Integer.toString(item.likeCount));

    try {
      imageId = res.getField(item.picture).getInt(null);
    } catch (Exception ex) {
      Log.w("ContentActivity", ex.getMessage());
    }
    img = (ImageView) findViewById(R.id.picture);
    img.setImageResource(imageId);

    text = (TextView) findViewById(R.id._text);
    text.setText(item.text);
    fillList();
  }
  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