use of dyvil.annotation.internal.NonNull in project Dyvil by Dyvil.
the class AnonymousClassLMF method buildCallSite.
@NonNull
@Override
public CallSite buildCallSite() throws LambdaConversionException {
final Class<?> innerClass = this.spinInnerClass();
if (this.parameterCount == 0) {
final Constructor[] ctrs = innerClass.getDeclaredConstructors();
if (ctrs.length != 1) {
final String message = "Expected one lambda constructor for " + innerClass.getCanonicalName() + ", got " + ctrs.length;
throw new LambdaConversionException(message);
}
try {
final Constructor ctr = ctrs[0];
ctr.setAccessible(true);
final Object inst = ctr.newInstance();
return new ConstantCallSite(MethodHandles.constant(this.samBase, inst));
} catch (ReflectiveOperationException e) {
throw new LambdaConversionException("Exception instantiating lambda object", e);
}
}
try {
UNSAFE.ensureClassInitialized(innerClass);
return new ConstantCallSite(LOOKUP.findStatic(innerClass, NAME_FACTORY, this.invokedType));
} catch (ReflectiveOperationException e) {
throw new LambdaConversionException("Exception finding constructor", e);
}
}
use of dyvil.annotation.internal.NonNull in project Dyvil by Dyvil.
the class AnnotationProxyFactory method buildCallSite.
@NonNull
public CallSite buildCallSite() throws Exception {
final Class<?> innerClass = this.spinInnerClass();
if (this.parameterCount == 0) {
final Constructor[] ctrs = innerClass.getDeclaredConstructors();
if (ctrs.length != 1) {
final String message = "Expected one annotation constructor for " + innerClass.getCanonicalName() + ", got " + ctrs.length;
throw new Exception(message);
}
try {
final Constructor ctr = ctrs[0];
ctr.setAccessible(true);
final Object inst = ctr.newInstance();
return new ConstantCallSite(MethodHandles.constant(this.annotationType, inst));
} catch (ReflectiveOperationException e) {
throw new Exception("Exception instantiating annotation proxy", e);
}
}
try {
UNSAFE.ensureClassInitialized(innerClass);
return new ConstantCallSite(LOOKUP.findStatic(innerClass, NAME_FACTORY, this.invokedType));
} catch (ReflectiveOperationException e) {
throw new Exception("Exception finding constructor", e);
}
}
use of dyvil.annotation.internal.NonNull in project Dyvil by Dyvil.
the class FieldReflection method getObjects.
@NonNull
public static <T> T[] getObjects(@NonNull Class clazz, Object instance, @NonNull Class<T> fieldType, boolean subtypes) {
List list = new ArrayList();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
try {
Class c = field.getType();
Object o = field.get(instance);
if (c == fieldType || subtypes && fieldType.isAssignableFrom(c)) {
list.add(o);
}
} catch (Exception ex) {
}
}
return (T[]) list.toArray();
}
use of dyvil.annotation.internal.NonNull in project Dyvil by Dyvil.
the class ReflectUtils method getFileLocation.
@NonNull
public static File getFileLocation(@NonNull Class<?> klass) throws ClassNotFoundException {
final String classLocation = '/' + klass.getName().replace('.', '/') + ".class";
final URL url = klass.getResource(classLocation);
if (url == null) {
throw new ClassNotFoundException("Location not found: " + classLocation);
}
final String path = url.toString().replace(File.separatorChar, '/');
int index = path.lastIndexOf(classLocation);
if (index < 0) {
throw new ClassNotFoundException("Invalid Path: " + path);
}
int startIndex = 0;
if (path.charAt(index - 1) == '!') {
index--;
// strip leading 'jar:'
startIndex = 4;
} else {
index++;
}
final String newPath = path.substring(startIndex, index);
try {
return new File(new URI(newPath));
} catch (URISyntaxException ex) {
throw new ClassNotFoundException("Invalid URI: " + newPath, ex);
}
}
Aggregations