/*
 * 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,
 * 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.apache.solr.handler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.solr.client.solrj.io.Tuple;
import org.apache.solr.client.solrj.io.stream.StreamContext;
import org.apache.solr.client.solrj.io.stream.expr.Explanation;
import org.apache.solr.client.solrj.io.stream.expr.Explanation.ExpressionType;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParameter;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionValue;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
import org.apache.solr.client.solrj.io.eval.*;

import org.apache.solr.common.SolrException;
import org.apache.lucene.analysis.*;
import org.apache.solr.core.SolrCore;

public class AnalyzeEvaluator extends SimpleEvaluator {
  private static final long serialVersionUID = 1L;

  private String fieldName;
  private String analyzerField;
  private Analyzer analyzer;

  public AnalyzeEvaluator(String _fieldName, String _analyzerField) {
    init(_fieldName, _analyzerField);
  }

  public AnalyzeEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{
    String _fieldName = factory.getValueOperand(expression, 0);
    String _analyzerField = factory.getValueOperand(expression, 1);
    init(_fieldName, _analyzerField);
  }

  public void setStreamContext(StreamContext context) {
    Object solrCoreObj = context.get("solr-core");
    if (solrCoreObj == null || !(solrCoreObj instanceof SolrCore) ) {
      throw new SolrException(SolrException.ErrorCode.INVALID_STATE, "StreamContext must have SolrCore in solr-core key");
    }
    SolrCore solrCore = (SolrCore) solrCoreObj;

    analyzer = solrCore.getLatestSchema().getFieldType(analyzerField).getIndexAnalyzer();
  }

  private void init(String fieldName, String analyzerField) {
    this.fieldName = fieldName;
    if(analyzerField == null) {
      this.analyzerField = fieldName;
    } else {
      this.analyzerField = analyzerField;
    }
  }

  @Override
  public Object evaluate(Tuple tuple) throws IOException {
    String value = tuple.getString(fieldName);
    if(value == null) {
      return null;
    }

    TokenStream tokenStream = analyzer.tokenStream(analyzerField, value);
    CharTermAttribute termAtt = tokenStream.getAttribute(CharTermAttribute.class);
    tokenStream.reset();
    List<String> tokens = new ArrayList();
    while (tokenStream.incrementToken()) {
      tokens.add(termAtt.toString());
    }

    tokenStream.end();
    tokenStream.close();

    return tokens;
  }

  @Override
  public StreamExpressionParameter toExpression(StreamFactory factory) throws IOException {
    return new StreamExpressionValue(fieldName);
  }

  @Override
  public Explanation toExplanation(StreamFactory factory) throws IOException {
    return new Explanation(nodeId.toString())
        .withExpressionType(ExpressionType.EVALUATOR)
        .withImplementingClass(getClass().getName())
        .withExpression(toExpression(factory).toString());
  }

}

@@ -3131,16 +3131,64 @@
  %7D%0A%0A    
+List%3CString%3E tokens = new ArrayList();%0A%0A    try(
 TokenStr
@@ -3247,18 +3247,22 @@
 , value)
-;%0A
+) %7B%0A  
     Char
@@ -3336,24 +3336,26 @@
 class);%0A    
+  
 tokenStream.
@@ -3369,49 +3369,8 @@
 ;%0A  
-  List%3CString%3E tokens = new ArrayList();%0A
     
@@ -3410,24 +3410,26 @@
 ()) %7B%0A      
+  
 tokens.add(t
@@ -3452,23 +3452,26 @@
 ));%0A    
+  
 %7D%0A
-%0A
+ 
     
+ 
 tokenStr
@@ -3489,29 +3489,9 @@
     
-tokenStream.close();%0A
+%7D
 %0A   

Add try-with-resources clause around TokenStream