kotlin.jvm.JvmClassMappingKt Java Examples

The following examples show how to use kotlin.jvm.JvmClassMappingKt. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BeanUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Retrieve the Java constructor corresponding to the Kotlin primary constructor, if any.
 * @param clazz the {@link Class} of the Kotlin class
 * @see <a href="https://kotlinlang.org/docs/reference/classes.html#constructors">
 * https://kotlinlang.org/docs/reference/classes.html#constructors</a>
 */
@Nullable
public static <T> Constructor<T> findPrimaryConstructor(Class<T> clazz) {
	try {
		KFunction<T> primaryCtor = KClasses.getPrimaryConstructor(JvmClassMappingKt.getKotlinClass(clazz));
		if (primaryCtor == null) {
			return null;
		}
		Constructor<T> constructor = ReflectJvmMapping.getJavaConstructor(primaryCtor);
		if (constructor == null) {
			throw new IllegalStateException(
					"Failed to find Java constructor for Kotlin primary constructor: " + clazz.getName());
		}
		return constructor;
	}
	catch (UnsupportedOperationException ex) {
		return null;
	}
}
 
Example #2
Source File: BeanUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Retrieve the Java constructor corresponding to the Kotlin primary constructor, if any.
 * @param clazz the {@link Class} of the Kotlin class
 * @see <a href="http://kotlinlang.org/docs/reference/classes.html#constructors">
 * http://kotlinlang.org/docs/reference/classes.html#constructors</a>
 */
@Nullable
public static <T> Constructor<T> findPrimaryConstructor(Class<T> clazz) {
	try {
		KFunction<T> primaryCtor = KClasses.getPrimaryConstructor(JvmClassMappingKt.getKotlinClass(clazz));
		if (primaryCtor == null) {
			return null;
		}
		Constructor<T> constructor = ReflectJvmMapping.getJavaConstructor(primaryCtor);
		if (constructor == null) {
			throw new IllegalStateException(
					"Failed to find Java constructor for Kotlin primary constructor: " + clazz.getName());
		}
		return constructor;
	}
	catch (UnsupportedOperationException ex) {
		return null;
	}
}
 
Example #3
Source File: TypeParser.java    From typescript-generator with MIT License 6 votes vote down vote up
@Override
public Type getMethodReturnType(Method method) {
    final KFunction<?> kFunction = ReflectJvmMapping.getKotlinFunction(method);
    if (kFunction != null) {
        return getType(kFunction.getReturnType(), new LinkedHashMap<>());
    } else {
        // `method` might be a getter so try to find a corresponding field and pass it to Kotlin reflection
        final KClass<?> kClass = JvmClassMappingKt.getKotlinClass(method.getDeclaringClass());
        final Optional<Field> field = KClasses.getMemberProperties(kClass).stream()
                .filter(kProperty -> Objects.equals(ReflectJvmMapping.getJavaGetter(kProperty), method))
                .map(kProperty -> ReflectJvmMapping.getJavaField(kProperty))
                .filter(Objects::nonNull)
                .findFirst();
        if (field.isPresent()) {
            return getFieldType(field.get());
        }
    }
    return javaTypeParser.getMethodReturnType(method);
}
 
Example #4
Source File: RdIdeaLoggerFactory.java    From azure-devops-intellij with MIT License 4 votes vote down vote up
public synchronized static void initialize() {
    if (!initialized) {
        Statics.Companion.of(JvmClassMappingKt.getKotlinClass(ILoggerFactory.class)).push(INSTANCE);
        initialized = true;
    }
}
 
Example #5
Source File: TypeParser.java    From typescript-generator with MIT License 4 votes vote down vote up
private Type getBareType(KType kType, Map<String, JTypeVariable<?>> typeParameters) {
    final KClassifier kClassifier = kType.getClassifier();
    if (kClassifier instanceof KClass) {
        final KClass<?> kClass = (KClass<?>) kClassifier;
        final Class<?> javaClass = JvmClassMappingKt.getJavaClass(kClass);
        if (isArrayOfPrimitiveType(javaClass)) {
            return javaClass;
        }
        final List<KTypeProjection> arguments = kType.getArguments();
        if (arguments.isEmpty()) {
            return javaClass;
        } else if (javaClass.isArray()) {
            return new JGenericArrayType(getType(arguments.get(0).getType(), typeParameters));
        } else {
            final List<Type> javaArguments = arguments.stream()
                    .map(argument -> getType(argument.getType(), typeParameters))
                    .collect(Collectors.toList());
            return Utils.createParameterizedType(javaClass, javaArguments);
        }
    }
    if (kClassifier instanceof KTypeParameter) {
        final KTypeParameter kTypeParameter = (KTypeParameter) kClassifier;
        final JTypeVariable<?> typeVariableFromMap = typeParameters.get(kTypeParameter.getName());
        if (typeVariableFromMap != null) {
            return typeVariableFromMap;
        } else {
            final TypeVariable<?> typeVariable = getJavaTypeVariable(kType);
            final JTypeVariable<?> newTypeVariable = new JTypeVariable<>(
                    typeVariable != null ? typeVariable.getGenericDeclaration() : null,
                    kTypeParameter.getName(),
                    /*bounds*/ null,
                    typeVariable != null ? typeVariable.getAnnotatedBounds() : null,
                    typeVariable != null ? typeVariable.getAnnotations() : null,
                    typeVariable != null ? typeVariable.getDeclaredAnnotations() : null
            );
            typeParameters.put(kTypeParameter.getName(), newTypeVariable);
            final Type[] bounds = getTypes(kTypeParameter.getUpperBounds(), typeParameters).toArray(new Type[0]);
            newTypeVariable.setBounds(bounds);
            return newTypeVariable;
        }
    }
    throw new RuntimeException("Unexpected type: " + kType.toString());
}
 
Example #6
Source File: Feature.java    From yawp with MIT License 4 votes vote down vote up
public <T> QueryBuilder<T> yawp(KClass<T> kClazz) {
    return yawp(JvmClassMappingKt.getJavaClass(kClazz));
}
 
Example #7
Source File: Feature.java    From yawp with MIT License 4 votes vote down vote up
public <T> T from(String json, KClass<T> kClazz) {
    return JsonUtils.from(yawp, json, JvmClassMappingKt.getJavaClass(kClazz));
}
 
Example #8
Source File: Feature.java    From yawp with MIT License 4 votes vote down vote up
public <T> List<T> fromList(String json, KClass<T> kClazz) {
    return JsonUtils.fromList(yawp, json, JvmClassMappingKt.getJavaClass(kClazz));
}
 
Example #9
Source File: Feature.java    From yawp with MIT License 4 votes vote down vote up
public <K, V> Map<K, V> fromMap(String json, KClass<K> kKeyClazz, KClass<V> kValueClazz) {
    return JsonUtils.fromMap(yawp, json, JvmClassMappingKt.getJavaClass(kKeyClazz), JvmClassMappingKt.getJavaClass(kValueClazz));
}