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.lang.reflect.Method;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.swing.UnsupportedLookAndFeelException;
public class BukkitTelnetClient public class BukkitTelnetClient
{ {
@ -77,9 +76,28 @@ public class BukkitTelnetClient
javax.swing.UIManager.setLookAndFeel(fallbackStyle.getClassName()); 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); 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; package me.StevenLawson.BukkitTelnetClient;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.logging.Level; import java.util.logging.Level;
@ -50,15 +49,7 @@ public abstract class ConfigEntry
final Element itemElement = (Element) itemNode; final Element itemElement = (Element) itemNode;
for (final Method method : getClass().getDeclaredMethods()) for (final Method method : getClass().getDeclaredMethods())
{ {
ParameterSetter annotation = null; final ParameterSetter annotation = BukkitTelnetClient.getDeclaredAnnotation(method, ParameterSetter.class);
for (Annotation _annotation : method.getDeclaredAnnotations())
{
if (_annotation instanceof ParameterSetter)
{
annotation = (ParameterSetter) _annotation;
break;
}
}
if (annotation == null) if (annotation == null)
{ {
continue; continue;
@ -99,15 +90,7 @@ public abstract class ConfigEntry
for (final Method method : getClass().getDeclaredMethods()) for (final Method method : getClass().getDeclaredMethods())
{ {
ParameterGetter annotation = null; final ParameterGetter annotation = BukkitTelnetClient.getDeclaredAnnotation(method, ParameterGetter.class);
for (Annotation _annotation : method.getDeclaredAnnotations())
{
if (_annotation instanceof ParameterGetter)
{
annotation = (ParameterGetter) _annotation;
break;
}
}
if (annotation == null) if (annotation == null)
{ {
continue; continue;

View file

@ -18,7 +18,6 @@
*/ */
package me.StevenLawson.BukkitTelnetClient; package me.StevenLawson.BukkitTelnetClient;
import java.lang.annotation.Annotation;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.util.*; import java.util.*;
import java.util.logging.Level; import java.util.logging.Level;
@ -45,54 +44,6 @@ public abstract class ConfigEntryList<E extends ConfigEntry>
return entryClass; 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) public boolean listFromXML(final Document doc)
{ {
NodeList itemNodes = doc.getDocumentElement().getElementsByTagName(getParentElementName()); NodeList itemNodes = doc.getDocumentElement().getElementsByTagName(getParentElementName());
@ -123,15 +74,7 @@ public abstract class ConfigEntryList<E extends ConfigEntry>
final Element itemElement = (Element) itemNode; final Element itemElement = (Element) itemNode;
for (final Method method : getEntryClass().getDeclaredMethods()) for (final Method method : getEntryClass().getDeclaredMethods())
{ {
ParameterSetter annotation = null; final ParameterSetter annotation = BukkitTelnetClient.getDeclaredAnnotation(method, ParameterSetter.class);
for (Annotation _annotation : method.getDeclaredAnnotations())
{
if (_annotation instanceof ParameterSetter)
{
annotation = (ParameterSetter) _annotation;
break;
}
}
if (annotation == null) if (annotation == null)
{ {
continue; continue;
@ -168,6 +111,46 @@ public abstract class ConfigEntryList<E extends ConfigEntry>
return true; 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 getParentElementName();
public abstract String getItemElementName(); public abstract String getItemElementName();