List of usage examples for java.lang.reflect Modifier isFinal
public static boolean isFinal(int mod)
From source file:com.github.wshackle.java4cpp.J4CppMain.java
private static boolean addSetterMethod(Field f, Class clss, List<Class> classes) { if (!Modifier.isPublic(f.getModifiers())) { return false; }/*from ww w . ja v a2s.c o m*/ if (!f.getType().isPrimitive() && !String.class.equals(f.getType()) && !classes.contains(f.getType())) { return false; } if (Modifier.isFinal(f.getModifiers())) { return false; } Method ma[] = clss.getMethods(); for (int i = 0; i < ma.length; i++) { Method method = ma[i]; if (method.getName().equalsIgnoreCase(f.getName())) { return false; } if (method.getName().equalsIgnoreCase("get" + f.getName())) { return false; } if (method.getName().equalsIgnoreCase("set" + f.getName())) { return false; } } return true; }
From source file:Base64.java
/** * Draws a line on the screen at the specified index. Default is green. * <p/>//from w ww. ja va2s. com * Available colours: red, green, cyan, purple, white. * * @param render The Graphics object to be used. * @param row The index where you want the text. * @param text The text you want to render. Colours can be set like [red]. */ public static void drawLine(Graphics render, int row, String text) { FontMetrics metrics = render.getFontMetrics(); int height = metrics.getHeight() + 4; // height + gap int y = row * height + 15 + 19; String[] texts = text.split("\\["); int xIdx = 7; Color cur = Color.GREEN; for (String t : texts) { for (@SuppressWarnings("unused") String element : COLOURS_STR) { // String element = COLOURS_STR[i]; // Don't search for a starting '[' cause it they don't exists. // we split on that. int endIdx = t.indexOf(']'); if (endIdx != -1) { String colorName = t.substring(0, endIdx); if (COLOR_MAP.containsKey(colorName)) { cur = COLOR_MAP.get(colorName); } else { try { Field f = Color.class.getField(colorName); int mods = f.getModifiers(); if (Modifier.isPublic(mods) && Modifier.isStatic(mods) && Modifier.isFinal(mods)) { cur = (Color) f.get(null); COLOR_MAP.put(colorName, cur); } } catch (Exception ignored) { } } t = t.replace(colorName + "]", ""); } } render.setColor(Color.BLACK); render.drawString(t, xIdx, y + 1); render.setColor(cur); render.drawString(t, xIdx, y); xIdx += metrics.stringWidth(t); } }
From source file:org.hfoss.posit.android.sync.Communicator.java
/** * Returns a list on name/value pairs for the Find. Should work for Plugin Finds as * well as Basic Finds. // ww w .jav a 2 s .com * @param find * @param clazz * @return */ private static List<NameValuePair> getNameValuePairs(Find find, Class clazz) { Field[] fields = clazz.getDeclaredFields(); List<NameValuePair> nvp = new ArrayList<NameValuePair>(); String methodName = ""; String value = ""; for (Field field : fields) { // Log.i(TAG, "class= " + clazz + " field = " + field); if (!Modifier.isFinal(field.getModifiers())) { String key = field.getName(); methodName = "get" + key.substring(0, 1).toUpperCase() + key.substring(1); value = ""; try { Class returnType = clazz.getDeclaredMethod(methodName, null).getReturnType(); if (returnType.equals(String.class)) value = (String) clazz.getDeclaredMethod(methodName, null).invoke(find, (Object[]) null); else if (returnType.equals(int.class)) value = String.valueOf( (Integer) clazz.getDeclaredMethod(methodName, null).invoke(find, (Object[]) null)); else if (returnType.equals(double.class)) value = String.valueOf( (Double) clazz.getDeclaredMethod(methodName, null).invoke(find, (Object[]) null)); else if (returnType.equals(boolean.class)) value = String.valueOf( (Boolean) clazz.getDeclaredMethod(methodName, null).invoke(find, (Object[]) null)); } catch (IllegalArgumentException e) { Log.e(TAG, e + ": " + e.getMessage()); } catch (SecurityException e) { Log.e(TAG, e + ": " + e.getMessage()); } catch (IllegalAccessException e) { Log.e(TAG, e + ": " + e.getMessage()); } catch (InvocationTargetException e) { Log.e(TAG, e + ": " + e.getMessage()); } catch (NoSuchMethodException e) { Log.e(TAG, e + ": " + e.getMessage()); } nvp.add(new BasicNameValuePair(key, value)); } } return nvp; }
From source file:ca.oson.json.Oson.java
private boolean ignoreModifiers(int modifiers, Set<MODIFIER> includeFieldsWithModifiers) { //Set<MODIFIER> includeFieldsWithModifiers = getIncludeFieldsWithModifiers(); if (includeFieldsWithModifiers == null || includeFieldsWithModifiers.size() == 0) { // by default, transient and volatile are ignored // unless you specify otherwise, by using MODIFIER.Transient enum, or all if (Modifier.isTransient(modifiers)) { return true; }//from w w w. j a v a 2 s .c om if (Modifier.isVolatile(modifiers)) { return true; } return false; } if (includeFieldsWithModifiers.contains(MODIFIER.All)) { return false; } for (MODIFIER modifier : includeFieldsWithModifiers) { switch (modifier) { case Abstract: if (Modifier.isAbstract(modifiers)) { return false; } break; case Final: if (Modifier.isFinal(modifiers)) { return false; } break; case Interface: if (Modifier.isInterface(modifiers)) { return false; } break; case Native: if (Modifier.isNative(modifiers)) { return false; } break; case Private: if (Modifier.isPrivate(modifiers)) { return false; } break; case Protected: if (Modifier.isProtected(modifiers)) { return false; } break; case Public: if (Modifier.isPublic(modifiers)) { return false; } break; case Package: if (ObjectUtil.isPackage(modifiers)) { return false; } break; case Static: if (Modifier.isStatic(modifiers)) { return false; } break; case Strict: if (Modifier.isStrict(modifiers)) { return false; } break; case Synchronized: if (Modifier.isSynchronized(modifiers)) { return false; } break; case Transient: if (Modifier.isTransient(modifiers)) { return false; } break; case Volatile: if (Modifier.isVolatile(modifiers)) { return false; } break; } } return true; }
From source file:org.apache.airavata.sharing.registry.server.SharingRegistryServerHandler.java
private <T> T getUpdatedObject(T oldEntity, T newEntity) throws SharingRegistryException { Field[] newEntityFields = newEntity.getClass().getDeclaredFields(); Hashtable newHT = fieldsToHT(newEntityFields, newEntity); Class oldEntityClass = oldEntity.getClass(); Field[] oldEntityFields = oldEntityClass.getDeclaredFields(); for (Field field : oldEntityFields) { if (!Modifier.isFinal(field.getModifiers())) { field.setAccessible(true);//from www . j a v a 2s .co m Object o = newHT.get(field.getName()); if (o != null) { Field f = null; try { f = oldEntityClass.getDeclaredField(field.getName()); f.setAccessible(true); logger.debug("setting " + f.getName()); f.set(oldEntity, o); } catch (Exception e) { throw new SharingRegistryException(e.getMessage()); } } } } return oldEntity; }
From source file:org.apache.openjpa.util.ProxyManagerImpl.java
/** * Proxy setter methods of the given type. * //from ww w. j a v a2s . c o m * @return true if we find any setters, false otherwise */ private boolean proxySetters(BCClass bc, Class type) { Method[] meths = type.getMethods(); int setters = 0; for (int i = 0; i < meths.length; i++) { if (isSetter(meths[i]) && !Modifier.isFinal(meths[i].getModifiers()) && bc.getDeclaredMethod(meths[i].getName(), meths[i].getParameterTypes()) == null) { setters++; proxySetter(bc, type, meths[i]); } } return setters > 0; }
From source file:fr.certu.chouette.command.Command.java
/** * @param itemType/*from w w w.j ava 2s.co m*/ * @param indent * @throws Exception */ private void printFieldDetails(Class<?> itemType, String indent) throws Exception { String itemName = itemType.getName(); if (itemName.startsWith("fr.certu.chouette.model.neptune.type.")) { if (itemName.endsWith("Enum")) { Field[] fields = itemType.getDeclaredFields(); System.out.print(indent + " "); String text = ""; for (Field field : fields) { int m = field.getModifiers(); if (Modifier.isPublic(m) && Modifier.isStatic(m) && Modifier.isFinal(m)) { Object instance = field.get(null); String name = instance.toString(); if (text.length() + name.length() > 79) { System.out.print(text + "\n" + indent + " "); text = ""; } text += name + " "; } } System.out.println(text); } else { Object instance = itemType.newInstance(); printFields(instance, indent + " "); } } else if (itemName.startsWith("fr.certu.chouette.model.neptune.")) { Object instance = itemType.newInstance(); if (instance instanceof NeptuneIdentifiedObject) { String simpleName = itemType.getSimpleName(); if (simpleName.equals("AreaCentroid")) { printFields(instance, indent + " "); } } else { printFields(instance, indent + " "); } } }
From source file:org.evosuite.setup.TestClusterGenerator.java
private boolean addDependencyClass(GenericClass clazz, int recursionLevel) { if (recursionLevel > Properties.CLUSTER_RECURSION) { logger.debug("Maximum recursion level reached, not adding dependency {}", clazz.getClassName()); return false; }/*from w w w . j a v a 2 s . c o m*/ clazz = clazz.getRawGenericClass(); if (analyzedClasses.contains(clazz.getRawClass())) { return true; } analyzedClasses.add(clazz.getRawClass()); // We keep track of generic containers in case we find other concrete generic components during runtime if (clazz.isAssignableTo(Collection.class) || clazz.isAssignableTo(Map.class)) { if (clazz.getNumParameters() > 0) { containerClasses.add(clazz.getRawClass()); } } if (clazz.equals(String.class)) { return false; } try { TestCluster cluster = TestCluster.getInstance(); logger.debug("Adding dependency class {}", clazz.getClassName()); // TODO: Should we include declared classes as well? if (!canUse(clazz.getRawClass())) { logger.info("*** Cannot use class: {}", clazz.getClassName()); return false; } // Add all constructors for (Constructor<?> constructor : getConstructors(clazz.getRawClass())) { String name = "<init>" + org.objectweb.asm.Type.getConstructorDescriptor(constructor); if (Properties.TT) { String orig = name; name = BooleanTestabilityTransformation.getOriginalNameDesc(clazz.getClassName(), "<init>", org.objectweb.asm.Type.getConstructorDescriptor(constructor)); if (!orig.equals(name)) logger.info("TT name: {} -> {}", orig, name); } if (canUse(constructor)) { GenericConstructor genericConstructor = new GenericConstructor(constructor, clazz); try { cluster.addGenerator(clazz.getWithWildcardTypes(), genericConstructor); addDependencies(genericConstructor, recursionLevel + 1); logger.debug("Keeping track of {}.{}{}", constructor.getDeclaringClass().getName(), constructor.getName(), Type.getConstructorDescriptor(constructor)); } catch (Throwable t) { logger.info("Error adding constructor {}: {}", constructor.getName(), t.getMessage()); } } else { logger.debug("Constructor cannot be used: {}", constructor); } } // Add all methods for (Method method : getMethods(clazz.getRawClass())) { String name = method.getName() + org.objectweb.asm.Type.getMethodDescriptor(method); if (Properties.TT) { String orig = name; name = BooleanTestabilityTransformation.getOriginalNameDesc(clazz.getClassName(), method.getName(), org.objectweb.asm.Type.getMethodDescriptor(method)); if (!orig.equals(name)) logger.info("TT name: {} -> {}", orig, name); } if (canUse(method, clazz.getRawClass()) && !method.getName().equals("hashCode")) { logger.debug("Adding method {}.{}{}", clazz.getClassName(), method.getName(), Type.getMethodDescriptor(method)); if (method.getTypeParameters().length > 0) { logger.info("Type parameters in methods are not handled yet, skipping {}", method); continue; } GenericMethod genericMethod = new GenericMethod(method, clazz); try { addDependencies(genericMethod, recursionLevel + 1); cluster.addModifier(clazz.getWithWildcardTypes(), genericMethod); // GenericClass retClass = new GenericClass( // genericMethod.getReturnType(), method.getReturnType()); GenericClass retClass = new GenericClass(method.getReturnType()); if (!retClass.isPrimitive() && !retClass.isVoid() && !retClass.isObject()) { cluster.addGenerator(retClass.getWithWildcardTypes(), genericMethod); } } catch (Throwable t) { logger.info("Error adding method {}: {}", method.getName(), t.getMessage()); } } else { logger.debug("Method cannot be used: {}", method); } } // Add all fields for (Field field : getFields(clazz.getRawClass())) { logger.debug("Checking field {}", field); if (canUse(field, clazz.getRawClass())) { logger.debug("Adding field {} for class {}", field, clazz); try { GenericField genericField = new GenericField(field, clazz); cluster.addGenerator(new GenericClass(field.getGenericType()).getWithWildcardTypes(), genericField); if (!Modifier.isFinal(field.getModifiers())) { cluster.addModifier(clazz.getWithWildcardTypes(), genericField); addDependencies(genericField, recursionLevel + 1); } } catch (Throwable t) { logger.info("Error adding field {}: {}", field.getName(), t.getMessage()); } } else { logger.debug("Field cannot be used: {}", field); } } logger.info("Finished analyzing {} at recursion level {}", clazz.getTypeName(), recursionLevel); cluster.getAnalyzedClasses().add(clazz.getRawClass()); } catch (Throwable t) { /* * NOTE: this is a problem we know it can happen in some cases in SF110, but don't * have a real solution now. As it is bound to happen, we try to minimize the logging (eg no * stack trace), although we still need to log it */ logger.error("Problem for {}. Failed to add dependencies for class {}: {}\n{}", Properties.TARGET_CLASS, clazz.getClassName(), t, Arrays.asList(t.getStackTrace())); return false; } return true; }
From source file:org.pkcs11.jacknji11.C.java
/** * Helper method. Adds all public static final long fields in c to map, mapping field value to name. * @param c class//from w ww.ja v a 2s .c om * @return map of field value:name */ public static Map<Long, String> createL2SMap(Class<?> c) { Map<Long, String> map = new HashMap<Long, String>(); try { for (Field f : c.getDeclaredFields()) { // only put 'public static final long' in map if (f.getType() == long.class && Modifier.isPublic(f.getModifiers()) && Modifier.isStatic(f.getModifiers()) && Modifier.isFinal(f.getModifiers())) { map.put(f.getLong(null), f.getName()); } } } catch (Exception e) { throw new RuntimeException(e); } return map; }
From source file:com.fluidops.iwb.api.ReadDataManagerImpl.java
/** * RDF string to Java Object/* www . ja v a2 s.com*/ */ @SuppressWarnings({ "unchecked" }) @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "REC_CATCH_EXCEPTION", justification = "Exception caught for robustness.") private <T> T convert(Value s, Class<T> t, Map<Value, Object> resourceStack) { if (s == null) return null; // avoid endless recursion if (resourceStack != null) { Object alreadySeen = resourceStack.get(s); if (alreadySeen != null) { if (alreadySeen.getClass().isAssignableFrom(t)) return (T) alreadySeen; else // TODO: theoretically, a subject could be accessed as Type // T1 and later // as Type T2. So really, the stack should be Map<String, // Map<Class, Object>> // but this seems like a bit of overkill, so we just return // null if the type // seen for this resource before in the stack does not match return null; } } try { if (t == short.class || t == Short.class) return (T) new Short(s.stringValue()); if (t == byte.class || t == Byte.class) return (T) new Byte(s.stringValue()); if (t == boolean.class || t == Boolean.class) return (T) Boolean.valueOf(s.stringValue()); if (t == long.class || t == Long.class) return (T) new Long(s.stringValue()); if (t == float.class || t == Float.class) return (T) new Float(s.stringValue()); if (t == double.class || t == Double.class) return (T) new Double(s.stringValue()); if (t == int.class || t == Integer.class) return (T) new Integer(s.stringValue()); if (t == String.class) return (T) s.stringValue(); if (t == Date.class) return (T) literal2HumanDate(s.stringValue()); if (t == Calendar.class) { GregorianCalendar cal = new GregorianCalendar(); cal.setTime(literal2HumanDate(s.stringValue())); return (T) cal; } if (t == URL.class) return (T) new URL(s.stringValue()); if (t == java.net.URI.class) return (T) new java.net.URI(s.stringValue()); if (t == org.openrdf.model.URI.class) return (T) s; /* * todo // interface - use dynamic proxy if ( t.isInterface() || * Proxy.class.isAssignableFrom( t ) ) return (T)Proxy.newInstance( * s, this, t ); */ T instance = t.newInstance(); // create object only if it is necessary if (resourceStack == null) resourceStack = new HashMap<Value, Object>(); resourceStack.put(s, instance); for (Field f : t.getFields()) { if (Modifier.isStatic(f.getModifiers())) continue; if (Modifier.isFinal(f.getModifiers())) continue; if (List.class.isAssignableFrom(f.getType())) { @SuppressWarnings("rawtypes") Class listTypeValue = String.class; if (f.getGenericType() instanceof ParameterizedType) listTypeValue = (Class<?>) ((ParameterizedType) f.getGenericType()) .getActualTypeArguments()[0]; if (f.getAnnotation(RDFMapping.class) == null ? false : f.getAnnotation(RDFMapping.class).inverse()) { List<String> x = getInverseProps(s, RDFMappingUtil.uri(f), listTypeValue, false); f.set(instance, x); } else { List<T> x = new ArrayList<T>(); for (Value v : getProps((Resource) s, RDFMappingUtil.uri(f))) x.add((T) convert(v, listTypeValue, resourceStack)); f.set(instance, x); } } else { if (f.getName().equals("__resource")) f.set(instance, s); else if (f.getAnnotation(RDFMapping.class) == null ? false : f.getAnnotation(RDFMapping.class).inverse()) { Object x = getInversePropInternal(s, RDFMappingUtil.uri(f), f.getType(), resourceStack); f.set(instance, x); } else if (s instanceof Resource) { // for Resources, traverse deeper, for Literals, there // is no substructure Object x = getPropInternal(getProp((Resource) s, RDFMappingUtil.uri(f)), f.getType(), resourceStack); f.set(instance, x); } } } return instance; } catch (Exception e) { throw new RuntimeException(e); } }