public class DeterministicCodeOptimizer extends ClassDeclarationFinder
| Modifier and Type | Field and Description |
|---|---|
protected Map<Expression,Boolean> |
constants
The map contains known to be effectively-final expression.
|
protected Map<Expression,ParameterExpression> |
dedup
The map that de-duplicates expressions, so the same expressions may reuse
the same final static fields.
|
private static Set<Class> |
DETERMINISTIC_CLASSES |
private static String |
FIELD_PREFIX |
protected Map<String,ParameterExpression> |
fieldsByName
The map of all the added final static fields.
|
private static Pattern |
NON_ASCII |
private static Pattern |
PREFIX_PATTERN |
addedDeclarations, parent| Constructor and Description |
|---|
DeterministicCodeOptimizer(ClassDeclarationFinder parent)
Creates a child optimizer.
|
| Modifier and Type | Method and Description |
|---|---|
protected boolean |
allMethodsDeterministic(Class klass)
Checks if all the methods in given class are deterministic (i.e.
|
protected Expression |
createField(Expression expression)
Creates final static field to hold the given expression.
|
protected ParameterExpression |
findDeclaredExpression(Expression expression)
Finds if there exists ready for reuse declaration for given expression.
|
private <C> Constructor<C> |
getConstructor(Class<C> klass) |
protected DeterministicCodeOptimizer |
goDeeper()
Creates child visitor.
|
protected boolean |
hasField(String name)
Verifies if the variable name is already in use.
|
protected String |
inventFieldName(Expression expression)
Generates field name to store given expression.
|
protected boolean |
isConstant(Expression expression)
Verifies if the expression is effectively constant.
|
protected boolean |
isConstructorDeterministic(NewExpression newExpression)
Checks if new instance creation can be reused.
|
protected boolean |
isMethodDeterministic(Method method)
Checks if given method is deterministic (i.e.
|
protected void |
learnFinalStaticDeclarations(List<MemberDeclaration> memberDeclarations)
Processes the list of declarations and learns final static ones as
effectively constant.
|
protected Expression |
tryOptimizeMethodCall(MethodCallExpression methodCallExpression)
Optimized method call, possibly converting it to final static field.
|
protected Expression |
tryOptimizeNewInstance(NewExpression newExpression)
Optimizes
new Type() constructs, |
Expression |
visit(BinaryExpression binaryExpression,
Expression expression0,
Expression expression1) |
MemberDeclaration |
visit(FieldDeclaration fieldDeclaration,
Expression initializer) |
Expression |
visit(MemberExpression memberExpression,
Expression expression) |
Expression |
visit(MethodCallExpression methodCallExpression,
Expression targetExpression,
List<Expression> expressions) |
Expression |
visit(TernaryExpression ternaryExpression,
Expression expression0,
Expression expression1,
Expression expression2) |
Expression |
visit(TypeBinaryExpression typeBinaryExpression,
Expression expression) |
Expression |
visit(UnaryExpression unaryExpression,
Expression expression) |
create, create, create, isConstant, optimizeDeclarations, preVisit, preVisit, visit, visitpreVisit, preVisit, 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, visitprotected final Map<Expression,Boolean> constants
ParameterExpression, however there might be
non-factored to final field expression that is known to be constant.
For instance, cast expression will not be factored to a field,
but we still need to track its constant status.protected final Map<Expression,ParameterExpression> dedup
protected final Map<String,ParameterExpression> fieldsByName
private static final Pattern NON_ASCII
private static final String FIELD_PREFIX
private static final Pattern PREFIX_PATTERN
public DeterministicCodeOptimizer(ClassDeclarationFinder parent)
parent - parent optimizerprotected Expression tryOptimizeNewInstance(NewExpression newExpression)
new Type() constructs,tryOptimizeNewInstance in class ClassDeclarationFindernewExpression - expression to optimizepublic Expression visit(BinaryExpression binaryExpression, Expression expression0, Expression expression1)
public Expression visit(TernaryExpression ternaryExpression, Expression expression0, Expression expression1, Expression expression2)
public Expression visit(UnaryExpression unaryExpression, Expression expression)
public Expression visit(TypeBinaryExpression typeBinaryExpression, Expression expression)
protected Expression tryOptimizeMethodCall(MethodCallExpression methodCallExpression)
methodCallExpression - method call to optimizepublic Expression visit(MethodCallExpression methodCallExpression, Expression targetExpression, List<Expression> expressions)
public Expression visit(MemberExpression memberExpression, Expression expression)
public MemberDeclaration visit(FieldDeclaration fieldDeclaration, Expression initializer)
protected void learnFinalStaticDeclarations(List<MemberDeclaration> memberDeclarations)
learnFinalStaticDeclarations in class ClassDeclarationFindermemberDeclarations - list of declarations to search finals fromprotected ParameterExpression findDeclaredExpression(Expression expression)
findDeclaredExpression in class ClassDeclarationFinderexpression - input expressionprotected Expression createField(Expression expression)
expression - expression to store in final fieldprotected String inventFieldName(Expression expression)
"_$L4J$C$" suffix is
added to avoid conflicts with other variables.
When multiple variables are mangled to the same name,
counter is used to avoid conflicts.expression - input expressionprotected boolean isConstant(Expression expression)
ConstantExpression or
ParameterExpression).
The method verifies parent chain since the expression might be defined
in enclosing class.isConstant in class ClassDeclarationFinderexpression - expression to testprotected boolean isMethodDeterministic(Method method)
method - method to testprotected boolean isConstructorDeterministic(NewExpression newExpression)
new
BigInteger("42") is effectively final and can be reused.newExpression - method to testprivate <C> Constructor<C> getConstructor(Class<C> klass)
protected boolean allMethodsDeterministic(Class klass)
klass - class to testprotected boolean hasField(String name)
fieldsByName are
verified. The method verifies parent chain.hasField in class ClassDeclarationFindername - name of the variable to testprotected DeterministicCodeOptimizer goDeeper()
goDeeper in class ClassDeclarationFinderCopyright © 2012–2023 The Apache Software Foundation. All rights reserved.