Skip to content

Instantly share code, notes, and snippets.

@vaclavcadek
Created May 24, 2013 13:05
Show Gist options
  • Save vaclavcadek/5643362 to your computer and use it in GitHub Desktop.
Save vaclavcadek/5643362 to your computer and use it in GitHub Desktop.
Annotation example
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";
}
package com.example;
public class Main {
public static void main(String[] args) {
FooBar foobar = new FooBar();
TranslationSupport.processWiredMessages(foobar, foobar.getClass());
}
}
/**
* 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();
}
}
}
}
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