Created
May 24, 2013 13:05
-
-
Save vaclavcadek/5643362 to your computer and use it in GitHub Desktop.
Annotation example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
public FooBar { | |
@WiredMessage(mapping="BUTTONS-FOO") | |
private String foo = "foo"; | |
// clear selected button label | |
@WiredMessage(mapping="BUTTONS-BAR") | |
private String bar = "bar"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
public class Main { | |
public static void main(String[] args) { | |
FooBar foobar = new FooBar(); | |
TranslationSupport.processWiredMessages(foobar, foobar.getClass()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A helper class that performs update of tests if they are customized by Message Models. | |
*/ | |
public class TranslationSupport | |
{ | |
/** | |
* Process all fields annotated with @link {@link WiredMessage} annotation | |
* and try to set the field with related message. | |
* @param translatableTactic | |
* @param instance | |
* @param clazz | |
*/ | |
public static void processWiredMessages(final Object instance, | |
final Class<?> clazz) | |
{ | |
final Field[] fields = clazz.getDeclaredFields(); | |
for (Field f : fields) | |
{ | |
if (f.isAnnotationPresent(WiredMessage.class)) | |
{ | |
final WiredMessage wm = f.getAnnotation(WiredMessage.class); | |
String value = "some value"; | |
try | |
{ | |
if (value != null) | |
{ | |
f.setAccessible(true); | |
f.set(instance, value); | |
f.setAccessible(false); | |
} | |
} | |
catch (Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target(ElementType.FIELD) | |
public @interface WiredMessage | |
{ | |
String mapping(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment