Package org.apache.calcite.linq4j.tree
Class ClassDeclarationFinder
- java.lang.Object
-
- org.apache.calcite.linq4j.tree.Shuttle
-
- org.apache.calcite.linq4j.tree.ClassDeclarationFinder
-
- Direct Known Subclasses:
DeterministicCodeOptimizer
public class ClassDeclarationFinder extends Shuttle
Entry point for optimizers that factor ou deterministic expressions to final static fields. Instances of this class should not be reused, so new visitor should be created for optimizing a new expression tree.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<MemberDeclaration>
addedDeclarations
The list of new final static fields to be added to the current class.private Function1<ClassDeclarationFinder,ClassDeclarationFinder>
childFactory
private static Function1<ClassDeclarationFinder,ClassDeclarationFinder>
DEFAULT_CHILD_FACTORY
protected ClassDeclarationFinder
parent
-
Constructor Summary
Constructors Modifier Constructor Description private
ClassDeclarationFinder(Function1<ClassDeclarationFinder,ClassDeclarationFinder> childFactory)
Creates optimizer with no parent.protected
ClassDeclarationFinder(ClassDeclarationFinder parent)
Creates a child optimizer.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ClassDeclarationFinder
create()
Creates visitor that uses default optimizer.static ClassDeclarationFinder
create(java.lang.Class<? extends ClassDeclarationFinder> optimizingClass)
Creates visitor that uses given class as optimizer.static ClassDeclarationFinder
create(Function1<ClassDeclarationFinder,ClassDeclarationFinder> childFactory)
Creates visitor that uses given factory to create optimizers.protected ParameterExpression
findDeclaredExpression(Expression expression)
Finds if there exists ready for reuse declaration for given expression.protected ClassDeclarationFinder
goDeeper()
Creates child visitor.protected boolean
hasField(java.lang.String name)
Verifies if the variable name is already in use.protected boolean
isConstant(java.lang.Iterable<? extends Expression> list)
Verifies if all the expressions in given list are effectively constant.protected boolean
isConstant(Expression expression)
Verifies if the expression is effectively constant.protected void
learnFinalStaticDeclarations(java.util.List<MemberDeclaration> memberDeclarations)
Processes the list of declarations when class expression detected.private static Function1<ClassDeclarationFinder,ClassDeclarationFinder>
newChildCreator(java.lang.Class<? extends ClassDeclarationFinder> optimizingClass)
Creates factory that creates instances of optimizing visitors.protected java.util.List<MemberDeclaration>
optimizeDeclarations(java.util.List<MemberDeclaration> memberDeclarations)
Adds new declarations (e.g.Shuttle
preVisit(ClassDeclaration classDeclaration)
Creates optimizer local to the newly generated class.Shuttle
preVisit(NewExpression newExpression)
Creates optimizer local to the newly generated anonymous class.protected Expression
tryOptimizeNewInstance(NewExpression newExpression)
Optimizesnew Type()
constructs.ClassDeclaration
visit(ClassDeclaration classDeclaration, java.util.List<MemberDeclaration> memberDeclarations)
Expression
visit(NewExpression newExpression, java.util.List<Expression> arguments, java.util.List<MemberDeclaration> memberDeclarations)
-
Methods inherited from class org.apache.calcite.linq4j.tree.Shuttle
preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit
-
-
-
-
Field Detail
-
parent
protected final ClassDeclarationFinder parent
-
addedDeclarations
protected final java.util.List<MemberDeclaration> addedDeclarations
The list of new final static fields to be added to the current class.
-
childFactory
private final Function1<ClassDeclarationFinder,ClassDeclarationFinder> childFactory
-
DEFAULT_CHILD_FACTORY
private static final Function1<ClassDeclarationFinder,ClassDeclarationFinder> DEFAULT_CHILD_FACTORY
-
-
Constructor Detail
-
ClassDeclarationFinder
private ClassDeclarationFinder(Function1<ClassDeclarationFinder,ClassDeclarationFinder> childFactory)
Creates optimizer with no parent.
-
ClassDeclarationFinder
protected ClassDeclarationFinder(ClassDeclarationFinder parent)
Creates a child optimizer. Typically a child is created for each class declaration, so each optimizer collects fields for exactly one class.- Parameters:
parent
- parent optimizer
-
-
Method Detail
-
create
public static ClassDeclarationFinder create()
Creates visitor that uses default optimizer.- Returns:
- optimizing visitor
-
create
public static ClassDeclarationFinder create(java.lang.Class<? extends ClassDeclarationFinder> optimizingClass)
Creates visitor that uses given class as optimizer. The implementation should support (ClassDeclarationFinder
) constructor.- Parameters:
optimizingClass
- class that implements optimizations- Returns:
- optimizing visitor
-
create
public static ClassDeclarationFinder create(Function1<ClassDeclarationFinder,ClassDeclarationFinder> childFactory)
Creates visitor that uses given factory to create optimizers.- Parameters:
childFactory
- factory that creates optimizers- Returns:
- optimizing visitor
-
newChildCreator
private static Function1<ClassDeclarationFinder,ClassDeclarationFinder> newChildCreator(java.lang.Class<? extends ClassDeclarationFinder> optimizingClass)
Creates factory that creates instances of optimizing visitors. The implementation should support (ClassDeclarationFinder
) constructor.- Parameters:
optimizingClass
- class that implements optimizations- Returns:
- factory that creates instances of given classes
-
preVisit
public Shuttle preVisit(NewExpression newExpression)
Creates optimizer local to the newly generated anonymous class.
-
preVisit
public Shuttle preVisit(ClassDeclaration classDeclaration)
Creates optimizer local to the newly generated class.
-
visit
public Expression visit(NewExpression newExpression, java.util.List<Expression> arguments, java.util.List<MemberDeclaration> memberDeclarations)
-
learnFinalStaticDeclarations
protected void learnFinalStaticDeclarations(java.util.List<MemberDeclaration> memberDeclarations)
Processes the list of declarations when class expression detected. Sub-classes might figure out the existing fields for reuse.- Parameters:
memberDeclarations
- list of declarations to process.
-
tryOptimizeNewInstance
protected Expression tryOptimizeNewInstance(NewExpression newExpression)
Optimizesnew Type()
constructs.- Parameters:
newExpression
- expression to optimize- Returns:
- always returns un-optimized expression
-
visit
public ClassDeclaration visit(ClassDeclaration classDeclaration, java.util.List<MemberDeclaration> memberDeclarations)
-
optimizeDeclarations
protected java.util.List<MemberDeclaration> optimizeDeclarations(java.util.List<MemberDeclaration> memberDeclarations)
Adds new declarations (e.g. final static fields) to the list of existing ones.- Parameters:
memberDeclarations
- existing list of declarations- Returns:
- new list of declarations or the same if no modifications required
-
isConstant
protected boolean isConstant(Expression expression)
Verifies if the expression is effectively constant. This method should be overridden in sub-classes.- Parameters:
expression
- expression to test- Returns:
- always returns false
-
isConstant
protected boolean isConstant(java.lang.Iterable<? extends Expression> list)
Verifies if all the expressions in given list are effectively constant.- Parameters:
list
- list of expressions to test- Returns:
- true when all the expressions are known to be constant
-
findDeclaredExpression
protected ParameterExpression findDeclaredExpression(Expression expression)
Finds if there exists ready for reuse declaration for given expression. This method should be overridden in sub-classes.- Parameters:
expression
- input expression- Returns:
- always returns null
-
hasField
protected boolean hasField(java.lang.String name)
Verifies if the variable name is already in use. This method should be overridden in sub-classes.- Parameters:
name
- name of the variable to test- Returns:
- always returns false
-
goDeeper
protected ClassDeclarationFinder goDeeper()
Creates child visitor. It is used to traverse nested class declarations.- Returns:
- new
Visitor
that is used to optimize class declarations
-
-