2021SC@SDUSC
目录
_ObjectBuilderSettingEvaluator.java
GetOptionalTemplateMethod.java
AliasTemplateDateFormatFactory.java
_ObjectBuilderSettingEvaluator.java
源码分析
/*
* 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 freemarker.core;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import freemarker.cache.AndMatcher;
import freemarker.cache.ConditionalTemplateConfigurationFactory;
import freemarker.cache.FileExtensionMatcher;
import freemarker.cache.FileNameGlobMatcher;
import freemarker.cache.FirstMatchTemplateConfigurationFactory;
import freemarker.cache.MergingTemplateConfigurationFactory;
import freemarker.cache.NotMatcher;
import freemarker.cache.OrMatcher;
import freemarker.cache.PathGlobMatcher;
import freemarker.cache.PathRegexMatcher;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.SimpleObjectWrapper;
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.Version;
import freemarker.template.utility.ClassUtil;
import freemarker.template.utility.StringUtil;
import freemarker.template.utility.WriteProtectable;
/**
* Don't use this; used internally by FreeMarker, might changes without notice.
*
* Evaluates object builder expressions used in configuration {@link Properties}.
* It should be replaced with FTL later (when it was improved to be practical for this), so the syntax should be
* a subset of the future FTL syntax. This is also why this syntax is restrictive; it shouldn't accept anything that
* FTL will not.
*/
// Java 5: use generics for expectedClass
// Java 5: Introduce ObjectBuilder interface
public class _ObjectBuilderSettingEvaluator {
private static final String INSTANCE_FIELD_NAME = "INSTANCE";
private static final String BUILD_METHOD_NAME = "build";
private static final String BUILDER_CLASS_POSTFIX = "Builder";
private static Map<String,String> SHORTHANDS;
private static final Object VOID = new Object();
private final String src;
private final Class expectedClass;
private final boolean allowNull;
private final _SettingEvaluationEnvironment env;
private int pos;
// 解析结果
private boolean modernMode = false;
private _ObjectBuilderSettingEvaluator(
String src, int pos, Class expectedClass, boolean allowNull, _SettingEvaluationEnvironment env) {
this.src = src;
this.pos = pos;
this.expectedClass = expectedClass;
this.allowNull = allowNull;
this.env = env;
}
public static Object eval(String src, Class expectedClass, boolean allowNull, _SettingEvaluationEnvironment env)
throws _ObjectBuilderSettingEvaluationException,
ClassNotFoundException, InstantiationException, IllegalAccessException {
return new _ObjectBuilderSettingEvaluator(src, 0, expectedClass, allowNull, env).eval();
}
//用于从现有字符串中获取一个设置赋值列表(如{@code (x=1, y=2)}),并将其应用于一个已经存在的bean。
public static int configureBean(
String argumentListSrc, int posAfterOpenParen, Object bean, _SettingEvaluationEnvironment env)
throws _ObjectBuilderSettingEvaluationException,
ClassNotFoundException, InstantiationException, IllegalAccessException {
return new _ObjectBuilderSettingEvaluator(
argumentListSrc, posAfterOpenParen, bean.getClass(), true, env).configureBean(bean);
}
private Object eval() throws _ObjectBuilderSettingEvaluationException,
ClassNotFoundException, InstantiationException, IllegalAccessException {
Object value;
skipWS();
try {
value = ensureEvaled(fetchValue(false, true, false, true));
} catch (LegacyExceptionWrapperSettingEvaluationExpression e) {
e.rethrowLegacy();
value = null; // newer reached
}
skipWS();
if (pos != src.length()) {
throw new _ObjectBuilderSettingEvaluationException("end-of-expression", src, pos);
}
if (value == null && !allowNull) {
throw new _ObjectBuilderSettingEvaluationException("Value can't be null.");
}
if (value != null && !expectedClass.isInstance(value)) {
throw new _ObjectBuilderSettingEvaluationException("The resulting object (of class "
+ value.getClass() + ") is not a(n) " + expectedClass.getName() + ".");
}
return value;
}
private int configureBean(Object bean) throws _ObjectBuilderSettingEvaluationException,
ClassNotFoundException, InstantiationException, IllegalAccessException {
final PropertyAssignmentsExpression propAssignments = new PropertyAssignmentsExpression(bean);
fetchParameterListInto(propAssignments);
skipWS();
propAssignments.eval();
return pos;
}
private Object ensureEvaled(Object value) throws _ObjectBuilderSettingEvaluationException {
return value instanceof SettingExpression ? ((SettingExpression) value).eval() : value;
}
private Object fetchBuilderCall(boolean optional, boolean topLevel)
throws _ObjectBuilderSettingEvaluationException {
int startPos = pos;
BuilderCallExpression exp = new BuilderCallExpression();
// 我们需要canBeStaticField/ mustbestaticfield复杂性来处理括号的遗留语法
exp.canBeStaticField = true;
final String fetchedClassName = fetchClassName(optional);
{
if (fetchedClassName == null) {
if (!optional) {
throw new _ObjectBuilderSettingEvaluationException("class name", src, pos);
}
return VOID;
}
exp.className = shorthandToFullQualified(fetchedClassName);
if (!fetchedClassName.equals(exp.className)) {
// Before 2.3.21 only full-qualified class names were allowed
modernMode = true;
exp.canBeStaticField = false;
}
}
skipWS();
char openParen = fetchOptionalChar("(");
// Only the top-level expression can omit the "(...)"
if (openParen == 0 && !topLevel) {
if (fetchedClassName.indexOf('.') != -1) {
exp.mustBeStaticField = true;
} else {
pos = startPos;
return VOID;
}
}
if (openParen != 0) {
fetchParameterListInto(exp);
exp.canBeStaticField = false;
}
return exp;
}
private void fetchParameterListInto(ExpressionWithParameters exp) throws _ObjectBuilderSettingEvaluationException {
// Before 2.3.21 there was no parameter list
modernMode = true;
skipWS();
if (fetchOptionalChar(")") != ')') {
do {
skipWS();
Object paramNameOrValue = fetchValue(false, false, true, false);
if (paramNameOrValue != VOID) {
skipWS();
if (paramNameOrValue instanceof Name) {
exp.namedParamNames.add(((Name) paramNameOrValue).name);
skipWS();
fetchRequiredChar("=");
skipWS();
Object paramValue = fetchValue(false, false, true, true);
exp.namedParamValues.add(ensureEvaled(paramValue));
} else {
if (!exp.namedParamNames.isEmpty()) {
throw new _ObjectBuilderSettingEvaluationException(
"Positional parameters must precede named parameters");
}
if (!exp.getAllowPositionalParameters()) {
throw new _ObjectBuilderSettingEvaluationException(
"Positional parameters not supported here");
}
exp.positionalParamValues.add(ensureEvaled(paramNameOrValue));
}
skipWS();
}
} while (fetchRequiredChar(",)") == ',');
}
}
private Object fetchValue(boolean optional, boolean topLevel, boolean resultCoerced, boolean resolveVariables)
throws _ObjectBuilderSettingEvaluationException {
if (pos < src.length()) {
Object val = fetchNumberLike(true, resultCoerced);
if (val != VOID) {
return val;
}
val = fetchStringLiteral(true);
if (val != VOID) {
return val;
}
val = fetchListLiteral(true);
if (val != VOID) {
return val;
}
val = fetchMapLiteral(true);
if (val != VOID) {
return val;
}
val = fetchBuilderCall(true, topLevel);
if (val != VOID) {
return val;
}
String name = fetchSimpleName(true);
if (name != null) {
val = keywordToValueOrVoid(name);
if (val != VOID) {
return val;
}
if (resolveVariables) {
// Not supported currently...
throw new _ObjectBuilderSettingEvaluationException("Can't resolve variable reference: " + name);
} else {
return new Name(name);
}
}
}
if (optional) {
return VOID;
} else {
throw new _ObjectBuilderSettingEvaluationException("value or name", src, pos);
}
}
private boolean isKeyword(String name) {
return keywordToValueOrVoid(name) != VOID;
}
private Object keywordToValueOrVoid(String name) {
if (name.equals("true")) return Boolean.TRUE;
if (name.equals("false")) return Boolean.FALSE;
if (name.equals("null")) return null;
return VOID;
}
private