序言
重写和重载其实是我们刚学习Java时就知道的知识点,看起来很简单,其实深挖起来,真的会有很多不一样的东西。学习的过程不就是在不断探索,不断积累,知识面横向扩展、纵向扩展。个人认为知识是永远学不完的,而且即使学过也可能忘记。但是学过和没学过真的是会有区别,即使有点印象,你也可以凭借印象来找到答案。这也就是为什么有的人在处理问题时会不断产生新的问题,而有的人却能举一反三,轻松找到最优解。
重写(Override)
重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变。即外壳不变,核心重写!
重写我们再熟悉不过了,对实现接口的公有抽象方法进行重写,对继承类的公有方法或抽象方法进行重写等。
方法重写的规则如下:
- 参数列表与被重写方法的参数列表必须完全相同
- 返回类型与被重写方法的返回类型可以不相同,但是必须是父类返回值的派生类(java5 及更早版本返回类型要一样,java7 及更高版本可以不同)
- 访问权限不能比父类中被重写的方法的访问权限更低。例如:如果父类的一个方法被声明为 public,那么在子类中重写该方法就不能声明为 protected
- 父类的成员方法只能被它的子类重写
- 声明为 final 的方法不能被重写
- 声明为 static 的方法不能被重写,但是能够被再次声明
- 子类和父类在同一个包中,那么子类可以重写父类所有方法,除了声明为 private 和 final 的方法
- 子类和父类不在同一个包中,那么子类只能够重写父类的声明为 public 和 protected 的非 final 方法
- 重写的方法能够抛出任何非强制异常,无论被重写的方法是否抛出异常。但是,重写的方法不能抛出新的强制性异常,或者比被重写方法声明的更广泛的强制性异常,反之则可以
- 构造方法不能被重写
- 如果不能继承一个类,则不能重写该类的方法
重载(Overload)
重载(overloading) 是在一个类里面,方法名字相同,而参数不同。返回类型可以相同也可以不同。
重载的规则如下:
- 被重载的方法必须改变参数列表(参数个数或类型不一样);
- 被重载的方法可以改变返回类型;
- 被重载的方法可以改变访问修饰符;
- 被重载的方法可以声明新的或更广的检查异常;
- 方法能够在同一个类中或者在一个子类中被重载。
- 无法以返回值类型作为重载函数的区分标准。
在《深入理解Java虚拟机》中的180页提到,在Java语言中,要重载(Overload)一个方法,除了要与原方法具有相同的简单名称之外,还要求必须拥有一个与原方法不同的特征签名,特征签名就是一个方法中各个参数在常量池中的字段符号引用的集合,也就是因为返回值不会包含在特征签名中,因此Java语言里面是无法仅仅依靠返回值的不同来对一个已有的方法进行重载的。
在《Java虚拟机规范(第2版)》的第四章节Signatures及《Java语言规范》的第八章节Method Signature都分别定义了字节码层面的方法特征签名以及Java代码曾名的方法特征签名。
- Java代码的方法特征签名包括:方法名称、参数顺序及参数类型
- 字节码的特征签名包括:方法名称、参数顺序、参数类型、返回值以及受查异常表
总结
重载和重写比较好区分,重载是在同一个方法需要有不同的入参时使用,而重写是覆写某个方法来达到不同的实现。
但我们阅读代码时会发现,重载并没有需要在重载的方法上加上注解,而重写有的代码加了@Override注解,有的代码则没有。
看Override注解,被修饰的是方法,且它的生命周期是在原文件中有效,被编译器丢弃。
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
那么Override的作用到底是什么呢?除了可读性更高,编译器也会在编译期进行校验覆写的父类方法是否存在。
在编译期元编程阶段(CTMP,Compile-Time MetaProgramming),源代码解析成抽象语法树(AST,Abstract Syntax Tree),从AST提取源程序后翻译并执行,再更新AST再编译成二进制机器代码。这一系列的流程对代码进行检查和优化,具体元编程内容参看文末参考链接2,下图也引用自该文章。
校验和检查使得代码不容易出错,且可读性更强,那么看看Java中是如何做的。找到org.codehaus.groovy.transform.trait包下的TraitASTTransformation类,抽象语法树的转换类如下,可以看到对注解类进行了检查和校验,
@GroovyASTTransformation(phase = CompilePhase.SEMANTIC_ANALYSIS)
public class TraitASTTransformation extends AbstractASTTransformation implements CompilationUnitAware {
public static final String DO_DYNAMIC = TraitReceiverTransformer.class+".doDynamic";
public static final String POST_TYPECHECKING_REPLACEMENT = TraitReceiverTransformer.class+".replacement";
private static final ClassNode INVOKERHELPER_CLASSNODE = ClassHelper.make(InvokerHelper.class);
private static final ClassNode OVERRIDE_CLASSNODE = ClassHelper.make(Override.class);
private SourceUnit unit;
private CompilationUnit compilationUnit;
public void visit(ASTNode[] nodes, SourceUnit source) {
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode anno = (AnnotationNode) nodes[0];
if (!Traits.TRAIT_CLASSNODE.equals(anno.getClassNode())) return;
unit = source;
init(nodes, source);
if (parent instanceof ClassNode) {
ClassNode cNode = (ClassNode) parent;
if (!checkNotInterface(cNode, Traits.TRAIT_TYPE_NAME)) return;
checkNoConstructor(cNode);
checkExtendsClause(cNode);
generateMethodsWithDefaultArgs(cNode);
replaceExtendsByImplements(cNode);
createHelperClass(cNode);
}
}
private void generateMethodsWithDefaultArgs(final ClassNode cNode) {
DefaultArgsMethodsAdder adder = new DefaultArgsMethodsAdder();
adder.addDefaultParameterMethods(cNode);
}
private void checkExtendsClause(final ClassNode cNode) {
ClassNode superClass = cNode.getSuperClass();
if (superClass.isInterface() && !Traits.isTrait(superClass)) {
addError("Trait cannot extend an interface. Use 'implements' instead", cNode);
}
}
private void replaceExtendsByImplements(final ClassNode cNode) {
ClassNode superClass = cNode.getUnresolvedSuperClass();
if (Traits.isTrait(superClass)) {
// move from super class to interface;
cNode.setSuperClass(ClassHelper.OBJECT_TYPE);
cNode.setUnresolvedSuperClass(ClassHelper.OBJECT_TYPE);
cNode.addInterface(superClass);
resolveScope(cNode);
}
}
private void resolveScope(final ClassNode cNode) {
// we need to resolve again!
VariableScopeVisitor scopeVisitor = new VariableScopeVisitor(unit);
scopeVisitor.visitClass(cNode);
}
private void checkNoConstructor(final ClassNode cNode) {
if (!cNode.getDeclaredConstructors().isEmpty()) {
addError("Error processing trait '" + cNode.getName() + "'. " +
" Constructors are not allowed.", cNode);
}
}
private void createHelperClass(final ClassNode cNode) {
ClassNode helper = new InnerClassNode(
cNode,
Traits.helperClassName(cNode),
ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT | ACC_SYNTHETIC,
ClassHelper.OBJECT_TYPE,
ClassNode.EMPTY_ARRAY,
null
);
cNode.setModifiers(ACC_PUBLIC | ACC_INTERFACE | ACC_ABSTRACT);
checkInnerClasses(cNode);
MethodNode initializer = createInitMethod(false, cNode, helper);
MethodNode staticInitializer = createInitMethod(true, cNode, helper);
// apply the verifier to have the property nodes generated
generatePropertyMethods(cNode);
// prepare fields
List<FieldNode> fields = new ArrayList<FieldNode>();
Set<String> fieldNames = new HashSet<String>();
for (FieldNode field : cNode.getFields()) {
if (!"metaClass".equals(field.getName()) && (!field.isSynthetic() || field.getName().indexOf('$') < 0)) {
fields.add(field);
fieldNames.add(field.getName());
}
}
ClassNode fieldHelper = null;
if (!fields.isEmpty()) {
fieldHelper = new InnerClassNode(
cNode,
Traits.fieldHelperClassName(cNode),
ACC_STATIC | ACC_PUBLIC | ACC_INTERFACE | ACC_ABSTRACT,
ClassHelper.OBJECT_TYPE
);
}
// add methods
List<MethodNode> methods = new ArrayList<MethodNode>(cNode.getMethods());
List<MethodNode> nonPublicAPIMethods = new LinkedList<MethodNode>();
for (final MethodNode methodNode : methods) {
boolean declared = methodNode.getDeclaringClass() == cNode;
if (declared) {
if (!methodNode.isSynthetic() && (methodNode.isProtected() || methodNode.getModifiers()==0)) {
unit.addError(new SyntaxException("Cannot have protected/package private method in a trait (" + cNode.getName() + "#" + methodNode.getTypeDescriptor() + ")",
methodNode.getLineNumber(), methodNode.getColumnNumber()));
return;
}
helper.addMethod(processMethod(cNode, methodNode, fieldHelper, fieldNames));
if (methodNode.isPrivate() || methodNode.isStatic()) {
nonPublicAPIMethods.add(methodNode);
}
}
}
// remove methods which should not appear in the trait interface
for (MethodNode privateMethod : nonPublicAPIMethods) {
cNode.removeMethod(privateMethod);
}
// add fields
for (FieldNode field : fields) {
processField(field, initializer, staticInitializer, fieldHelper, cNode, fieldNames);
}
// clear properties to avoid generation of methods
cNode.getProperties().clear();
// copy annotations
copyClassAnnotations(cNode, helper);
fields = new ArrayList<FieldNode>(cNode.getFields()); // reuse the full list of fields
for (FieldNode field : fields) {
cNode.removeField(field.getName());
}
// visit AST xforms
registerASTTranformations(helper);
unit.getAST().addClass(helper);
if (fieldHelper != null) {
unit.getAST().addClass(fieldHelper);
}
// resolve scope (for closures)
resolveScope(helper);
if (fieldHelper!=null) {
resolveScope(fieldHelper);
}
}
private MethodNode createInitMethod(final boolean isStatic, final ClassNode cNode, final ClassNode helper) {
MethodNode initializer = new MethodNode(
isStatic?Traits.STATIC_INIT_METHOD:Traits.INIT_METHOD,
ACC_STATIC | ACC_PUBLIC | ACC_SYNTHETIC,
ClassHelper.VOID_TYPE,
new Parameter[]{createSelfParameter(cNode, isStatic)},
ClassNode.EMPTY_ARRAY,
new BlockStatement()
);
helper.addMethod(initializer);
// Cannot add static compilation of init method because of GROOVY-7217, see example 2 of test case
//AnnotationNode an = new AnnotationNode(TraitComposer.COMPILESTATIC_CLASSNODE);
//initializer.addAnnotation(an);
//cNode.addTransform(StaticCompileTransformation.class, an);
return initializer;
}
private void registerASTTranformations(final ClassNode helper) {
ASTTransformationCollectorCodeVisitor collector = new ASTTransformationCollectorCodeVisitor(
unit, compilationUnit.getTransformLoader()
);
collector.visitClass(helper);
// Perform an additional phase which has to be done *after* type checking
compilationUnit.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() {
@Override
public void call(final SourceUnit source, final GeneratorContext context, final ClassNode classNode) throws CompilationFailedException {
if (classNode==helper) {
PostTypeCheckingExpressionReplacer replacer = new PostTypeCheckingExpressionReplacer(source);
replacer.visitClass(helper);
}
}
}, CompilePhase.INSTRUCTION_SELECTION.getPhaseNumber());
}
/**
* Copies annotation from the trait to the helper, excluding the trait annotation itself
* @param cNode the trait class node
* @param helper the helper class node
*/
private void copyClassAnnotations(final ClassNode cNode, final ClassNode helper) {
List<AnnotationNode> annotations = cNode.getAnnotations();
for (AnnotationNode annotation : annotations) {
if (!annotation.getClassNode().equals(Traits.TRAIT_CLASSNODE)) {
helper.addAnnotation(annotation);
}
}
}
private void checkInnerClasses(final ClassNode cNode) {
Iterator<InnerClassNode> it = cNode.getInnerClasses();
while (it.hasNext()) {
InnerClassNode origin = it.next();
if ((origin.getModifiers() & ACC_STATIC) == 0) {
unit.addError(new SyntaxException("Cannot have non-static inner class inside a trait ("+origin.getName()+")", origin.getLineNumber(), origin.getColumnNumber()));
}
}
}
private void generatePropertyMethods(final ClassNode cNode) {
for (PropertyNode node : cNode.getProperties()) {
processProperty(cNode, node);
}
}
/**
* Mostly copied from the {@link Verifier} class but does *not* generate bytecode
*
* @param cNode
* @param node
*/
private static void processProperty(final ClassNode cNode, PropertyNode node) {
String name = node.getName();
FieldNode field = node.getField();
int propNodeModifiers = node.getModifiers();
String getterName = "get" + Verifier.capitalize(name);
String setterName = "set" + Verifier.capitalize(name);
// GROOVY-3726: clear volatile, transient modifiers so that they don't get applied to methods
if ((propNodeModifiers & Modifier.VOLATILE) != 0) {
propNodeModifiers = propNodeModifiers - Modifier.VOLATILE;
}
if ((propNodeModifiers & Modifier.TRANSIENT) != 0) {
propNodeModifiers = propNodeModifiers - Modifier.TRANSIENT;
}
Statement getterBlock = node.getGetterBlock();
if (getterBlock == null) {
MethodNode getter = cNode.getGetterMethod(getterName);
if (getter == null && ClassHelper.boolean_TYPE == node.getType()) {
String secondGetterName = "is" + Verifier.capitalize(name);
getter = cNode.getGetterMethod(secondGetterName);
}
if (!node.isPrivate() && methodNeedsReplacement(cNode, getter)) {
getterBlock = new ExpressionStatement(new FieldExpression(field));
}
}
Statement setterBlock = node.getSetterBlock();
if (setterBlock == null) {
// 2nd arg false below: though not usual, allow setter with non-void return type
MethodNode setter = cNode.getSetterMethod(setterName, false);
if (!node.isPrivate() &&
(propNodeModifiers & ACC_FINAL) == 0 &&
methodNeedsReplacement(cNode, setter)) {
setterBlock = new ExpressionStatement(
new BinaryExpression(
new FieldExpression(field),
Token.newSymbol(Types.EQUAL, 0, 0),
new VariableExpression("value")
)
);
}
}
if (getterBlock != null) {
MethodNode getter =
new MethodNode(getterName, propNodeModifiers, node.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock);
getter.setSynthetic(true);
cNode.addMethod(getter);
if (ClassHelper.boolean_TYPE == node.getType() || ClassHelper.Boolean_TYPE == node.getType()) {
String secondGetterName = "is" + Verifier.capitalize(name);
MethodNode secondGetter =
new MethodNode(secondGetterName, propNodeModifiers, node.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock);
secondGetter.setSynthetic(true);
cNode.addMethod(secondGetter);
}
}
if (setterBlock != null) {
Parameter[] setterParameterTypes = {new Parameter(node.getType(), "value")};
VariableExpression var = (VariableExpression) ((BinaryExpression) ((ExpressionStatement) setterBlock).getExpression()).getRightExpression();
var.setAccessedVariable(setterParameterTypes[0]);
MethodNode setter =
new MethodNode(setterName, propNodeModifiers, ClassHelper.VOID_TYPE, setterParameterTypes, ClassNode.EMPTY_ARRAY, setterBlock);
setter.setSynthetic(true);
cNode.addMethod(setter);
}
}
private static boolean methodNeedsReplacement(ClassNode classNode, MethodNode m) {
// no method found, we need to replace
if (m == null) return true;
// method is in current class, nothing to be done
if (m.getDeclaringClass() == classNode) return false;
// do not overwrite final
if ((m.getModifiers() & ACC_FINAL) != 0) return false;
return true;
}
private void processField(final FieldNode field, final MethodNode initializer, final MethodNode staticInitializer, final ClassNode fieldHelper, final ClassNode trait, final Set<String> knownFields) {
Expression initialExpression = field.getInitialExpression();
MethodNode selectedMethod = field.isStatic()?staticInitializer:initializer;
if (initialExpression != null) {
VariableExpression thisObject = new VariableExpression(selectedMethod.getParameters()[0]);
ExpressionStatement initCode = new ExpressionStatement(initialExpression);
processBody(thisObject, selectedMethod, initCode, trait, fieldHelper, knownFields);
BlockStatement code = (BlockStatement) selectedMethod.getCode();
MethodCallExpression mce;
if (field.isStatic()) {
mce = new MethodCallExpression(
new ClassExpression(INVOKERHELPER_CLASSNODE),
"invokeStaticMethod",
new ArgumentListExpression(
thisObject,
new ConstantExpression(Traits.helperSetterName(field)),
initCode.getExpression()
)
);
} else {
mce = new MethodCallExpression(
new CastExpression(createReceiverType(field.isStatic(), fieldHelper), thisObject),
Traits.helperSetterName(field),
new CastExpression(field.getOriginType(),initCode.getExpression())
);
}
mce.setImplicitThis(false);
mce.setSourcePosition(initialExpression);
code.addStatement(new ExpressionStatement(mce));
}
// define setter/getter helper methods
fieldHelper.addMethod(
Traits.helperSetterName(field),
ACC_PUBLIC | ACC_ABSTRACT,
field.getOriginType(),
new Parameter[]{new Parameter(field.getOriginType(), "val")},
ClassNode.EMPTY_ARRAY,
null
);
fieldHelper.addMethod(
Traits.helperGetterName(field),
ACC_PUBLIC | ACC_ABSTRACT,
field.getOriginType(),
Parameter.EMPTY_ARRAY,
ClassNode.EMPTY_ARRAY,
null
);
// dummy fields are only used to carry annotations if instance field
// and to differentiate from static fields otherwise
String dummyFieldName = (field.isStatic() ? Traits.STATIC_FIELD_PREFIX : Traits.FIELD_PREFIX) +
(field.isPublic()? Traits.PUBLIC_FIELD_PREFIX : Traits.PRIVATE_FIELD_PREFIX)+
Traits.remappedFieldName(field.getOwner(), field.getName());
FieldNode dummyField = new FieldNode(
dummyFieldName,
ACC_STATIC | ACC_PUBLIC | ACC_FINAL | ACC_SYNTHETIC,
field.getOriginType(),
fieldHelper,
null
);
// copy annotations from field to dummy field
List<AnnotationNode> copied = new LinkedList<AnnotationNode>();
List<AnnotationNode> notCopied = new LinkedList<AnnotationNode>();
GeneralUtils.copyAnnotatedNodeAnnotations(field, copied, notCopied);
dummyField.addAnnotations(copied);
fieldHelper.addField(dummyField);
}
private MethodNode processMethod(ClassNode traitClass, MethodNode methodNode, ClassNode fieldHelper, Collection<String> knownFields) {
Parameter[] initialParams = methodNode.getParameters();
Parameter[] newParams = new Parameter[initialParams.length + 1];
newParams[0] = createSelfParameter(traitClass, methodNode.isStatic());
System.arraycopy(initialParams, 0, newParams, 1, initialParams.length);
final int mod = methodNode.isPrivate()?ACC_PRIVATE:ACC_PUBLIC;
MethodNode mNode = new MethodNode(
methodNode.getName(),
mod | ACC_STATIC,
methodNode.getReturnType(),
newParams,
methodNode.getExceptions(),
processBody(new VariableExpression(newParams[0]), methodNode, methodNode.getCode(), traitClass, fieldHelper, knownFields)
);
mNode.setSourcePosition(methodNode);
mNode.addAnnotations(filterAnnotations(methodNode.getAnnotations()));
mNode.setGenericsTypes(methodNode.getGenericsTypes());
if (methodNode.isAbstract()) {
mNode.setModifiers(ACC_PUBLIC | ACC_ABSTRACT);
} else {
methodNode.addAnnotation(new AnnotationNode(Traits.IMPLEMENTED_CLASSNODE));
}
methodNode.setCode(null);
if (!methodNode.isPrivate() && !methodNode.isStatic()) {
methodNode.setModifiers(ACC_PUBLIC | ACC_ABSTRACT);
}
return mNode;
}
private static List<AnnotationNode> filterAnnotations(List<AnnotationNode> annotations) {
List<AnnotationNode> result = new ArrayList<AnnotationNode>(annotations.size());
for (AnnotationNode annotation : annotations) {
if (!OVERRIDE_CLASSNODE.equals(annotation.getClassNode())) {
result.add(annotation);
}
}
return result;
}
private Parameter createSelfParameter(final ClassNode traitClass, boolean isStatic) {
final ClassNode rawType = traitClass.getPlainNodeReference();
ClassNode type = createReceiverType(isStatic, rawType);
return new Parameter(type, isStatic?Traits.STATIC_THIS_OBJECT:Traits.THIS_OBJECT);
}
private ClassNode createReceiverType(final boolean isStatic, final ClassNode rawType) {
ClassNode type;
if (isStatic) {
// Class<TraitClass>
type = ClassHelper.CLASS_Type.getPlainNodeReference();
type.setGenericsTypes(new GenericsType[]{
new GenericsType(rawType)
});
} else {
// TraitClass
type = rawType;
}
return type;
}
private Statement processBody(VariableExpression thisObject, MethodNode methodNode, Statement code, ClassNode trait, ClassNode fieldHelper, Collection<String> knownFields) {
if (code == null) return null;
NAryOperationRewriter operationRewriter = new NAryOperationRewriter(unit, knownFields);
code.visit(operationRewriter);
SuperCallTraitTransformer superTrn = new SuperCallTraitTransformer(unit);
code.visit(superTrn);
TraitReceiverTransformer trn = new TraitReceiverTransformer(thisObject, unit, trait, fieldHelper, knownFields);
code.visit(trn);
return code;
}
public void setCompilationUnit(final CompilationUnit unit) {
this.compilationUnit = unit;
}
private static class DefaultArgsMethodsAdder extends Verifier {
@Override
public void addDefaultParameterMethods(final ClassNode node) {
setClassNode(node);
super.addDefaultParameterMethods(node);
}
}
private static class PostTypeCheckingExpressionReplacer extends ClassCodeExpressionTransformer {
private final SourceUnit sourceUnit;
private PostTypeCheckingExpressionReplacer(final SourceUnit sourceUnit) {
this.sourceUnit = sourceUnit;
}
@Override
protected SourceUnit getSourceUnit() {
return sourceUnit;
}
@Override
public Expression transform(final Expression exp) {
Expression replacement = exp.getNodeMetaData(TraitASTTransformation.POST_TYPECHECKING_REPLACEMENT);
if (replacement!=null) {
return replacement;
}
return super.transform(exp);
}
}
}
参考链接:
1、Java 重写(Override)与重载(Overload) | 菜鸟教程
2、元编程技术综述_pl_lab_001的博客-CSDN博客
参考书目:
1、《深入理解Java虚拟机——JVM高级特性与最佳实践》第二版