List of usage examples for java.lang.reflect Field getInt
@CallerSensitive @ForceInline public int getInt(Object obj) throws IllegalArgumentException, IllegalAccessException
From source file:jdroidremote.ServerFrame.java
public static void typeCharacter(String letter) { System.out.println(letter);//ww w . j a v a2 s.com Robot robot = null; try { robot = new Robot(); } catch (Exception ex) { ex.printStackTrace(); } if (Character.isLetter(letter.charAt(0))) { try { boolean upperCase = Character.isUpperCase(letter.charAt(0)); String variableName = "VK_" + letter.toUpperCase(); KeyEvent ke = new KeyEvent(new JTextField(), 0, 0, 0, 0, ' '); Class clazz = ke.getClass(); Field field = clazz.getField(variableName); int keyCode = field.getInt(ke); //System.out.println(keyCode + " = keyCode"); robot.delay(80); if (upperCase) { robot.keyPress(KeyEvent.VK_SHIFT); } robot.keyPress(keyCode); robot.keyRelease(keyCode); if (upperCase) { robot.keyRelease(KeyEvent.VK_SHIFT); } } catch (Exception e) { System.out.println(e); } } else if (letter.equals(".")) { robot.keyPress(KeyEvent.VK_PERIOD); //keyCode 46 robot.keyRelease(KeyEvent.VK_PERIOD); } else if (letter.equals("!")) { robot.keyPress(KeyEvent.VK_SHIFT); //keyCode 16 robot.keyPress(KeyEvent.VK_1); //keycode 49 robot.keyRelease(KeyEvent.VK_1); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (letter.equals(" ")) { robot.keyPress(KeyEvent.VK_SPACE); robot.keyRelease(KeyEvent.VK_SPACE); } else if (letter.equals("?")) { robot.keyPress(KeyEvent.VK_SHIFT); //keyCode 16 robot.keyPress(KeyEvent.VK_SLASH); //keyCode 47 robot.keyRelease(KeyEvent.VK_SLASH); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (letter.equals(",")) { robot.keyPress(KeyEvent.VK_COMMA); robot.keyRelease(KeyEvent.VK_COMMA); } else if (letter.equals("@enter")) { robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); } else if (letter.equals("@backspace")) { robot.keyPress(KeyEvent.VK_BACK_SPACE); robot.keyRelease(KeyEvent.VK_BACK_SPACE); } }
From source file:edu.umd.cs.marmoset.utilities.MarmosetUtilities.java
/** * Uses reflection to extract the pid, a private field of the private class UNIXProcess. * This will fail on any non-Unix platform that doesn't use UNIXProcess. It may * fail if the UNIXProcess class changes at all. It may fail anyway for unpredictable * reasons./*from w w w . jav a 2 s . c o m*/ * @param process The process * @return the pid of this process */ public static int getPid(Process process) { try { Class<? extends Process> processClass = process.getClass(); Field pidField = processClass.getDeclaredField("pid"); pidField.setAccessible(true); return pidField.getInt(process); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static ContentValues objectToContentValues(Object object) { if (object == null) { throw new IllegalArgumentException("please check your argument"); }/*from www. j av a 2 s . c om*/ ContentValues contentValues = new ContentValues(); try { Field[] fields = object.getClass().getDeclaredFields(); for (Field field : fields) { String fieldName = field.getName(); Type type = field.getType(); field.setAccessible(true); if (type.equals(String.class)) { contentValues.put(fieldName, field.get(object).toString()); } else if (type.equals(Integer.class) || type.equals(Integer.TYPE)) { contentValues.put(fieldName, field.getInt(object)); } else if (type.equals(Float.class) || type.equals(Float.TYPE)) { contentValues.put(fieldName, field.getFloat(object)); } else if (type.equals(Long.class) || type.equals(Long.TYPE)) { contentValues.put(fieldName, field.getLong(object)); } else if (type.equals(Double.class) || type.equals(Double.TYPE)) { contentValues.put(fieldName, field.getDouble(object)); } else if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) { contentValues.put(fieldName, field.getBoolean(object)); } } } catch (IllegalAccessException | IllegalArgumentException e) { e.printStackTrace(); } return contentValues; }
From source file:com.cardvlaue.sys.util.ScreenUtil.java
/** * ?????MIUI 6?//from ww w . j a va2 s.c o m */ public static void setStatusBarDarkMode(boolean darkmode, Activity activity) { Class<? extends Window> clazz = activity.getWindow().getClass(); try { int darkModeFlag = 0; Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams"); Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE"); darkModeFlag = field.getInt(layoutParams); Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class); extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag); } catch (Exception e) { // e.printStackTrace(); } }
From source file:Main.java
public static BitmapFactory.Options getBitmapOptions(DisplayMetrics mDisplayMetrics) { try {// w ww .j a va2s. c om // TODO I think this can all be done without reflection now because all these properties are SDK 4 final Field density = DisplayMetrics.class.getDeclaredField("DENSITY_DEFAULT"); final Field inDensity = BitmapFactory.Options.class.getDeclaredField("inDensity"); final Field inTargetDensity = BitmapFactory.Options.class.getDeclaredField("inTargetDensity"); final Field targetDensity = DisplayMetrics.class.getDeclaredField("densityDpi"); final BitmapFactory.Options options = new BitmapFactory.Options(); inDensity.setInt(options, density.getInt(null)); inTargetDensity.setInt(options, targetDensity.getInt(mDisplayMetrics)); return options; } catch (final IllegalAccessException ex) { Log.d(TAG, "Couldn't access fields.", ex); } catch (final NoSuchFieldException ex) { Log.d(TAG, "Couldn't find fields.", ex); } return null; }
From source file:com.xxxifan.devbox.core.util.ViewUtils.java
/** * set status bar icon to light theme, which is called dark mode. * should be called in onCreate()/* w w w . j a va2s . c o m*/ */ public static void setStatusBarLightMode(Activity activity, boolean lightMode) { if (activity == null || activity.getWindow() == null) { return; } Window window = activity.getWindow(); boolean changed = false; // try miui try { Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams"); Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE"); int darkIcon = field.getInt(layoutParams); Method extraFlagField = window.getClass().getMethod("setExtraFlags", int.class, int.class); extraFlagField.invoke(window, lightMode ? darkIcon : 0, darkIcon); changed = true; } catch (Exception ignored) { } // try flyme try { WindowManager.LayoutParams lp = window.getAttributes(); Field darkIcon = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON"); Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags"); darkIcon.setAccessible(true); meizuFlags.setAccessible(true); int bit = darkIcon.getInt(null); int value = meizuFlags.getInt(lp); if (lightMode) { value |= bit; } else { value &= ~bit; } meizuFlags.setInt(lp, value); window.setAttributes(lp); changed = true; } catch (Exception ignored) { } if (!changed && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { int visibility = window.getDecorView().getSystemUiVisibility(); if (lightMode) { visibility |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; } else { visibility &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; } window.getDecorView().setSystemUiVisibility(visibility); } }
From source file:org.apache.hadoop.gateway.services.security.impl.X509CertificateUtil.java
/** * Create a self-signed X.509 Certificate * @param dn the X.509 Distinguished Name, eg "CN=Test, L=London, C=GB" * @param pair the KeyPair//from www.j a v a 2 s. c o m * @param days how many days from now the Certificate is valid for * @param algorithm the signing algorithm, eg "SHA1withRSA" */ public static X509Certificate generateCertificate(String dn, KeyPair pair, int days, String algorithm) throws GeneralSecurityException, IOException { PrivateKey privkey = pair.getPrivate(); Object x509CertImplObject = null; try { Date from = new Date(); Date to = new Date(from.getTime() + days * 86400000l); Class<?> certInfoClass = Class.forName(getX509CertInfoModuleName()); Constructor<?> certInfoConstr = certInfoClass.getConstructor(); Object certInfoObject = certInfoConstr.newInstance(); // CertificateValidity interval = new CertificateValidity(from, to); Class<?> certValidityClass = Class.forName(getX509CertifValidityModuleName()); Constructor<?> certValidityConstr = certValidityClass .getConstructor(new Class[] { Date.class, Date.class }); Object certValidityObject = certValidityConstr.newInstance(from, to); BigInteger sn = new BigInteger(64, new SecureRandom()); // X500Name owner = new X500Name(dn); Class<?> x500NameClass = Class.forName(getX509X500NameModuleName()); Constructor<?> x500NameConstr = x500NameClass.getConstructor(new Class[] { String.class }); Object x500NameObject = x500NameConstr.newInstance(dn); Method methodSET = certInfoObject.getClass().getMethod("set", String.class, Object.class); // info.set(X509CertInfo.VALIDITY, interval); methodSET.invoke(certInfoObject, getSetField(certInfoObject, "VALIDITY"), certValidityObject); // info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(sn)); Class<?> certificateSerialNumberClass = Class.forName(getCertificateSerialNumberModuleName()); Constructor<?> certificateSerialNumberConstr = certificateSerialNumberClass .getConstructor(new Class[] { BigInteger.class }); Object certificateSerialNumberObject = certificateSerialNumberConstr.newInstance(sn); methodSET.invoke(certInfoObject, getSetField(certInfoObject, "SERIAL_NUMBER"), certificateSerialNumberObject); // info.set(X509CertInfo.SUBJECT, new CertificateSubjectName(owner)); try { Class<?> certificateSubjectNameClass = Class.forName(getCertificateSubjectNameModuleName()); Constructor<?> certificateSubjectNameConstr = certificateSubjectNameClass .getConstructor(new Class[] { x500NameClass }); Object certificateSubjectNameObject = certificateSubjectNameConstr.newInstance(x500NameObject); methodSET.invoke(certInfoObject, getSetField(certInfoObject, "SUBJECT"), certificateSubjectNameObject); } catch (InvocationTargetException ite) { methodSET.invoke(certInfoObject, getSetField(certInfoObject, "SUBJECT"), x500NameObject); } // info.set(X509CertInfo.ISSUER, new CertificateIssuerName(owner)); try { Class<?> certificateIssuerNameClass = Class.forName(getCertificateIssuerNameModuleName()); Constructor<?> certificateIssuerNameConstr = certificateIssuerNameClass .getConstructor(new Class[] { x500NameClass }); Object certificateIssuerNameObject = certificateIssuerNameConstr.newInstance(x500NameObject); methodSET.invoke(certInfoObject, getSetField(certInfoObject, "ISSUER"), certificateIssuerNameObject); } catch (InvocationTargetException ite) { methodSET.invoke(certInfoObject, getSetField(certInfoObject, "ISSUER"), x500NameObject); } // info.set(X509CertInfo.KEY, new CertificateX509Key(pair.getPublic())); Class<?> certificateX509KeyClass = Class.forName(getCertificateX509KeyModuleName()); Constructor<?> certificateX509KeyConstr = certificateX509KeyClass .getConstructor(new Class[] { PublicKey.class }); Object certificateX509KeyObject = certificateX509KeyConstr.newInstance(pair.getPublic()); methodSET.invoke(certInfoObject, getSetField(certInfoObject, "KEY"), certificateX509KeyObject); // info.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3)); Class<?> certificateVersionClass = Class.forName(getCertificateVersionModuleName()); Constructor<?> certificateVersionConstr = certificateVersionClass .getConstructor(new Class[] { int.class }); Constructor<?> certificateVersionConstr0 = certificateVersionClass.getConstructor(); Object certInfoObject0 = certificateVersionConstr0.newInstance(); Field v3IntField = certInfoObject0.getClass().getDeclaredField("V3"); v3IntField.setAccessible(true); int fValue = (int) v3IntField.getInt(certInfoObject0); Object certificateVersionObject = certificateVersionConstr.newInstance(fValue); methodSET.invoke(certInfoObject, getSetField(certInfoObject, "VERSION"), certificateVersionObject); // AlgorithmId algo = new AlgorithmId(AlgorithmId.md5WithRSAEncryption_oid); Class<?> algorithmIdClass = Class.forName(getAlgorithmIdModuleName()); Field md5WithRSAField = algorithmIdClass.getDeclaredField("md5WithRSAEncryption_oid"); md5WithRSAField.setAccessible(true); Class<?> objectIdentifierClass = Class.forName(getObjectIdentifierModuleName()); Object md5WithRSAValue = md5WithRSAField.get(algorithmIdClass); Constructor<?> algorithmIdConstr = algorithmIdClass .getConstructor(new Class[] { objectIdentifierClass }); Object algorithmIdObject = algorithmIdConstr.newInstance(md5WithRSAValue); // info.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algo)); Class<?> certificateAlgorithmIdClass = Class.forName(getCertificateAlgorithmIdModuleName()); Constructor<?> certificateAlgorithmIdConstr = certificateAlgorithmIdClass .getConstructor(new Class[] { algorithmIdClass }); Object certificateAlgorithmIdObject = certificateAlgorithmIdConstr.newInstance(algorithmIdObject); methodSET.invoke(certInfoObject, getSetField(certInfoObject, "ALGORITHM_ID"), certificateAlgorithmIdObject); // Sign the cert to identify the algorithm that's used. // X509CertImpl cert = new X509CertImpl(info); Class<?> x509CertImplClass = Class.forName(getX509CertImplModuleName()); Constructor<?> x509CertImplConstr = x509CertImplClass.getConstructor(new Class[] { certInfoClass }); x509CertImplObject = x509CertImplConstr.newInstance(certInfoObject); // cert.sign(privkey, algorithm); Method methoSIGN = x509CertImplObject.getClass().getMethod("sign", PrivateKey.class, String.class); methoSIGN.invoke(x509CertImplObject, privkey, algorithm); // Update the algorith, and resign. // algo = (AlgorithmId)cert.get(X509CertImpl.SIG_ALG); Method methoGET = x509CertImplObject.getClass().getMethod("get", String.class); String sig_alg = getSetField(x509CertImplObject, "SIG_ALG"); String certAlgoIdNameValue = getSetField(certificateAlgorithmIdObject, "NAME"); String certAlgoIdAlgoValue = getSetField(certificateAlgorithmIdObject, "ALGORITHM"); // info.set(CertificateAlgorithmId.NAME + "." + CertificateAlgorithmId.ALGORITHM, algo); methodSET.invoke(certInfoObject, certAlgoIdNameValue + "." + certAlgoIdAlgoValue, methoGET.invoke(x509CertImplObject, sig_alg)); // cert = new X509CertImpl(info); x509CertImplObject = x509CertImplConstr.newInstance(certInfoObject); // cert.sign(privkey, algorithm); methoSIGN.invoke(x509CertImplObject, privkey, algorithm); } catch (Exception e) { LOG.failedToGenerateCertificate(e); } return (X509Certificate) x509CertImplObject; }
From source file:com.l2jfree.network.mmocore.packethandlers.PacketDefinition.java
public static Integer findOpcode(Class<?> clazz, String fieldName, boolean required) throws Exception { // easier than checking all the modifiers, visibility, type, etc :) final Field field; try {/* w ww .j a va 2 s . c o m*/ field = clazz.getField(fieldName); } catch (NoSuchFieldException e) { if (required) { System.err.println("Missing 'public static final int " + fieldName + "'!"); throw e; } else return null; // ignore as it was expected } return field.getInt(null); }
From source file:org.eclipse.wb.internal.swt.model.property.editor.AcceleratorPropertyEditor.java
/** * Prepares {@link Map}'s for key code/name conversion. */// w w w .j av a2s . c o m private static void prepareKeyMaps() { if (m_keyCodeToName == null) { m_keyFields = Lists.newArrayList(); m_keyCodeToName = Maps.newTreeMap(); m_keyNameToCode = Maps.newTreeMap(); // add fields ExecutionUtils.runLog(new RunnableEx() { public void run() throws Exception { // add key codes from SWT for (Field field : SWT.class.getFields()) { String fieldName = field.getName(); int fieldValue = field.getInt(null); if (hasBits(fieldValue, SWT.KEYCODE_BIT)) { m_keyFields.add(fieldName); m_keyCodeToName.put(fieldValue, fieldName); m_keyNameToCode.put(fieldName, fieldValue); } } // add numbers for (char c = '0'; c < '9'; c++) { String charName = Character.toString(c); int charValue = c; m_keyFields.add(charName); m_keyCodeToName.put(charValue, charName); m_keyNameToCode.put(charName, charValue); } // add characters for (char c = 'A'; c < 'Z'; c++) { String charName = Character.toString(c); int charValue = c; m_keyFields.add(charName); m_keyCodeToName.put(charValue, charName); m_keyNameToCode.put(charName, charValue); } } }); } }
From source file:com.xlythe.engine.theme.Theme.java
@SuppressWarnings("rawtypes") public static void buildResourceMap(Class r) { RES_MAP = new SparseArray<Theme.Res>(); try {//w w w . j ava 2 s . co m Log.d("Theme", "Building resource map"); Class color = Class.forName(r.getName() + "$color"); for (Field f : color.getFields()) { RES_MAP.put(f.getInt(null), new Res(COLOR, f.getName())); } Log.d("Theme", "color loaded"); Class drawable = Class.forName(r.getName() + "$drawable"); for (Field f : drawable.getFields()) { RES_MAP.put(f.getInt(null), new Res(DRAWABLE, f.getName())); } Log.d("Theme", "drawable loaded"); Class bool = Class.forName(r.getName() + "$bool"); for (Field f : bool.getFields()) { RES_MAP.put(f.getInt(null), new Res(BOOLEAN, f.getName())); } Log.d("Theme", "bool loaded"); Class raw = Class.forName(r.getName() + "$raw"); for (Field f : raw.getFields()) { RES_MAP.put(f.getInt(null), new Res(RAW, f.getName())); } Log.d("Theme", "raw loaded"); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { // Do nothing here } }