List of usage examples for java.lang.reflect Modifier FINAL
int FINAL
To view the source code for java.lang.reflect Modifier FINAL.
Click Source Link
From source file:org.granitemc.granite.reflect.ReflectionUtils.java
/** * Will force access to a field. This even works with private static final fields! * <p/>/*from ww w . j a v a 2 s . c om*/ * Internally, this uses some reflection-on-reflection trickery I found on StackOverflow :) * * @param f The field to force access to */ public static void forceStaticAccessible(Field f) { try { f.setAccessible(true); // Some reflection-ception trickery Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(f, f.getModifiers() & ~Modifier.FINAL); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } }
From source file:org.apache.atlas.repository.graph.TitanGraphProvider.java
/** * Titan loads index backend name to implementation using StandardIndexProvider.ALL_MANAGER_CLASSES * But StandardIndexProvider.ALL_MANAGER_CLASSES is a private static final ImmutableMap * Only way to inject Solr5Index is to modify this field. So, using hacky reflection to add Sol5Index *///from w ww . j av a 2 s .co m private static void addSolr5Index() { try { Field field = StandardIndexProvider.class.getDeclaredField("ALL_MANAGER_CLASSES"); field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); Map<String, String> customMap = new HashMap(StandardIndexProvider.getAllProviderClasses()); customMap.put("solr5", Solr5Index.class.getName()); ImmutableMap<String, String> immap = ImmutableMap.copyOf(customMap); field.set(null, immap); LOG.debug("Injected solr5 index - {}", Solr5Index.class.getName()); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Spy.java
private static int modifierFromString(String s) { int m = 0x0;//from w w w .ja v a 2 s. c o m if ("public".equals(s)) m |= Modifier.PUBLIC; else if ("protected".equals(s)) m |= Modifier.PROTECTED; else if ("private".equals(s)) m |= Modifier.PRIVATE; else if ("static".equals(s)) m |= Modifier.STATIC; else if ("final".equals(s)) m |= Modifier.FINAL; else if ("transient".equals(s)) m |= Modifier.TRANSIENT; else if ("volatile".equals(s)) m |= Modifier.VOLATILE; return m; }
From source file:org.apache.atlas.repository.graphdb.titan0.Titan0Database.java
/** * Titan loads index backend name to implementation using * StandardIndexProvider.ALL_MANAGER_CLASSES But * StandardIndexProvider.ALL_MANAGER_CLASSES is a private static final * ImmutableMap Only way to inject Solr5Index is to modify this field. So, * using hacky reflection to add Sol5Index *//*w w w . j av a2 s . c om*/ private static void addSolr5Index() { try { Field field = StandardIndexProvider.class.getDeclaredField("ALL_MANAGER_CLASSES"); field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); Map<String, String> customMap = new HashMap<>(StandardIndexProvider.getAllProviderClasses()); customMap.put("solr", Solr5Index.class.getName()); // for // consistency // with Titan // 1.0.0 customMap.put("solr5", Solr5Index.class.getName()); // for backward // compatibility ImmutableMap<String, String> immap = ImmutableMap.copyOf(customMap); field.set(null, immap); LOG.debug("Injected solr5 index - {}", Solr5Index.class.getName()); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.lunarray.model.descriptor.builder.annotation.base.listener.property.SetImmutableListener.java
/** {@inheritDoc} */ @Override// w w w.j a v a 2s. c om public void handleEvent(final UpdatedPropertyTypeEvent<P, E, B> event) throws EventException { SetImmutableListener.LOGGER.debug("Handling event {}", event); Validate.notNull(event, "Event may not be null."); final AnnotationPropertyDescriptorBuilder<P, E, B> builder = event.getBuilder(); final DescribedProperty<P> property = event.getProperty(); // Mutability test. if ((property.getModifiers() & Modifier.FINAL) == Modifier.FINAL) { builder.immutable(); } }
From source file:io.github.wysohn.triggerreactor.tools.ReflectionUtil.java
/** * https://stackoverflow.com/questions/3301635/change-private-static-final-field-using-java-reflection * @param field// w w w. ja va 2s .c om * @param newValue * @throws SecurityException * @throws NoSuchFieldException * @throws Exception */ private static void setFinalField(Object target, Field field, Object newValue) throws NoSuchFieldException { field.setAccessible(true); Field modifiersField = null; try { modifiersField = Field.class.getDeclaredField("modifiers"); } catch (SecurityException e1) { e1.printStackTrace(); } modifiersField.setAccessible(true); try { modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); } catch (IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); } try { field.set(target, newValue); } catch (IllegalAccessException e) { e.printStackTrace(); } }
From source file:org.opendaylight.sxp.route.core.RoutingServiceFactoryTest.java
private void changeOsIsLinuxField(final boolean mockedOsName) throws NoSuchFieldException, IllegalAccessException { final Field osNameField = SystemUtils.class.getDeclaredField("IS_OS_LINUX"); osNameField.setAccessible(true);/*from www. ja v a 2 s . c o m*/ Field modifiers = osNameField.getClass().getDeclaredField("modifiers"); modifiers.setAccessible(true); modifiers.setInt(osNameField, osNameField.getModifiers() & ~Modifier.FINAL); osNameField.set(null, mockedOsName); }
From source file:com.coinblesk.client.utils.ClientUtils.java
private static void setFinalStatic(Field field, Object newValue) throws Exception { field.setAccessible(true);//w ww . j av a2s . c om // remove final modifier from field try { Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); } catch (Exception e) { Log.w(TAG, "Could not remove 'final' modifier: " + e.getMessage()); } field.set(null, newValue); }
From source file:com.orange.clara.cloud.servicedbdumper.task.boot.sequences.BootSequenceSecurity.java
public void removeEncryptionRestriction() { if (!isRestrictedCryptography()) { logger.info("Cryptography restrictions removal not needed"); return;//from www . jav a 2 s.c om } try { /* * Do the following, but with reflection to bypass access checks: * * JceSecurity.isRestricted = false; * JceSecurity.defaultPolicy.perms.clear(); * JceSecurity.defaultPolicy.add(CryptoAllPermission.INSTANCE); */ final Class<?> jceSecurity = Class.forName("javax.crypto.JceSecurity"); final Class<?> cryptoPermissions = Class.forName("javax.crypto.CryptoPermissions"); final Class<?> cryptoAllPermission = Class.forName("javax.crypto.CryptoAllPermission"); final Field isRestrictedField = jceSecurity.getDeclaredField("isRestricted"); isRestrictedField.setAccessible(true); final Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(isRestrictedField, isRestrictedField.getModifiers() & ~Modifier.FINAL); isRestrictedField.set(null, false); final Field defaultPolicyField = jceSecurity.getDeclaredField("defaultPolicy"); defaultPolicyField.setAccessible(true); final PermissionCollection defaultPolicy = (PermissionCollection) defaultPolicyField.get(null); final Field perms = cryptoPermissions.getDeclaredField("perms"); perms.setAccessible(true); ((Map<?, ?>) perms.get(defaultPolicy)).clear(); final Field instance = cryptoAllPermission.getDeclaredField("INSTANCE"); instance.setAccessible(true); defaultPolicy.add((Permission) instance.get(null)); logger.info("Successfully removed cryptography restrictions"); } catch (final Exception e) { logger.warn("Failed to remove cryptography restrictions", e); } }
From source file:org.modeshape.rhq.util.I18n.java
/** * Should be called in a <code>static</code> block to load the properties file and assign values to the class string fields. * /*w ww . jav a2 s . c om*/ * @throws IllegalStateException if there is a problem reading the I8n class file or properties file */ protected void initialize() { final Map<String, Field> fields = new HashMap<String, Field>(); // collect all public, static, non-final, string fields try { for (final Field field : getClass().getDeclaredFields()) { final int modifiers = field.getModifiers(); if ((field.getType() == String.class) && ((modifiers & Modifier.PUBLIC) == Modifier.PUBLIC) && ((modifiers & Modifier.STATIC) == Modifier.STATIC) && ((modifiers & Modifier.FINAL) != Modifier.FINAL)) { fields.put(field.getName(), field); } } } catch (final Exception e) { throw new IllegalStateException(I18n.bind(UtilI18n.problemLoadingI18nClass, getClass().getName()), e); } // return if nothing to do if (ToolBox.isEmpty(fields)) { return; } // load properties file InputStream stream = null; IllegalStateException problem = null; try { final Class<? extends I18n> thisClass = getClass(); final String bundleName = thisClass.getName().replaceAll("\\.", "/").concat(".properties"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ final URL url = thisClass.getClassLoader().getResource(bundleName); stream = url.openStream(); final Collection<String> errors = new ArrayList<String>(); final Properties props = new I18nProperties(fields, thisClass, errors); props.load(stream); // log errors for any properties keys that don't have fields for (final String error : errors) { if (problem == null) { problem = new IllegalStateException(error); } this.logger.error(error); } // log errors for any fields that don't have properties for (final String fieldName : fields.keySet()) { final String error = I18n.bind(UtilI18n.missingPropertiesKey, fieldName, getClass().getName()); if (problem == null) { problem = new IllegalStateException(error); } this.logger.error(error); } } catch (final Exception e) { throw new IllegalStateException(I18n.bind(UtilI18n.problemLoadingI18nProperties, getClass().getName()), e); } finally { if (stream != null) { try { stream.close(); } catch (final Exception e) { } finally { stream = null; } } if (problem != null) { throw problem; } } }