Search in sources :

Example 6 with NonNull

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);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) NonNull(dyvil.annotation.internal.NonNull)

Example 7 with NonNull

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);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) ConstantCallSite(java.lang.invoke.ConstantCallSite) NonNull(dyvil.annotation.internal.NonNull)

Example 8 with NonNull

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();
}
Also used : Field(java.lang.reflect.Field) ArrayList(dyvil.collection.mutable.ArrayList) ArrayList(dyvil.collection.mutable.ArrayList) List(dyvil.collection.List) NonNull(dyvil.annotation.internal.NonNull)

Example 9 with NonNull

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);
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) File(java.io.File) URI(java.net.URI) URL(java.net.URL) NonNull(dyvil.annotation.internal.NonNull)

Aggregations

NonNull (dyvil.annotation.internal.NonNull)9 ArrayList (dyvil.collection.mutable.ArrayList)2 IType (dyvilx.tools.compiler.ast.type.IType)2 TypeList (dyvilx.tools.compiler.ast.type.TypeList)2 Marker (dyvilx.tools.parsing.marker.Marker)2 Constructor (java.lang.reflect.Constructor)2 DyvilModifiers (dyvil.annotation.internal.DyvilModifiers)1 List (dyvil.collection.List)1 LambdaType (dyvilx.tools.compiler.ast.type.compound.LambdaType)1 GenericType (dyvilx.tools.compiler.ast.type.generic.GenericType)1 InternalGenericType (dyvilx.tools.compiler.ast.type.generic.InternalGenericType)1 InternalType (dyvilx.tools.compiler.ast.type.raw.InternalType)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 ConstantCallSite (java.lang.invoke.ConstantCallSite)1 Field (java.lang.reflect.Field)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1