List of usage examples for java.lang.reflect Modifier isPrivate
public static boolean isPrivate(int mod)
From source file:com.asuka.android.asukaandroid.view.ViewInjectorImpl.java
@SuppressWarnings("ConstantConditions") private static void injectObject(Object handler, Class<?> handlerType, ViewFinder finder) { if (handlerType == null || IGNORED.contains(handlerType)) { return;/*from w w w . ja va2 s. co m*/ } // ? injectObject(handler, handlerType.getSuperclass(), finder); // inject view Field[] fields = handlerType.getDeclaredFields(); if (fields != null && fields.length > 0) { for (Field field : fields) { Class<?> fieldType = field.getType(); if ( /* ??? */ Modifier.isStatic(field.getModifiers()) || /* ?final */ Modifier.isFinal(field.getModifiers()) || /* ? */ fieldType.isPrimitive() || /* ? */ fieldType.isArray()) { continue; } ViewInject viewInject = field.getAnnotation(ViewInject.class); if (viewInject != null) { try { View view = finder.findViewById(viewInject.value(), viewInject.parentId()); if (view != null) { field.setAccessible(true); field.set(handler, view); } else { throw new RuntimeException("Invalid @ViewInject for " + handlerType.getSimpleName() + "." + field.getName()); } } catch (Throwable ex) { LogUtil.e(ex.getMessage(), ex); } } } } // end inject view // inject event Method[] methods = handlerType.getDeclaredMethods(); if (methods != null && methods.length > 0) { for (Method method : methods) { if (Modifier.isStatic(method.getModifiers()) || !Modifier.isPrivate(method.getModifiers())) { continue; } //??event Event event = method.getAnnotation(Event.class); if (event != null) { try { // id? int[] values = event.value(); int[] parentIds = event.parentId(); int parentIdsLen = parentIds == null ? 0 : parentIds.length; //id?ViewInfo??? for (int i = 0; i < values.length; i++) { int value = values[i]; if (value > 0) { ViewInfo info = new ViewInfo(); info.value = value; info.parentId = parentIdsLen > i ? parentIds[i] : 0; method.setAccessible(true); EventListenerManager.addEventMethod(finder, info, event, handler, method); } } } catch (Throwable ex) { LogUtil.e(ex.getMessage(), ex); } } } } // end inject event }
From source file:com.geodevv.testing.irmina.IrminaContextLoader.java
private boolean isStaticNonPrivateAndNonFinal(Class<?> clazz) { Assert.notNull(clazz, "Class must not be null"); int modifiers = clazz.getModifiers(); return (Modifier.isStatic(modifiers) && !Modifier.isPrivate(modifiers) && !Modifier.isFinal(modifiers)); }
From source file:uk.gov.gchq.gaffer.schemabuilder.service.SchemaBuilderService.java
private static void keepPublicConcreteClasses(final Collection<Class> classes) { if (null != classes) { final Iterator<Class> itr = classes.iterator(); for (Class clazz = null; itr.hasNext(); clazz = itr.next()) { if (null != clazz) { final int modifiers = clazz.getModifiers(); if (Modifier.isAbstract(modifiers) || Modifier.isInterface(modifiers) || Modifier.isPrivate(modifiers) || Modifier.isProtected(modifiers)) { itr.remove();//from w ww.j av a2s . c om } } } } }
From source file:RevEngAPI.java
/** Generate a .java file for the outline of the given class. */ public void doClass(Class c) throws IOException { className = c.getName();/* w w w .ja v a 2 s . co m*/ // pre-compute offset for stripping package name classNameOffset = className.lastIndexOf('.') + 1; // Inner class if (className.indexOf('$') != -1) return; // get name, as String, with . changed to / String slashName = className.replace('.', '/'); String fileName = slashName + ".java"; System.out.println(className + " --> " + fileName); String dirName = slashName.substring(0, slashName.lastIndexOf("/")); new File(dirName).mkdirs(); // create the file. PrintWriter out = new PrintWriter(new FileWriter(fileName)); out.println("// Generated by RevEngAPI for class " + className); // If in a package, say so. Package pkg; if ((pkg = c.getPackage()) != null) { out.println("package " + pkg.getName() + ';'); out.println(); } // print class header int cMods = c.getModifiers(); printMods(cMods, out); out.print("class "); out.print(trim(c.getName())); out.print(' '); // XXX get superclass out.println('{'); // print constructors Constructor[] ctors = c.getDeclaredConstructors(); for (int i = 0; i < ctors.length; i++) { if (i == 0) { out.println(); out.println("\t// Constructors"); } Constructor cons = ctors[i]; int mods = cons.getModifiers(); if (Modifier.isPrivate(mods)) continue; out.print('\t'); printMods(mods, out); out.print(trim(cons.getName()) + "("); Class[] classes = cons.getParameterTypes(); for (int j = 0; j < classes.length; j++) { if (j > 0) out.print(", "); out.print(trim(classes[j].getName()) + ' ' + mkName(PREFIX_ARG, j)); } out.println(") {"); out.print("\t}"); } // print method names Method[] mems = c.getDeclaredMethods(); for (int i = 0; i < mems.length; i++) { if (i == 0) { out.println(); out.println("\t// Methods"); } Method m = mems[i]; if (m.getName().startsWith("access$")) continue; int mods = m.getModifiers(); if (Modifier.isPrivate(mods)) continue; out.print('\t'); printMods(mods, out); out.print(m.getReturnType()); out.print(' '); out.print(trim(m.getName()) + "("); Class[] classes = m.getParameterTypes(); for (int j = 0; j < classes.length; j++) { if (j > 0) out.print(", "); out.print(trim(classes[j].getName()) + ' ' + mkName(PREFIX_ARG, j)); } out.println(") {"); out.println("\treturn " + defaultValue(m.getReturnType()) + ';'); out.println("\t}"); } // print fields Field[] flds = c.getDeclaredFields(); for (int i = 0; i < flds.length; i++) { if (i == 0) { out.println(); out.println("\t// Fields"); } Field f = flds[i]; int mods = f.getModifiers(); if (Modifier.isPrivate(mods)) continue; out.print('\t'); printMods(mods, out); out.print(trim(f.getType().getName())); out.print(' '); out.print(f.getName()); if (Modifier.isFinal(mods)) { try { out.print(" = " + f.get(null)); } catch (IllegalAccessException ex) { out.print("; // " + ex.toString()); } } out.println(';'); } out.println("}"); //out.flush(); out.close(); }
From source file:gaffer.rest.service.SimpleGraphConfigurationService.java
private static void keepPublicConcreteClasses(final Collection<Class> classes) { if (null != classes) { final Iterator<Class> itr = classes.iterator(); while (itr.hasNext()) { final Class clazz = itr.next(); if (null != clazz) { final int modifiers = clazz.getModifiers(); if (Modifier.isAbstract(modifiers) || Modifier.isInterface(modifiers) || Modifier.isPrivate(modifiers) || Modifier.isProtected(modifiers)) { itr.remove();/*from www. j av a 2 s . c om*/ } } } } }
From source file:net.krotscheck.util.ResourceUtilTest.java
/** * Ensure the constructor is private.// ww w .j ava 2 s.c o m * * @throws Exception Tests throw exceptions. */ @Test public void testConstructorIsPrivate() throws Exception { Constructor<ResourceUtil> constructor = ResourceUtil.class.getDeclaredConstructor(); Assert.assertTrue(Modifier.isPrivate(constructor.getModifiers())); // Override the private constructor and create an instance constructor.setAccessible(true); ResourceUtil util = constructor.newInstance(); Assert.assertNotNull(util); }
From source file:org.evosuite.testcase.fm.MethodDescriptor.java
/** * For example, do not mock methods with no return value * * @return/* w ww.j av a 2 s. c om*/ */ public boolean shouldBeMocked() { int modifiers = method.getMethod().getModifiers(); if (method.getReturnType().equals(Void.TYPE) || method.getName().equals("equals") || method.getName().equals("hashCode") || Modifier.isPrivate(modifiers)) { return false; } if (Properties.hasTargetClassBeenLoaded()) { //null can happen in some unit tests if (!Modifier.isPublic(modifiers)) { assert !Modifier.isPrivate(modifiers); //previous checks String sutName = Properties.TARGET_CLASS; int lastIndexMethod = className.lastIndexOf('.'); int lastIndexSUT = sutName.lastIndexOf('.'); boolean samePackage; if (lastIndexMethod != lastIndexSUT) { samePackage = false; } else if (lastIndexMethod < 0) { samePackage = true; //default package } else { samePackage = className.substring(0, lastIndexMethod) .equals(sutName.substring(0, lastIndexSUT)); } if (!samePackage) { return false; } } } else { logger.warn("The target class should be loaded before invoking this method"); } return true; }
From source file:org.guzz.builder.JPA2AnnotationsBuilder.java
protected static void parseClassForAttributes(GuzzContextImpl gf, POJOBasedObjectMapping map, Business business, DBGroup dbGroup, SimpleTable st, Class domainClass) { //???//from w w w. j ava 2s . c om Class parentCls = domainClass.getSuperclass(); if (parentCls != null && parentCls.isAnnotationPresent(MappedSuperclass.class)) { parseClassForAttributes(gf, map, business, dbGroup, st, parentCls); } javax.persistence.Access access = (javax.persistence.Access) domainClass .getAnnotation(javax.persistence.Access.class); AccessType accessType = null; if (access == null) { //@Id@Idfieldproperty boolean hasColumnAOnField = false; boolean hasColumnAOnProperty = false; //detect from @Id, field first. Field[] fs = domainClass.getDeclaredFields(); for (Field f : fs) { if (f.isAnnotationPresent(Transient.class)) continue; if (f.isAnnotationPresent(javax.persistence.Id.class)) { accessType = AccessType.FIELD; break; } else if (f.isAnnotationPresent(javax.persistence.Column.class)) { hasColumnAOnField = true; } else if (f.isAnnotationPresent(org.guzz.annotations.Column.class)) { hasColumnAOnField = true; } } if (accessType == null) { Method[] ms = domainClass.getDeclaredMethods(); for (Method m : ms) { if (m.isAnnotationPresent(Transient.class)) continue; if (m.isAnnotationPresent(javax.persistence.Id.class)) { accessType = AccessType.PROPERTY; break; } else if (m.isAnnotationPresent(javax.persistence.Column.class)) { hasColumnAOnProperty = true; } else if (m.isAnnotationPresent(org.guzz.annotations.Column.class)) { hasColumnAOnProperty = true; } } } //@Id@Column@Columnfield? if (accessType == null) { if (hasColumnAOnField) { accessType = AccessType.FIELD; } else if (hasColumnAOnProperty) { accessType = AccessType.PROPERTY; } else { accessType = AccessType.FIELD; } } } else { accessType = access.value(); } //orm by field if (accessType == AccessType.FIELD) { Field[] fs = domainClass.getDeclaredFields(); for (Field f : fs) { if (f.isAnnotationPresent(Transient.class)) continue; if (Modifier.isTransient(f.getModifiers())) continue; if (Modifier.isStatic(f.getModifiers())) continue; if (f.isAnnotationPresent(javax.persistence.Id.class)) { addIdMapping(gf, map, st, dbGroup, f.getName(), domainClass, f); } else { addPropertyMapping(gf, map, st, f.getName(), f, f.getType()); } } } else { Method[] ms = domainClass.getDeclaredMethods(); for (Method m : ms) { if (m.isAnnotationPresent(Transient.class)) continue; if (Modifier.isTransient(m.getModifiers())) continue; if (Modifier.isStatic(m.getModifiers())) continue; if (Modifier.isPrivate(m.getModifiers())) continue; String methodName = m.getName(); String fieldName = null; if (m.getParameterTypes().length != 0) { continue; } else if (Void.TYPE.equals(m.getReturnType())) { continue; } if (methodName.startsWith("get")) { fieldName = methodName.substring(3); } else if (methodName.startsWith("is")) {//is boolean? Class retType = m.getReturnType(); if (boolean.class.isAssignableFrom(retType)) { fieldName = methodName.substring(2); } else if (Boolean.class.isAssignableFrom(retType)) { fieldName = methodName.substring(2); } } //not a javabean read method if (fieldName == null) { continue; } fieldName = java.beans.Introspector.decapitalize(fieldName); if (m.isAnnotationPresent(javax.persistence.Id.class)) { addIdMapping(gf, map, st, dbGroup, fieldName, domainClass, m); } else { addPropertyMapping(gf, map, st, fieldName, m, m.getReturnType()); } } } //?attribute override AttributeOverride gao = (AttributeOverride) domainClass.getAnnotation(AttributeOverride.class); AttributeOverrides gaos = (AttributeOverrides) domainClass.getAnnotation(AttributeOverrides.class); AttributeOverride[] aos = gao == null ? new AttributeOverride[0] : new AttributeOverride[] { gao }; if (gaos != null) { ArrayUtil.addToArray(aos, gaos.value()); } for (AttributeOverride ao : aos) { String name = ao.name(); Column col = ao.column(); TableColumn tc = st.getColumnByPropName(name); Assert.assertNotNull(tc, "@AttributeOverride cann't override a attribute that doesn't exist. The attribute is:" + name); //update is remove and add st.removeColumn(tc); //change the column name in the database. tc.setColName(col.name()); st.addColumn(tc); } }
From source file:org.grouplens.grapht.util.ClassProxy.java
/** * Check whether a member is injection-sensitive and should be checked for validity in * deserialization.//from w w w.j av a2 s .c om * * @param m The member. * @param <M> The type of member (done so we can check multiple types). * @return {@code true} if the member should be checksummed, {@code false} to ignore it. */ private static <M extends Member & AnnotatedElement> boolean isInjectionSensitive(M m) { // static methods are not sensitive if (Modifier.isStatic(m.getModifiers())) { return false; } // private members w/o @Inject are not sensitive if (Modifier.isPrivate(m.getModifiers()) && m.getAnnotation(Inject.class) == null) { return false; } // public, protected, or @Inject - it's sensitive (be conservative) return true; }
From source file:com.github.wnameless.jsonapi.JapisonTest.java
@Test public void testPrivateConstruct() throws Exception { Constructor<JsonApi> c = JsonApi.class.getDeclaredConstructor(); assertTrue(Modifier.isPrivate(c.getModifiers())); c.setAccessible(true);// w w w. j a v a 2s . com c.newInstance(); }