List of usage examples for java.lang.reflect Modifier isPublic
public static boolean isPublic(int mod)
From source file:com.parse.ParseObject.java
private static boolean isAccessible(Member m) { return Modifier.isPublic(m.getModifiers()) || (m.getDeclaringClass().getPackage().getName().equals("com.parse") && !Modifier.isPrivate(m.getModifiers()) && !Modifier.isProtected(m.getModifiers())); }
From source file:com.almende.eve.rpc.jsonrpc.JSONRPC.java
/** * Check whether a method is available for JSON-RPC calls. This is the case * when it is public, has named parameters, and has a public or private @Access * annotation/*from ww w. ja v a 2s . co m*/ * * @param method * the method * @param destination * the destination * @param requestParams * the request params * @param auth * the auth * @return available */ private static boolean isAvailable(final AnnotatedMethod method, final Object destination, final RequestParams requestParams, final JSONAuthorizor auth) { if (method == null) { return false; } Access methodAccess = method.getAnnotation(Access.class); if (destination != null && !method.getActualMethod().getDeclaringClass().isAssignableFrom(destination.getClass())) { return false; } final int mod = method.getActualMethod().getModifiers(); if (!(Modifier.isPublic(mod) && hasNamedParams(method, requestParams))) { return false; } final Access classAccess = AnnotationUtil .get(destination != null ? destination.getClass() : method.getActualMethod().getDeclaringClass()) .getAnnotation(Access.class); if (methodAccess == null) { methodAccess = classAccess; } if (methodAccess == null) { // New default: UNAVAILABLE! return false; } if (methodAccess.value() == AccessType.UNAVAILABLE) { return false; } if (methodAccess.value() == AccessType.PRIVATE) { return auth != null ? auth.onAccess(requestParams.get(Sender.class).toString(), methodAccess.tag()) : false; } if (methodAccess.value() == AccessType.SELF) { return auth != null ? auth.isSelf(requestParams.get(Sender.class).toString()) : false; } return true; }
From source file:org.jboss.dashboard.factory.Component.java
protected Field getField(Object obj, String propertyName) { try {/* www . j a va2 s . c o m*/ Field field = obj.getClass().getField(propertyName); int modifiers = field.getModifiers(); if (Modifier.isPublic(modifiers) && !Modifier.isFinal(field.getModifiers())) { return field; } } catch (NoSuchFieldException e) { } if (log.isDebugEnabled()) { log.debug("Could not find a field for property " + propertyName + " in " + obj.getClass()); } return null; }
From source file:org.ebayopensource.turmeric.tools.errorlibrary.ErrorLibraryGeneratorTest.java
@Test public void validateContentOfErrorDataCollections() throws Exception { String errorDataCOllectionClassName = "org.ebayopensource.turmeric.test.errorlibrary.turmericruntime.ErrorDataCollection"; String sampleErrorName = "svc_factory_custom_ser_no_bound_type"; // Initialize testing paths MavenTestingUtils.ensureEmpty(testingdir); File rootDir = TestResourceUtil.copyResourceRootDir("errorLibrary/TestErrorLibrary", testingdir); File binDir = new File(rootDir, "bin"); File gensrcDir = new File(rootDir, "gen-src"); MavenTestingUtils.ensureDirExists(binDir); MavenTestingUtils.ensureDirExists(gensrcDir); // @formatter:off String pluginParameters[] = { "-gentype", "genTypeDataCollection", "-pr", rootDir.getAbsolutePath(), "-domain", "TurmericRuntime", "-errorlibname", "TestErrorLibrary" }; // @formatter:on performDirectCodeGen(pluginParameters); Class<?> errDataCollection = compileGeneratedFile(errorDataCOllectionClassName, gensrcDir, binDir); Assert.assertThat("errDataCollection", errDataCollection, notNullValue()); Assert.assertThat(errDataCollection.getName(), is(errorDataCOllectionClassName)); Field member = errDataCollection.getField(sampleErrorName); Assert.assertThat("member", member, notNullValue()); Assert.assertThat("member.type", member.getType().getName(), is(CommonErrorData.class.getName())); Assert.assertThat("member.isFinal", Modifier.isFinal(member.getModifiers()), is(true)); Assert.assertThat("member.isPublic", Modifier.isPublic(member.getModifiers()), is(true)); Assert.assertThat("member.isStatic", Modifier.isStatic(member.getModifiers()), is(true)); CommonErrorData edata = (CommonErrorData) member.get(null); Assert.assertThat("CommonErrorData", edata, notNullValue()); Assert.assertThat("CommonErrorData.category", edata.getCategory(), is(ErrorCategory.SYSTEM)); Assert.assertThat("CommonErrorData.severity", edata.getSeverity(), is(ErrorSeverity.ERROR)); Assert.assertThat("CommonErrorData.subdommain", edata.getSubdomain(), is("Config")); }
From source file:com.impetus.kundera.utils.KunderaCoreUtils.java
/** * @param clazz//from www .j ava2 s. c o m * @return */ public static Object createNewInstance(Class clazz) { Object target = null; try { Constructor[] constructors = clazz.getDeclaredConstructors(); for (Constructor constructor : constructors) { if ((Modifier.isProtected(constructor.getModifiers()) || Modifier.isPublic(constructor.getModifiers())) && constructor.getParameterTypes().length == 0) { constructor.setAccessible(true); target = constructor.newInstance(); constructor.setAccessible(false); break; } } return target; } catch (InstantiationException iex) { logger.error("Error while creating an instance of {} .", clazz); throw new PersistenceException(iex); } catch (IllegalAccessException iaex) { logger.error("Illegal Access while reading data from {}, Caused by: .", clazz, iaex); throw new PersistenceException(iaex); } catch (Exception e) { logger.error("Error while creating an instance of {}, Caused by: .", clazz, e); throw new PersistenceException(e); } }
From source file:org.jenkinsci.plugins.ghprb.GhprbTestUtil.java
public static List<String> checkClassForGetters(Class<?> clazz) { Field[] fields = clazz.getDeclaredFields(); List<Field> xmlFields = new ArrayList<Field>(); List<String> errors = new ArrayList<String>(); for (Field field : fields) { int modifiers = field.getModifiers(); if (modifiers == (Modifier.PRIVATE) || modifiers == (Modifier.PRIVATE | Modifier.FINAL)) { xmlFields.add(field);//from w w w.jav a2s. c om } } for (Field field : xmlFields) { String getter = "get" + StringUtils.capitalize(field.getName()); try { Method method = clazz.getDeclaredMethod(getter); int modifier = method.getModifiers(); if (!Modifier.isPublic(modifier)) { errors.add(getter + " is not a public method"); } } catch (Exception e) { String wrongGetter = "is" + StringUtils.capitalize(field.getName()); try { clazz.getDeclaredMethod(wrongGetter); errors.add("Setter is using the wrong name, is " + wrongGetter + " and should be " + getter); } catch (Exception err) { errors.add("Missing " + getter); } } } return errors; }
From source file:org.apache.openjpa.util.ProxyManagerImpl.java
private static boolean isProxyable(Class<?> cls) { int mod = cls.getModifiers(); if (Modifier.isFinal(mod)) return false; if (Modifier.isProtected(mod) || Modifier.isPublic(mod)) return true; // Default scoped class, we can only extend if it is in the same package as the generated proxy. Ideally // we'd fix the code gen portion and place proxies in the same pacakge as the types being proxied. if (cls.getPackage().getName().equals("org.apache.openjpa.util")) return true; return false; }
From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java
public static boolean isPublic(Constructor<?> constructor) { return Modifier.isPublic(constructor.getModifiers()); }
From source file:com.alibaba.fastjson.parser.ParserConfig.java
public ObjectDeserializer createJavaBeanDeserializer(Class<?> clazz, Type type) { boolean asmEnable = this.asmEnable & !this.fieldBased; if (asmEnable) { JSONType jsonType = TypeUtils.getAnnotation(clazz, JSONType.class); if (jsonType != null) { Class<?> deserializerClass = jsonType.deserializer(); if (deserializerClass != Void.class) { try { Object deseralizer = deserializerClass.newInstance(); if (deseralizer instanceof ObjectDeserializer) { return (ObjectDeserializer) deseralizer; }//from w ww. jav a2 s .c o m } catch (Throwable e) { // skip } } asmEnable = jsonType.asm(); } if (asmEnable) { Class<?> superClass = JavaBeanInfo.getBuilderClass(clazz, jsonType); if (superClass == null) { superClass = clazz; } for (;;) { if (!Modifier.isPublic(superClass.getModifiers())) { asmEnable = false; break; } superClass = superClass.getSuperclass(); if (superClass == Object.class || superClass == null) { break; } } } } if (clazz.getTypeParameters().length != 0) { asmEnable = false; } if (asmEnable && asmFactory != null && asmFactory.classLoader.isExternalClass(clazz)) { asmEnable = false; } if (asmEnable) { asmEnable = ASMUtils.checkName(clazz.getSimpleName()); } if (asmEnable) { if (clazz.isInterface()) { asmEnable = false; } JavaBeanInfo beanInfo = JavaBeanInfo.build(clazz, type, propertyNamingStrategy); if (asmEnable && beanInfo.fields.length > 200) { asmEnable = false; } Constructor<?> defaultConstructor = beanInfo.defaultConstructor; if (asmEnable && defaultConstructor == null && !clazz.isInterface()) { asmEnable = false; } for (FieldInfo fieldInfo : beanInfo.fields) { if (fieldInfo.getOnly) { asmEnable = false; break; } Class<?> fieldClass = fieldInfo.fieldClass; if (!Modifier.isPublic(fieldClass.getModifiers())) { asmEnable = false; break; } if (fieldClass.isMemberClass() && !Modifier.isStatic(fieldClass.getModifiers())) { asmEnable = false; break; } if (fieldInfo.getMember() != null // && !ASMUtils.checkName(fieldInfo.getMember().getName())) { asmEnable = false; break; } JSONField annotation = fieldInfo.getAnnotation(); if (annotation != null // && ((!ASMUtils.checkName(annotation.name())) // || annotation.format().length() != 0 // || annotation.deserializeUsing() != Void.class // || annotation.unwrapped()) || (fieldInfo.method != null && fieldInfo.method.getParameterTypes().length > 1)) { asmEnable = false; break; } if (fieldClass.isEnum()) { // EnumDeserializer ObjectDeserializer fieldDeser = this.getDeserializer(fieldClass); if (!(fieldDeser instanceof EnumDeserializer)) { asmEnable = false; break; } } } } if (asmEnable) { if (clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers())) { asmEnable = false; } } if (!asmEnable) { return new JavaBeanDeserializer(this, clazz, type); } JavaBeanInfo beanInfo = JavaBeanInfo.build(clazz, type, propertyNamingStrategy); try { return asmFactory.createJavaBeanDeserializer(this, beanInfo); // } catch (VerifyError e) { // e.printStackTrace(); // return new JavaBeanDeserializer(this, clazz, type); } catch (NoSuchMethodException ex) { return new JavaBeanDeserializer(this, clazz, type); } catch (JSONException asmError) { return new JavaBeanDeserializer(this, beanInfo); } catch (Exception e) { throw new JSONException("create asm deserializer error, " + clazz.getName(), e); } }
From source file:com.android.camera2.its.ItsSerializer.java
@SuppressWarnings("unchecked") public static CaptureRequest.Builder deserialize(CaptureRequest.Builder mdDefault, JSONObject jsonReq) throws ItsException { try {// w w w . j av a 2s .c o m Logt.i(TAG, "Parsing JSON capture request ..."); // Iterate over the CaptureRequest reflected fields. CaptureRequest.Builder md = mdDefault; Field[] allFields = CaptureRequest.class.getDeclaredFields(); for (Field field : allFields) { if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers()) && field.getType() == CaptureRequest.Key.class && field.getGenericType() instanceof ParameterizedType) { ParameterizedType paramType = (ParameterizedType) field.getGenericType(); Type[] argTypes = paramType.getActualTypeArguments(); if (argTypes.length > 0) { CaptureRequest.Key key = (CaptureRequest.Key) field.get(md); String keyName = key.getName(); Type keyType = argTypes[0]; // For each reflected CaptureRequest entry, look inside the JSON object // to see if it is being set. If it is found, remove the key from the // JSON object. After this process, there should be no keys left in the // JSON (otherwise an invalid key was specified). if (jsonReq.has(keyName) && !jsonReq.isNull(keyName)) { if (keyType instanceof GenericArrayType) { Type elmtType = ((GenericArrayType) keyType).getGenericComponentType(); JSONArray ja = jsonReq.getJSONArray(keyName); Object val[] = new Object[ja.length()]; for (int i = 0; i < ja.length(); i++) { if (elmtType == int.class) { Array.set(val, i, ja.getInt(i)); } else if (elmtType == byte.class) { Array.set(val, i, (byte) ja.getInt(i)); } else if (elmtType == float.class) { Array.set(val, i, (float) ja.getDouble(i)); } else if (elmtType == long.class) { Array.set(val, i, ja.getLong(i)); } else if (elmtType == double.class) { Array.set(val, i, ja.getDouble(i)); } else if (elmtType == boolean.class) { Array.set(val, i, ja.getBoolean(i)); } else if (elmtType == String.class) { Array.set(val, i, ja.getString(i)); } else if (elmtType == Size.class) { JSONObject obj = ja.getJSONObject(i); Array.set(val, i, new Size(obj.getInt("width"), obj.getInt("height"))); } else if (elmtType == Rect.class) { JSONObject obj = ja.getJSONObject(i); Array.set(val, i, new Rect(obj.getInt("left"), obj.getInt("top"), obj.getInt("bottom"), obj.getInt("right"))); } else if (elmtType == Rational.class) { JSONObject obj = ja.getJSONObject(i); Array.set(val, i, new Rational(obj.getInt("numerator"), obj.getInt("denominator"))); } else if (elmtType == RggbChannelVector.class) { JSONArray arr = ja.getJSONArray(i); Array.set(val, i, new RggbChannelVector((float) arr.getDouble(0), (float) arr.getDouble(1), (float) arr.getDouble(2), (float) arr.getDouble(3))); } else if (elmtType == ColorSpaceTransform.class) { JSONArray arr = ja.getJSONArray(i); Rational xform[] = new Rational[9]; for (int j = 0; j < 9; j++) { xform[j] = new Rational(arr.getJSONObject(j).getInt("numerator"), arr.getJSONObject(j).getInt("denominator")); } Array.set(val, i, new ColorSpaceTransform(xform)); } else if (elmtType == MeteringRectangle.class) { JSONObject obj = ja.getJSONObject(i); Array.set(val, i, new MeteringRectangle(obj.getInt("x"), obj.getInt("y"), obj.getInt("width"), obj.getInt("height"), obj.getInt("weight"))); } else { throw new ItsException("Failed to parse key from JSON: " + keyName); } } if (val != null) { Logt.i(TAG, "Set: " + keyName + " -> " + Arrays.toString(val)); md.set(key, val); jsonReq.remove(keyName); } } else { Object val = null; if (keyType == Integer.class) { val = jsonReq.getInt(keyName); } else if (keyType == Byte.class) { val = (byte) jsonReq.getInt(keyName); } else if (keyType == Double.class) { val = jsonReq.getDouble(keyName); } else if (keyType == Long.class) { val = jsonReq.getLong(keyName); } else if (keyType == Float.class) { val = (float) jsonReq.getDouble(keyName); } else if (keyType == Boolean.class) { val = jsonReq.getBoolean(keyName); } else if (keyType == String.class) { val = jsonReq.getString(keyName); } else if (keyType == Size.class) { JSONObject obj = jsonReq.getJSONObject(keyName); val = new Size(obj.getInt("width"), obj.getInt("height")); } else if (keyType == Rect.class) { JSONObject obj = jsonReq.getJSONObject(keyName); val = new Rect(obj.getInt("left"), obj.getInt("top"), obj.getInt("right"), obj.getInt("bottom")); } else if (keyType == Rational.class) { JSONObject obj = jsonReq.getJSONObject(keyName); val = new Rational(obj.getInt("numerator"), obj.getInt("denominator")); } else if (keyType == RggbChannelVector.class) { JSONObject obj = jsonReq.optJSONObject(keyName); JSONArray arr = jsonReq.optJSONArray(keyName); if (arr != null) { val = new RggbChannelVector((float) arr.getDouble(0), (float) arr.getDouble(1), (float) arr.getDouble(2), (float) arr.getDouble(3)); } else if (obj != null) { val = new RggbChannelVector((float) obj.getDouble("red"), (float) obj.getDouble("greenEven"), (float) obj.getDouble("greenOdd"), (float) obj.getDouble("blue")); } else { throw new ItsException("Invalid RggbChannelVector object"); } } else if (keyType == ColorSpaceTransform.class) { JSONArray arr = jsonReq.getJSONArray(keyName); Rational a[] = new Rational[9]; for (int i = 0; i < 9; i++) { a[i] = new Rational(arr.getJSONObject(i).getInt("numerator"), arr.getJSONObject(i).getInt("denominator")); } val = new ColorSpaceTransform(a); } else if (keyType instanceof ParameterizedType && ((ParameterizedType) keyType).getRawType() == Range.class && ((ParameterizedType) keyType).getActualTypeArguments().length == 1 && ((ParameterizedType) keyType) .getActualTypeArguments()[0] == Integer.class) { JSONArray arr = jsonReq.getJSONArray(keyName); val = new Range<Integer>(arr.getInt(0), arr.getInt(1)); } else { throw new ItsException( "Failed to parse key from JSON: " + keyName + ", " + keyType); } if (val != null) { Logt.i(TAG, "Set: " + keyName + " -> " + val); md.set(key, val); jsonReq.remove(keyName); } } } } } } // Ensure that there were no invalid keys in the JSON request object. if (jsonReq.length() != 0) { throw new ItsException("Invalid JSON key(s): " + jsonReq.toString()); } Logt.i(TAG, "Parsing JSON capture request completed"); return md; } catch (java.lang.IllegalAccessException e) { throw new ItsException("Access error: ", e); } catch (org.json.JSONException e) { throw new ItsException("JSON error: ", e); } }