* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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,
 * See the License for the specific language governing permissions and
 * limitations under the License.

package org.apache.zeppelin.kotlin.reflect;

import org.jetbrains.kotlin.cli.common.repl.AggregatedReplStageState;
import org.jetbrains.kotlin.cli.common.repl.ReplHistoryRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import kotlin.Pair;
import kotlin.reflect.KFunction;
import kotlin.reflect.KProperty;
import kotlin.reflect.jvm.ReflectJvmMapping;

 * ContextUpdater updates current user-defined functions and variables
 * to use in completion and KotlinContext.
public class ContextUpdater {
  private static final Logger logger = LoggerFactory.getLogger(ContextUpdater.class);
  private static final Set<Method> objectMethods =
      new HashSet<>(Arrays.asList(Object.class.getMethods()));
  private AggregatedReplStageState<?, ?> state;
  private Map<String, KotlinVariableInfo> vars;
  private Set<KotlinFunctionInfo> functions;

  public ContextUpdater(AggregatedReplStageState<?, ?> state,
                        Map<String, KotlinVariableInfo> vars, 
                        Set<KotlinFunctionInfo> functions) {
    this.state = state;
    this.vars = vars;
    this.functions = functions;

  public void update() {
    try {
      List<Object> lines = getLines();
    } catch (ReflectiveOperationException | NullPointerException e) {
      logger.error("Exception updating current variables", e);

  private void refreshMethods(List<Object> lines) {
    for (Object line : lines) {
      Method[] methods = line.getClass().getMethods();
      for (Method method : methods) {
        if (objectMethods.contains(method) || method.getName().equals("main")) {
        KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
        if (function == null) {
        functions.add(new KotlinFunctionInfo(function));

  private List<Object> getLines() {
    List<Object> lines = state.getHistory().stream()

    return lines;

    ReplHistoryRecord class holds a pair of line ID and a compiled Line_N class.
    This Line class is generated by Kotlin REPL compiler, its base class is Object
    and it contains the functions and variables declared in the paragraph as its
    methods and fields. For example, line "val i = 1" can be compiled to Line_0.class
    with field "public final int i" set to 1.
    Note that when a new value is reassigned to variable in a separate paragraph,
    REPL compiler does not change the value in a previously created field,
    instead it creates a field with the same name in the new Line_(N+1) class.
  private Object getLineFromRecord(ReplHistoryRecord<? extends Pair<?, ?>> record) {
    Object statePair = record.getItem().getSecond();
    return ((Pair<?, ?>) statePair).getSecond();

  private Object getImplicitReceiver(Object script)
      throws ReflectiveOperationException {
    Field receiverField = script.getClass().getDeclaredField("$$implicitReceiver0");
    return receiverField.get(script);

  private void refreshVariables(List<Object> lines) throws ReflectiveOperationException {
    if (!lines.isEmpty()) {
      Object receiver = getImplicitReceiver(lines.get(0));
    for (Object line : lines) {

  // For lines, we only want fields from top level class
  private void findLineVariables(Object line) throws IllegalAccessException {
    Field[] fields = line.getClass().getDeclaredFields();
    findVariables(fields, line);

  // For implicit receiver, we want to also get fields in parent classes
  private void findReceiverVariables(Object receiver) throws IllegalAccessException {
    List<Field> fieldsList = new ArrayList<>();
    for (Class<?> cl = receiver.getClass(); cl != null; cl = cl.getSuperclass()) {
    findVariables(fieldsList.toArray(new Field[0]), receiver);

  private void findVariables(Field[] fields, Object o) throws IllegalAccessException {
    for (Field field : fields) {
      String fieldName = field.getName();
      if (fieldName.contains("$$implicitReceiver")) {

      Object value = field.get(o);
      if (!fieldName.contains("script$")) {
        KProperty<?> descriptor = ReflectJvmMapping.getKotlinProperty(field);
        if (descriptor != null) {
          vars.putIfAbsent(fieldName, new KotlinVariableInfo(value, descriptor));