Skip to content

Commit

Permalink
Use java.util.function.Predicate instead of Guava in JS modules
Browse files Browse the repository at this point in the history
  • Loading branch information
udalov committed Apr 3, 2017
1 parent 7c3ad97 commit f723ad7
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@

package org.jetbrains.kotlin.js.patterns;

import com.google.common.base.Predicate;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.FunctionDescriptor;

import java.util.function.Predicate;

public interface DescriptorPredicate extends Predicate<FunctionDescriptor> {
@Override
boolean apply(@Nullable FunctionDescriptor descriptor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@

package org.jetbrains.kotlin.js.patterns;

import com.google.common.base.Predicate;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.builtins.PrimitiveType;
import org.jetbrains.kotlin.name.Name;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;

public final class NamePredicate implements Predicate<Name> {

Expand Down Expand Up @@ -75,7 +74,7 @@ public NamePredicate(@NotNull String... validNames) {
this(Arrays.asList(validNames));
}

public NamePredicate(@NotNull List<String> validNames) {
private NamePredicate(@NotNull List<String> validNames) {
for (String validName : validNames) {
this.validNames.add(Name.guessByFirstCharacter(validName));
}
Expand All @@ -90,7 +89,7 @@ public NamePredicate(@NotNull Name... validNames) {
}

@Override
public boolean apply(@Nullable Name name) {
return name != null && validNames.contains(name);
public boolean test(Name name) {
return validNames.contains(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private static DescriptorPredicate pattern(@NotNull List<NamePredicate> checkers
private static DescriptorPredicate pattern(@NotNull List<NamePredicate> checkers, @Nullable List<NamePredicate> arguments) {
assert !checkers.isEmpty();
final List<NamePredicate> checkersWithPrefixChecker = Lists.newArrayList();
if (!checkers.get(0).apply(KOTLIN_NAME)) {
if (!checkers.get(0).test(KOTLIN_NAME)) {
checkersWithPrefixChecker.add(KOTLIN_NAME_PREDICATE);
}

Expand All @@ -134,8 +134,7 @@ private static DescriptorPredicate pattern(@NotNull List<NamePredicate> checkers

return new DescriptorPredicate() {
@Override
public boolean apply(@Nullable FunctionDescriptor descriptor) {
assert descriptor != null : "argument for DescriptorPredicate.apply should not be null, checkers=" + checkersWithPrefixChecker;
public boolean test(FunctionDescriptor descriptor) {
//TODO: no need to wrap if we check beforehand
try {
return doApply(descriptor);
Expand All @@ -162,7 +161,7 @@ private boolean checkAllArgumentsValidIfNeeded(@NotNull FunctionDescriptor descr
ValueParameterDescriptor valueParameterDescriptor = valueParameterDescriptors.get(i);
Name name = DescriptorUtilsKt.getNameIfStandardType(valueParameterDescriptor.getType());
NamePredicate namePredicate = argumentCheckers.get(i);
if (!namePredicate.apply(name)) return false;
if (!namePredicate.test(name)) return false;
}
}
return true;
Expand All @@ -172,7 +171,7 @@ private boolean allNamePartsValid(@NotNull List<Name> nameParts) {
for (int i = 0; i < nameParts.size(); ++i) {
Name namePart = nameParts.get(i);
NamePredicate correspondingPredicate = checkersWithPrefixChecker.get(i);
if (!correspondingPredicate.apply(namePart)) {
if (!correspondingPredicate.test(namePart)) {
return false;
}
}
Expand Down Expand Up @@ -233,9 +232,7 @@ private boolean matches(@NotNull CallableDescriptor callable) {
}

@Override
public boolean apply(@Nullable FunctionDescriptor functionDescriptor) {
assert functionDescriptor != null :
"argument for DescriptorPredicate.apply should not be null, receiverFqName=" + receiverFqName + " names=" + Arrays.asList(names);
public boolean test(FunctionDescriptor functionDescriptor) {
ReceiverParameterDescriptor actualReceiver = functionDescriptor.getExtensionReceiverParameter();
if (actualReceiver != null) {
if (receiverFqName == null) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@

package org.jetbrains.kotlin.js.patterns.typePredicates

import com.google.common.base.Predicate
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.types.KotlinType
import java.util.function.Predicate

public interface TypePredicate : Predicate<KotlinType> {
override fun apply(type: KotlinType?): Boolean
}
interface TypePredicate : Predicate<KotlinType>

private val KOTLIN = TypePredicateImpl("kotlin")
val COMPARABLE: TypePredicate = KOTLIN.inner("Comparable")
Expand All @@ -40,8 +38,8 @@ private class TypePredicateImpl
: TypePredicate {
constructor(name: String) : this(listOf(name))

override fun apply(type: KotlinType?): Boolean {
var descriptor: DeclarationDescriptor? = type?.constructor?.declarationDescriptor ?: return false
override fun test(type: KotlinType): Boolean {
var descriptor: DeclarationDescriptor? = type.constructor.declarationDescriptor ?: return false

for (i in nameParts.lastIndex downTo 0) {
if (nameParts[i] != descriptor?.name?.asString()) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package org.jetbrains.kotlin.js.resolve.diagnostics

import org.jetbrains.kotlin.js.backend.ast.JsFunctionScope
import org.jetbrains.kotlin.js.backend.ast.JsProgram
import org.jetbrains.kotlin.js.backend.ast.JsRootScope
import com.google.gwt.dev.js.parserExceptions.AbortParsingException
import com.google.gwt.dev.js.rhino.CodePosition
import com.google.gwt.dev.js.rhino.ErrorReporter
Expand All @@ -29,6 +26,9 @@ import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory1
import org.jetbrains.kotlin.js.backend.ast.JsFunctionScope
import org.jetbrains.kotlin.js.backend.ast.JsProgram
import org.jetbrains.kotlin.js.backend.ast.JsRootScope
import org.jetbrains.kotlin.js.parser.parse
import org.jetbrains.kotlin.js.patterns.DescriptorPredicate
import org.jetbrains.kotlin.js.patterns.PatternBuilder
Expand Down Expand Up @@ -57,7 +57,7 @@ class JsCallChecker(

@JvmStatic fun <F : CallableDescriptor?> ResolvedCall<F>.isJsCall(): Boolean {
val descriptor = resultingDescriptor
return descriptor is SimpleFunctionDescriptor && JS_PATTERN.apply(descriptor)
return descriptor is SimpleFunctionDescriptor && JS_PATTERN.test(descriptor)
}

@JvmStatic fun extractStringValue(compileTimeConstant: CompileTimeConstant<*>?): String? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.jetbrains.kotlin.js.backend.ast.JsExpression;
import org.jetbrains.kotlin.js.backend.ast.JsInvocation;
import org.jetbrains.kotlin.js.backend.ast.JsLiteral;
import org.jetbrains.kotlin.js.config.JSConfigurationKeys;
import org.jetbrains.kotlin.js.patterns.NamePredicate;
import org.jetbrains.kotlin.js.patterns.typePredicates.TypePredicatesKt;
import org.jetbrains.kotlin.js.translate.context.Namer;
Expand Down Expand Up @@ -222,9 +221,9 @@ private JsExpression getIsTypeCheckCallableForBuiltin(@NotNull KotlinType type)

}

if (TypePredicatesKt.getCHAR_SEQUENCE().apply(type)) return namer().isCharSequence();
if (TypePredicatesKt.getCHAR_SEQUENCE().test(type)) return namer().isCharSequence();

if (TypePredicatesKt.getCOMPARABLE().apply(type)) return namer().isComparable();
if (TypePredicatesKt.getCOMPARABLE().test(type)) return namer().isComparable();

return null;
}
Expand All @@ -233,27 +232,27 @@ private JsExpression getIsTypeCheckCallableForBuiltin(@NotNull KotlinType type)
private JsExpression getIsTypeCheckCallableForPrimitiveBuiltin(@NotNull KotlinType type) {
Name typeName = getNameIfStandardType(type);

if (NamePredicate.STRING.apply(typeName)) {
if (NamePredicate.STRING.test(typeName)) {
return namer().isTypeOf(program().getStringLiteral("string"));
}

if (NamePredicate.BOOLEAN.apply(typeName)) {
if (NamePredicate.BOOLEAN.test(typeName)) {
return namer().isTypeOf(program().getStringLiteral("boolean"));
}

if (NamePredicate.LONG.apply(typeName)) {
if (NamePredicate.LONG.test(typeName)) {
return namer().isInstanceOf(Namer.kotlinLong());
}

if (NamePredicate.NUMBER.apply(typeName)) {
if (NamePredicate.NUMBER.test(typeName)) {
return namer().kotlin(Namer.IS_NUMBER);
}

if (NamePredicate.CHAR.apply(typeName)) {
if (NamePredicate.CHAR.test(typeName)) {
return namer().kotlin(Namer.IS_CHAR);
}

if (NamePredicate.PRIMITIVE_NUMBERS_MAPPED_TO_PRIMITIVE_JS.apply(typeName)) {
if (NamePredicate.PRIMITIVE_NUMBERS_MAPPED_TO_PRIMITIVE_JS.test(typeName)) {
return namer().isTypeOf(program().getStringLiteral("number"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ private boolean mustCallToString(@NotNull KotlinType type) {
Name typeName = DescriptorUtilsKt.getNameIfStandardType(type);
if (typeName != null) {
//TODO: this is a hacky optimization, should use some generic approach
if (NamePredicate.STRING.apply(typeName)) {
if (NamePredicate.STRING.test(typeName)) {
return false;
}
else if (NamePredicate.PRIMITIVE_NUMBERS.apply(typeName)) {
else if (NamePredicate.PRIMITIVE_NUMBERS.test(typeName)) {
return resultingExpression == null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.jetbrains.kotlin.js.translate.intrinsic.functions.factories;

import com.google.common.base.Predicate;
import com.google.common.collect.Lists;
import com.intellij.openapi.util.Pair;
import org.jetbrains.annotations.NotNull;
Expand All @@ -25,6 +24,7 @@
import org.jetbrains.kotlin.js.translate.intrinsic.functions.basic.FunctionIntrinsic;

import java.util.List;
import java.util.function.Predicate;

public abstract class CompositeFIF implements FunctionIntrinsicFactory {
@NotNull
Expand All @@ -37,7 +37,7 @@ protected CompositeFIF() {
@Override
public FunctionIntrinsic getIntrinsic(@NotNull FunctionDescriptor descriptor) {
for (Pair<Predicate<FunctionDescriptor>, FunctionIntrinsic> entry : patternsAndIntrinsics) {
if (entry.first.apply(descriptor)) {
if (entry.first.test(descriptor)) {
return entry.second;
}
}
Expand All @@ -47,4 +47,4 @@ public FunctionIntrinsic getIntrinsic(@NotNull FunctionDescriptor descriptor) {
protected void add(@NotNull Predicate<FunctionDescriptor> pattern, @NotNull FunctionIntrinsic intrinsic) {
patternsAndIntrinsics.add(Pair.create(pattern, intrinsic));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package org.jetbrains.kotlin.js.translate.intrinsic.functions.factories

import org.jetbrains.kotlin.js.backend.ast.JsExpression
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.js.backend.ast.JsExpression
import org.jetbrains.kotlin.js.patterns.PatternBuilder.pattern
import org.jetbrains.kotlin.js.translate.context.Namer
import org.jetbrains.kotlin.js.translate.context.TranslationContext
Expand Down Expand Up @@ -86,15 +86,15 @@ object LongOperationFIF : FunctionIntrinsicFactory {
override fun getIntrinsic(descriptor: FunctionDescriptor): FunctionIntrinsic? {
val operationName = descriptor.name.asString()
return when {
LONG_EQUALS_ANY.apply(descriptor) || LONG_BINARY_OPERATION_LONG.apply(descriptor) || LONG_BIT_SHIFTS.apply(descriptor) ->
LONG_EQUALS_ANY.test(descriptor) || LONG_BINARY_OPERATION_LONG.test(descriptor) || LONG_BIT_SHIFTS.test(descriptor) ->
longBinaryIntrinsics[operationName]
INTEGER_BINARY_OPERATION_LONG.apply(descriptor) ->
INTEGER_BINARY_OPERATION_LONG.test(descriptor) ->
wrapIntrinsicIfPresent(longBinaryIntrinsics[operationName], ::longFromInt, ID())
LONG_BINARY_OPERATION_INTEGER.apply(descriptor) ->
LONG_BINARY_OPERATION_INTEGER.test(descriptor) ->
wrapIntrinsicIfPresent(longBinaryIntrinsics[operationName], ID(), ::longFromInt)
FLOATING_POINT_BINARY_OPERATION_LONG.apply(descriptor) ->
FLOATING_POINT_BINARY_OPERATION_LONG.test(descriptor) ->
wrapIntrinsicIfPresent(floatBinaryIntrinsics[operationName], ID(), { invokeMethod(it, Namer.LONG_TO_NUMBER) })
LONG_BINARY_OPERATION_FLOATING_POINT.apply(descriptor) ->
LONG_BINARY_OPERATION_FLOATING_POINT.test(descriptor) ->
wrapIntrinsicIfPresent(floatBinaryIntrinsics[operationName], { invokeMethod(it, Namer.LONG_TO_NUMBER) }, ID())
else ->
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@

package org.jetbrains.kotlin.js.translate.intrinsic.functions.factories

import com.google.common.base.Predicates
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.js.backend.ast.JsExpression
import org.jetbrains.kotlin.js.patterns.PatternBuilder.pattern
import org.jetbrains.kotlin.js.translate.context.TranslationContext
import org.jetbrains.kotlin.js.translate.intrinsic.functions.basic.FunctionIntrinsicWithReceiverComputed
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils.*
import org.jetbrains.kotlin.utils.identity
import java.util.function.Predicate

object NumberAndCharConversionFIF : CompositeFIF() {
val USE_AS_IS = Predicates.or(
pattern("Int.toInt|toFloat|toDouble"), pattern("Short.toShort|toInt|toFloat|toDouble"),
pattern("Byte.toByte|toShort|toInt|toFloat|toDouble"), pattern("Float|Double.toFloat|toDouble"),
pattern("Long.toLong"), pattern("Char.toChar")
)
val USE_AS_IS: Predicate<FunctionDescriptor> = pattern("Int.toInt|toFloat|toDouble")
.or(pattern("Short.toShort|toInt|toFloat|toDouble"))
.or(pattern("Byte.toByte|toShort|toInt|toFloat|toDouble"))
.or(pattern("Float|Double.toFloat|toDouble"))
.or(pattern("Long.toLong"))
.or(pattern("Char.toChar"))

private val convertOperations: Map<String, ConversionUnaryIntrinsic> =
private val convertOperations: Map<String, ConversionUnaryIntrinsic> =
mapOf(
"Float|Double.toInt" to ConversionUnaryIntrinsic(::toInt32),
"Int|Float|Double.toShort" to ConversionUnaryIntrinsic(::toShort),
Expand Down Expand Up @@ -71,7 +73,7 @@ object NumberAndCharConversionFIF : CompositeFIF() {
}

init {
add(USE_AS_IS!!, ConversionUnaryIntrinsic(identity()))
add(USE_AS_IS, ConversionUnaryIntrinsic(identity()))
for((stringPattern, intrinsic) in convertOperations) {
add(pattern(stringPattern), intrinsic)
}
Expand Down
Loading

0 comments on commit f723ad7

Please sign in to comment.