Skip to content

Commit

Permalink
Initial code commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Sriram Keerthi M K committed Dec 17, 2015
1 parent 0942589 commit 3eb036e
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
hs_err_pid*

# IntelliJ files
.idea/*
.idea/
*.iml
target/*
target/
25 changes: 25 additions & 0 deletions statiflex/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.caffinc</groupId>
<artifactId>statiflex</artifactId>
<name>statiflex</name>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
99 changes: 99 additions & 0 deletions statiflex/src/main/java/com/caffinc/statiflex/Statiflex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.caffinc.statiflex;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;


/**
* Uses reflection to modify static final fields.
* User under extreme duress.
*
* uuuuuuu
* uu$$$$$$$$$$$uu
* uu$$$$$$$$$$$$$$$$$uu
* u$$$$$$$$$$$$$$$$$$$$$u
* u$$$$$$$$$$$$$$$$$$$$$$$u
* u$$$$$$$$$$$$$$$$$$$$$$$$$u
* u$$$$$$$$$$$$$$$$$$$$$$$$$u
* u$$$$$$" "$$$" "$$$$$$u
* "$$$$" u$u $$$$"
* $$$u u$u u$$$
* $$$u u$$$u u$$$
* "$$$$uu$$$ $$$uu$$$$"
* "$$$$$$$" "$$$$$$$"
* u$$$$$$$u$$$$$$$u
* u$"$"$"$"$"$"$u
* uuu $$u$ $ $ $ $u$$ uuu
* u$$$$ $$$$$u$u$u$$$ u$$$$
* $$$$$uu "$$$$$$$$$" uu$$$$$$
*u$$$$$$$$$$$uu """"" uuuu$$$$$$$$$$
*$$$$"""$$$$$$$$$$uuu uu$$$$$$$$$"""$$$"
* """ ""$$$$$$$$$$$uu ""$"""
* uuuu ""$$$$$$$$$$uuu
* u$$$uuu$$$$$$$$$uu ""$$$$$$$$$$$uuu$$$
* $$$$$$$$$$"""" ""$$$$$$$$$$$"
* "$$$$$" ""$$$$""
* $$$" $$$$"
*
* @author Sriram
*/
public class Statiflex
{
private static final Logger LOG = LoggerFactory.getLogger( Statiflex.class );


/**
* Private constructor, no instantiation
*/
private Statiflex()
{
}


/**
* Changes the value of a private static final field in a class
* Note: Compiler optimization might inline static final fields which will break this, use with caution
* @param clazz Class to modify
* @param fieldName Field to modify
* @param value Value to set
* @return true if set succeeded, false otherwise
* @throws NoSuchFieldException Thrown if the field specified was not found
*/
public static boolean flex( Class clazz, String fieldName, Object value ) throws NoSuchFieldException
{
return flex( clazz.getDeclaredField( fieldName ), value );
}


/**
* Changes the value of a private static final field
* @param field Field to modify
* @param value Value to set
* @return true if set succeeded, false otherwise
*/
private static boolean flex( Field field, Object value )
{
try {
Field modifiersField = Field.class.getDeclaredField( "modifiers" );
boolean isModifierAccessible = modifiersField.isAccessible();
modifiersField.setAccessible( true );
modifiersField.setInt( field, field.getModifiers() & ~Modifier.FINAL );

boolean isAccessible = field.isAccessible();
field.setAccessible( true );
field.set( null, value );

field.setAccessible( isAccessible );
modifiersField.setAccessible( isModifierAccessible );
return true;
} catch ( IllegalAccessException e ) {
LOG.error( "Could not access field {}", field.getName(), e );
} catch ( NoSuchFieldException e ) {
LOG.error( "Could not find field, cannot modify value", e );
}
return false;
}
}
8 changes: 8 additions & 0 deletions statiflex/src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
33 changes: 33 additions & 0 deletions statiflex/src/test/java/com/caffinc/statiflex/StatiflexTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.caffinc.statiflex;

import org.junit.Assert;
import org.junit.Test;


/**
* Tests the Statiflex class
* @author Sriram
*/
public class StatiflexTest
{
static class DummyClass
{
private static final String DUMMY_FIELD = getDummyValue();


private static String getDummyValue()
{
return "TEST";
}
}


@Test
public void testFlex() throws Exception
{
Assert.assertEquals( "private static final value should be TEST", "TEST", DummyClass.DUMMY_FIELD );
Statiflex.flex( DummyClass.class, "DUMMY_FIELD", "TESTED" );
Assert.assertEquals( "private static final value should be TESTED after flexing", "TESTED", DummyClass
.DUMMY_FIELD );
}
}

0 comments on commit 3eb036e

Please sign in to comment.