From 4b3a8e757b5688162b3a0fd00d2cc36a70c0c35f Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 3 Dec 2019 20:57:24 +0300 Subject: [PATCH] Reduce size of a Kotlin program that uses lightweight reflection --- .../impl/ClassForNameTransformer.java | 22 ++++++++++------ .../teavm/classlib/impl/ExceptionHelpers.java | 25 +++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 classlib/src/main/java/org/teavm/classlib/impl/ExceptionHelpers.java diff --git a/classlib/src/main/java/org/teavm/classlib/impl/ClassForNameTransformer.java b/classlib/src/main/java/org/teavm/classlib/impl/ClassForNameTransformer.java index c56741f35..bb59d5b2e 100644 --- a/classlib/src/main/java/org/teavm/classlib/impl/ClassForNameTransformer.java +++ b/classlib/src/main/java/org/teavm/classlib/impl/ClassForNameTransformer.java @@ -121,14 +121,22 @@ public class ClassForNameTransformer implements ClassHolderTransformer { representative = program.variableAt(nameRepresentatives[nameIndex]); } else if (constant != null) { if (hierarchy.getClassSource().get(constant) == null || !filterClassName(constant)) { - continue; + InvokeInstruction invokeException = new InvokeInstruction(); + invokeException.setType(InvocationType.SPECIAL); + invokeException.setMethod(new MethodReference(ExceptionHelpers.class, "classNotFound", + Class.class)); + invokeException.setReceiver(program.createVariable()); + invokeException.setLocation(invoke.getLocation()); + invoke.insertPrevious(invokeException); + representative = invokeException.getReceiver(); + } else { + ClassConstantInstruction classConstant = new ClassConstantInstruction(); + classConstant.setConstant(ValueType.object(constant)); + classConstant.setReceiver(program.createVariable()); + classConstant.setLocation(invoke.getLocation()); + invoke.insertPrevious(classConstant); + representative = classConstant.getReceiver(); } - ClassConstantInstruction classConstant = new ClassConstantInstruction(); - classConstant.setConstant(ValueType.object(constant)); - classConstant.setReceiver(program.createVariable()); - classConstant.setLocation(invoke.getLocation()); - invoke.insertPrevious(classConstant); - representative = classConstant.getReceiver(); } else { continue; } diff --git a/classlib/src/main/java/org/teavm/classlib/impl/ExceptionHelpers.java b/classlib/src/main/java/org/teavm/classlib/impl/ExceptionHelpers.java new file mode 100644 index 000000000..8e8ca4543 --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/impl/ExceptionHelpers.java @@ -0,0 +1,25 @@ +/* + * Copyright 2019 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.impl; + +public class ExceptionHelpers { + private ExceptionHelpers() { + } + + public static Class classNotFound() throws ClassNotFoundException { + throw new ClassNotFoundException(); + } +}