Gabriel Tofvesson d4c53993ec - Added tests
- Refactored package to match my site name
- Renamed "logging" package to "stream"
- Fixed various bugs
- Fixed issues with illegal reflective access
- Added proper throwable for when a dangerous threaded action happens in async
- Added more reflective tools
  - Added ability to auto-generate simple wrapper classes
  - Added tools for converting enums to a flag-like system
  - Added annotation scanning for a supplied classloader
- Properly renamed module to "libRefTools"
2018-01-04 01:10:08 +01:00

33 lines
1.3 KiB
Java

package net.tofvesson.reflection;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class Enums {
public static <T extends Enum<T>> boolean isFlagSet(int flags, T flag){
return (flags&flag.ordinal())!=0;
}
public static void asFlags(){
Class<?> c = SafeReflection.getCallerClass();
if(c==null || !c.isEnum()) return;
//noinspection unchecked
asFlags((Class<? extends Enum<?>>)c);
}
public static void asFlags(Class<? extends Enum<?>> type){
int increment = -1;
for(Field f : type.getDeclaredFields())
if(Modifier.isStatic(f.getModifiers()) && f.getType().equals(type))
try {
SafeReflection.setValue(f.get(null), Enum.class, "ordinal", (int) Math.pow(2, ++increment));
}catch(Exception e){ e.printStackTrace(); }
}
public static void asOrdinals(Class<? extends Enum<?>> type){
int increment = -1;
for(Field f : type.getDeclaredFields())
if(Modifier.isStatic(f.getModifiers()) && f.getType().equals(type))
try {
SafeReflection.setValue(f.get(null), Enum.class, "ordinal", ++increment);
}catch(Exception e){ e.printStackTrace(); }
}
}