Added JDK 7 safe getDeclaredAnnotation.

This commit is contained in:
StevenLawson 2014-09-02 12:33:38 -04:00
parent 680df00aec
commit a03bdea309
3 changed files with 63 additions and 79 deletions

View file

@ -22,7 +22,6 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UnsupportedLookAndFeelException;
public class BukkitTelnetClient
{
@ -77,9 +76,28 @@ public class BukkitTelnetClient
javax.swing.UIManager.setLookAndFeel(fallbackStyle.getClassName());
}
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex)
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
}
}
// JDK 7 safe getDeclaredAnnotation
public static <T extends Annotation> T getDeclaredAnnotation(final Method method, final Class<T> annotationClass)
{
java.util.Objects.requireNonNull(annotationClass);
T annotation = null;
for (final Annotation _annotation : method.getDeclaredAnnotations())
{
if (_annotation.annotationType() == annotationClass)
{
annotation = annotationClass.cast(_annotation);
break;
}
}
return annotation;
}
}

View file

@ -18,7 +18,6 @@
*/
package me.StevenLawson.BukkitTelnetClient;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
@ -50,15 +49,7 @@ public abstract class ConfigEntry
final Element itemElement = (Element) itemNode;
for (final Method method : getClass().getDeclaredMethods())
{
ParameterSetter annotation = null;
for (Annotation _annotation : method.getDeclaredAnnotations())
{
if (_annotation instanceof ParameterSetter)
{
annotation = (ParameterSetter) _annotation;
break;
}
}
final ParameterSetter annotation = BukkitTelnetClient.getDeclaredAnnotation(method, ParameterSetter.class);
if (annotation == null)
{
continue;
@ -99,15 +90,7 @@ public abstract class ConfigEntry
for (final Method method : getClass().getDeclaredMethods())
{
ParameterGetter annotation = null;
for (Annotation _annotation : method.getDeclaredAnnotations())
{
if (_annotation instanceof ParameterGetter)
{
annotation = (ParameterGetter) _annotation;
break;
}
}
final ParameterGetter annotation = BukkitTelnetClient.getDeclaredAnnotation(method, ParameterGetter.class);
if (annotation == null)
{
continue;

View file

@ -18,7 +18,6 @@
*/
package me.StevenLawson.BukkitTelnetClient;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.*;
import java.util.logging.Level;
@ -45,54 +44,6 @@ public abstract class ConfigEntryList<E extends ConfigEntry>
return entryClass;
}
public Element listToXML(final Document doc)
{
final Element parent = doc.createElement(getParentElementName());
for (final E entry : getList())
{
final Element item = doc.createElement(getItemElementName());
parent.appendChild(item);
for (final Method method : getEntryClass().getDeclaredMethods())
{
ParameterGetter annotation = null;
for (Annotation _annotation : method.getDeclaredAnnotations())
{
if (_annotation instanceof ParameterGetter)
{
annotation = (ParameterGetter) _annotation;
break;
}
}
if (annotation == null)
{
continue;
}
final Element parameter = doc.createElement(annotation.name());
Object value = null;
try
{
value = method.invoke(entry);
}
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
{
BukkitTelnetClient.LOGGER.log(Level.SEVERE, null, ex);
}
if (value != null)
{
parameter.appendChild(doc.createTextNode(value.toString()));
}
item.appendChild(parameter);
}
}
return parent;
}
public boolean listFromXML(final Document doc)
{
NodeList itemNodes = doc.getDocumentElement().getElementsByTagName(getParentElementName());
@ -123,15 +74,7 @@ public abstract class ConfigEntryList<E extends ConfigEntry>
final Element itemElement = (Element) itemNode;
for (final Method method : getEntryClass().getDeclaredMethods())
{
ParameterSetter annotation = null;
for (Annotation _annotation : method.getDeclaredAnnotations())
{
if (_annotation instanceof ParameterSetter)
{
annotation = (ParameterSetter) _annotation;
break;
}
}
final ParameterSetter annotation = BukkitTelnetClient.getDeclaredAnnotation(method, ParameterSetter.class);
if (annotation == null)
{
continue;
@ -168,6 +111,46 @@ public abstract class ConfigEntryList<E extends ConfigEntry>
return true;
}
public Element listToXML(final Document doc)
{
final Element parent = doc.createElement(getParentElementName());
for (final E entry : getList())
{
final Element item = doc.createElement(getItemElementName());
parent.appendChild(item);
for (final Method method : getEntryClass().getDeclaredMethods())
{
final ParameterGetter annotation = BukkitTelnetClient.getDeclaredAnnotation(method, ParameterGetter.class);
if (annotation == null)
{
continue;
}
final Element parameter = doc.createElement(annotation.name());
Object value = null;
try
{
value = method.invoke(entry);
}
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
{
BukkitTelnetClient.LOGGER.log(Level.SEVERE, null, ex);
}
if (value != null)
{
parameter.appendChild(doc.createTextNode(value.toString()));
}
item.appendChild(parameter);
}
}
return parent;
}
public abstract String getParentElementName();
public abstract String getItemElementName();