001/*
002 * Copyright (c) 2007-2022 The Cascading Authors. All Rights Reserved.
003 *
004 * Project and contact information: https://cascading.wensel.net/
005 *
006 * This file is part of the Cascading project.
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020
021package cascading.operation.expression;
022
023import java.beans.ConstructorProperties;
024import java.io.IOException;
025import java.io.StringReader;
026
027import cascading.operation.OperationException;
028import cascading.tuple.Fields;
029import org.codehaus.commons.compiler.CompileException;
030import org.codehaus.janino.ExpressionEvaluator;
031import org.codehaus.janino.Scanner;
032
033import static cascading.tuple.coerce.Coercions.asClass;
034
035/**
036 * Class ExpressionOperation is the base class for {@link ExpressionFunction}, {@link ExpressionFilter},
037 * {@link cascading.operation.assertion.AssertExpression}.
038 */
039public class ExpressionOperation extends ScriptOperation
040  {
041  @ConstructorProperties({"expression"})
042  protected ExpressionOperation( String expression )
043    {
044    super( ANY, expression, Boolean.class );
045    }
046
047  @ConstructorProperties({"fieldDeclaration", "expression"})
048  protected ExpressionOperation( Fields fieldDeclaration, String expression )
049    {
050    super( ANY, fieldDeclaration, expression, asClass( fieldDeclaration.getType( 0 ) ) );
051    }
052
053  @ConstructorProperties({"fieldDeclaration", "expression", "parameterType"})
054  protected ExpressionOperation( Fields fieldDeclaration, String expression, Class parameterType )
055    {
056    super( 1, fieldDeclaration, expression, asClass( fieldDeclaration.getType( 0 ) ), null,
057      new Class[]{parameterType} );
058    }
059
060  @ConstructorProperties({"fieldDeclaration", "expression", "parameterNames", "parameterTypes"})
061  protected ExpressionOperation( Fields fieldDeclaration, String expression, String[] parameterNames, Class[] parameterTypes )
062    {
063    super( parameterTypes.length, fieldDeclaration, expression, asClass( fieldDeclaration.getType( 0 ) ), parameterNames, parameterTypes );
064    }
065
066  @ConstructorProperties({"expression", "parameterType"})
067  protected ExpressionOperation( String expression, Class parameterType )
068    {
069    super( 1, expression, Object.class, null, new Class[]{parameterType} );
070    }
071
072  @ConstructorProperties({"expression", "parameterNames", "parameterTypes"})
073  protected ExpressionOperation( String expression, String[] parameterNames, Class[] parameterTypes )
074    {
075    super( parameterTypes.length, expression, Object.class, parameterNames, parameterTypes );
076    }
077
078  public String getExpression()
079    {
080    return getBlock();
081    }
082
083  protected String[] guessParameterNames() throws CompileException, IOException
084    {
085    return ExpressionEvaluator.guessParameterNames( new Scanner( "expressionEval", new StringReader( block ) ) );
086    }
087
088  @Override
089  protected Evaluator getEvaluator( Class returnType, String[] parameterNames, Class[] parameterTypes )
090    {
091    try
092      {
093      ExpressionEvaluator evaluator = new ExpressionEvaluator();
094      evaluator.setReturnType( returnType );
095      evaluator.setParameters( parameterNames, parameterTypes );
096      evaluator.setExtendedClass( getExtendedClass() );
097      evaluator.cook( block );
098
099      return evaluator::evaluate;
100      }
101    catch( CompileException exception )
102      {
103      throw new OperationException( "could not compile expression: " + block, exception );
104      }
105    }
106  }