List of usage examples for java.lang.reflect Modifier isPublic
public static boolean isPublic(int mod)
From source file:edu.mit.media.funf.config.DefaultRuntimeTypeAdapterFactory.java
public static boolean isTypeInstatiable(Class<?> type) { int modifiers = type.getModifiers(); if (!(Modifier.isAbstract(modifiers) || Modifier.isInterface(modifiers))) { try {/* w w w. j a v a 2s .c o m*/ Constructor<?> noArgConstructor = type.getConstructor(); return Modifier.isPublic(noArgConstructor.getModifiers()); } catch (SecurityException e) { } catch (NoSuchMethodException e) { } } return false; }
From source file:org.jfree.data.KeyToGroupMap.java
/** * Attempts to clone the specified object using reflection. * * @param object the object (<code>null</code> permitted). * * @return The cloned object, or the original object if cloning failed. *///from w w w .j a v a 2s . co m private static Object clone(Object object) { if (object == null) { return null; } Class c = object.getClass(); Object result = null; try { Method m = c.getMethod("clone", (Class[]) null); if (Modifier.isPublic(m.getModifiers())) { try { result = m.invoke(object, (Object[]) null); } catch (Exception e) { e.printStackTrace(); } } } catch (NoSuchMethodException e) { result = object; } return result; }
From source file:com.igormaznitsa.upom.UPomModel.java
private static Method findMethod(final Class klazz, final String methodName, final boolean onlyPublic) { Method result = null;/*www.j a v a 2s .c om*/ for (final Method m : klazz.getMethods()) { if (onlyPublic && !Modifier.isPublic(m.getModifiers())) { continue; } if (m.getName().equalsIgnoreCase(methodName)) { result = m; break; } } return result; }
From source file:$.Reflections.java
/** * ?private/protected???public?????JDKSecurityManager *///from www . j av a2s.co m public static void makeAccessible(Field field) { if ((!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers()) || Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) { field.setAccessible(true); } }
From source file:com.laxser.blitz.web.impl.module.ControllerRef.java
private boolean quicklyPass(List<Method> pastMethods, Method method, Class<?> controllerClass) { // public, not static, not abstract, @Ignored if (!Modifier.isPublic(method.getModifiers()) || Modifier.isAbstract(method.getModifiers()) || Modifier.isStatic(method.getModifiers()) || method.isAnnotationPresent(Ignored.class)) { if (logger.isDebugEnabled()) { logger.debug("ignores method of controller " + controllerClass.getName() + "." + method.getName() + " [@ignored?not public?abstract?static?]"); }//from w w w . j a v a2s . c om return true; } // ?(?)????? for (Method past : pastMethods) { if (past.getName().equals(method.getName())) { if (Arrays.equals(past.getParameterTypes(), method.getParameterTypes())) { return true; } } } return false; }
From source file:org.apache.openaz.xacml.std.json.JSONResponse.java
/** * Use reflection to load the map with all the names of all DataTypes allowing us to output the shorthand * version rather than the full Identifier name. (to shorten the JSON output). The shorthand map is used * differently in JSONRequest than in JSONResponse, so there are similarities and differences in the * implementation. This is done once the first time a Request is processed. *///from ww w.j av a 2 s . co m private static void initOutputShorthandMap() throws JSONStructureException { Field[] declaredFields = XACML3.class.getDeclaredFields(); outputShorthandMap = new HashMap<String, String>(); for (Field field : declaredFields) { if (Modifier.isStatic(field.getModifiers()) && field.getName().startsWith("ID_DATATYPE") && Modifier.isPublic(field.getModifiers())) { try { Identifier id = (Identifier) field.get(null); String longName = id.stringValue(); // most names start with 'http://www.w3.org/2001/XMLSchema#' int sharpIndex = longName.lastIndexOf("#"); if (sharpIndex <= 0) { // some names start with 'urn:oasis:names:tc:xacml:1.0:data-type:' // or urn:oasis:names:tc:xacml:2.0:data-type: if (longName.contains(":data-type:")) { sharpIndex = longName.lastIndexOf(":"); } else { continue; } } String shortName = longName.substring(sharpIndex + 1); // put both the full name and the short name in the table outputShorthandMap.put(id.stringValue(), shortName); } catch (Exception e) { throw new JSONStructureException("Error loading ID Table, e=" + e); } } } }
From source file:jenkins.scm.api.SCMHeadMixinEqualityGenerator.java
/** * Creates the {@link SCMHeadMixin.Equality} instance. * * @param type the {@link SCMHead} type to create the instance for. * @return the {@link SCMHeadMixin.Equality} instance. */// w ww. j a v a 2 s .c o m @NonNull private SCMHeadMixin.Equality create(@NonNull Class<? extends SCMHead> type) { Map<String, Method> properties = new TreeMap<String, Method>(); for (Class clazz : (List<Class>) ClassUtils.getAllInterfaces(type)) { if (!SCMHeadMixin.class.isAssignableFrom(clazz)) { // not a mix-in continue; } if (SCMHeadMixin.class == clazz) { // no need to check this by reflection continue; } if (!Modifier.isPublic(clazz.getModifiers())) { // not public continue; } // this is a mixin interface, only look at declared properties; for (Method method : clazz.getDeclaredMethods()) { if (method.getReturnType() == Void.class) { // nothing to do with us continue; } if (!Modifier.isPublic(method.getModifiers())) { // should never get here continue; } if (Modifier.isStatic(method.getModifiers())) { // might get here with Java 8 continue; } if (method.getParameterTypes().length != 0) { // not a property continue; } String name = method.getName(); if (!name.matches("^((is[A-Z0-9_].*)|(get[A-Z0-9_].*))$")) { // not a property continue; } if (name.startsWith("is")) { name = "" + Character.toLowerCase(name.charAt(2)) + (name.length() > 3 ? name.substring(3) : ""); } else { name = "" + Character.toLowerCase(name.charAt(3)) + (name.length() > 4 ? name.substring(4) : ""); } if (properties.containsKey(name)) { // a higher priority interface already defined the method continue; } properties.put(name, method); } } if (properties.isEmpty()) { // no properties to consider return new ConstantEquality(); } if (forceReflection) { return new ReflectiveEquality(properties.values().toArray(new Method[properties.size()])); } // now we define the class ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); String name = SCMHeadMixin.class.getPackage().getName() + ".internal." + type.getName(); // TODO Move to 1.7 opcodes once baseline 1.612+ cw.visit(Opcodes.V1_6, ACC_PUBLIC, name.replace('.', '/'), null, Type.getInternalName(Object.class), new String[] { Type.getInternalName(SCMHeadMixin.Equality.class) }); generateDefaultConstructor(cw); generateEquals(cw, properties.values()); byte[] image = cw.toByteArray(); Class<? extends SCMHeadMixin.Equality> c = defineClass(name, image, 0, image.length) .asSubclass(SCMHeadMixin.Equality.class); try { return c.newInstance(); } catch (InstantiationException e) { // fallback to reflection } catch (IllegalAccessException e) { // fallback to reflection } return new ReflectiveEquality(properties.values().toArray(new Method[properties.size()])); }
From source file:com.google.gdt.eclipse.designer.model.property.DateTimeFormatPropertyEditor.java
public void configure(EditorState state, Map<String, Object> parameters) throws Exception { m_loader = new CompositeClassLoader() { @Override/*from w ww . jav a 2 s. c o m*/ public Class<?> loadClass(String name) throws ClassNotFoundException { if (ReflectionUtils.class.getCanonicalName().equals(name)) { return ReflectionUtils.class; } return super.loadClass(name); } }; m_loader.add(state.getEditorLoader(), null); // get static formats { m_formatClass = m_loader.loadClass("com.google.gwt.i18n.client.DateTimeFormat"); m_formatMethods = Lists.newArrayList(); for (Method method : m_formatClass.getMethods()) { int modifiers = method.getModifiers(); if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers) && method.getParameterTypes().length == 0 && method.getName().endsWith("Format")) { m_formatMethods.add(method); } } } // extract script { final String EXTRACT_PARAM = "extract"; if (parameters.containsKey(EXTRACT_PARAM)) { m_extractScript = (String) parameters.get(EXTRACT_PARAM); } else { m_extractScript = ""; } } // source template { final String SOURCE_PARAM = "source"; if (parameters.containsKey(SOURCE_PARAM)) { m_sourceTemplate = (String) parameters.get(SOURCE_PARAM); } else { m_sourceTemplate = ""; } } }
From source file:org.evosuite.setup.TestUsageChecker.java
public static boolean canUse(Field f, Class<?> ownerClass) { // TODO we could enable some methods from Object, like getClass if (f.getDeclaringClass().equals(java.lang.Object.class)) return false;// handled here to avoid printing reasons if (f.getDeclaringClass().equals(java.lang.Thread.class)) return false;// handled here to avoid printing reasons if (!Properties.USE_DEPRECATED && f.isAnnotationPresent(Deprecated.class)) { final Class<?> targetClass = Properties.getTargetClassAndDontInitialise(); if (Properties.hasTargetClassBeenLoaded() && !f.getDeclaringClass().equals(targetClass)) { logger.debug("Skipping deprecated field " + f.getName()); return false; }/*from w ww .j a va 2 s. com*/ } if (f.isSynthetic()) { logger.debug("Skipping synthetic field " + f.getName()); return false; } if (f.getName().startsWith("ajc$")) { logger.debug("Skipping AspectJ field " + f.getName()); return false; } if (!f.getType().equals(String.class) && !canUse(f.getType())) { return false; } // in, out, err if (f.getDeclaringClass().equals(FileDescriptor.class)) { return false; } if (Modifier.isPublic(f.getModifiers())) { // It may still be the case that the field is defined in a non-visible superclass of the class // we already know we can use. In that case, the compiler would be fine with accessing the // field, but reflection would start complaining about IllegalAccess! // Therefore, we set the field accessible to be on the safe side TestClusterUtils.makeAccessible(f); return true; } // If default access rights, then check if this class is in the same package as the target class if (!Modifier.isPrivate(f.getModifiers())) { // && !Modifier.isProtected(f.getModifiers())) { String packageName = ClassUtils.getPackageName(ownerClass); String declaredPackageName = ClassUtils.getPackageName(f.getDeclaringClass()); if (packageName.equals(Properties.CLASS_PREFIX) && packageName.equals(declaredPackageName)) { TestClusterUtils.makeAccessible(f); return true; } } return false; }
From source file:org.apache.axis2.jaxws.server.endpoint.injection.impl.WebServiceContextInjectorImpl.java
public void injectOnMethod(Object resource, Object instance, Method method) throws ResourceInjectionException { if (instance == null) { if (log.isDebugEnabled()) { log.debug("Cannot inject Resource on a null Service Instance."); }/*from w ww .j a va 2 s. c om*/ throw new ResourceInjectionException(Messages.getMessage("WebServiceContextInjectionImplErr1")); } if (method == null) { if (log.isDebugEnabled()) { log.debug("Cannot inject WebServiceContext on ServiceInstance Method, method cannot be NULL"); } throw new ResourceInjectionException(Messages.getMessage("WebServiceContextInjectionImplErr3")); } try { if (!Modifier.isPublic(method.getModifiers())) { setAccessible(method, true); } method.invoke(instance, resource); return; } catch (IllegalAccessException e) { throw new ResourceInjectionException(e); } catch (InvocationTargetException e) { throw new ResourceInjectionException(e); } }