src/share/classes/com/sun/tools/javac/comp/Check.java

changeset 1588
2620c953e9fe
parent 1579
0baaae675b19
child 1603
6118072811e5
equal deleted inserted replaced
1587:f1f605f85850 1588:2620c953e9fe
1586 // Error if static method overrides instance method (JLS 8.4.6.2). 1586 // Error if static method overrides instance method (JLS 8.4.6.2).
1587 if ((m.flags() & STATIC) != 0 && 1587 if ((m.flags() & STATIC) != 0 &&
1588 (other.flags() & STATIC) == 0) { 1588 (other.flags() & STATIC) == 0) {
1589 log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.static", 1589 log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.static",
1590 cannotOverride(m, other)); 1590 cannotOverride(m, other));
1591 m.flags_field |= BAD_OVERRIDE;
1591 return; 1592 return;
1592 } 1593 }
1593 1594
1594 // Error if instance method overrides static or final 1595 // Error if instance method overrides static or final
1595 // method (JLS 8.4.6.1). 1596 // method (JLS 8.4.6.1).
1597 (m.flags() & STATIC) == 0 && 1598 (m.flags() & STATIC) == 0 &&
1598 (other.flags() & STATIC) != 0) { 1599 (other.flags() & STATIC) != 0) {
1599 log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.meth", 1600 log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.meth",
1600 cannotOverride(m, other), 1601 cannotOverride(m, other),
1601 asFlagSet(other.flags() & (FINAL | STATIC))); 1602 asFlagSet(other.flags() & (FINAL | STATIC)));
1603 m.flags_field |= BAD_OVERRIDE;
1602 return; 1604 return;
1603 } 1605 }
1604 1606
1605 if ((m.owner.flags() & ANNOTATION) != 0) { 1607 if ((m.owner.flags() & ANNOTATION) != 0) {
1606 // handled in validateAnnotationMethod 1608 // handled in validateAnnotationMethod
1613 log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.weaker.access", 1615 log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.weaker.access",
1614 cannotOverride(m, other), 1616 cannotOverride(m, other),
1615 other.flags() == 0 ? 1617 other.flags() == 0 ?
1616 Flag.PACKAGE : 1618 Flag.PACKAGE :
1617 asFlagSet(other.flags() & AccessFlags)); 1619 asFlagSet(other.flags() & AccessFlags));
1620 m.flags_field |= BAD_OVERRIDE;
1618 return; 1621 return;
1619 } 1622 }
1620 1623
1621 Type mt = types.memberType(origin.type, m); 1624 Type mt = types.memberType(origin.type, m);
1622 Type ot = types.memberType(origin.type, other); 1625 Type ot = types.memberType(origin.type, other);
1640 } else { 1643 } else {
1641 log.error(TreeInfo.diagnosticPositionFor(m, tree), 1644 log.error(TreeInfo.diagnosticPositionFor(m, tree),
1642 "override.incompatible.ret", 1645 "override.incompatible.ret",
1643 cannotOverride(m, other), 1646 cannotOverride(m, other),
1644 mtres, otres); 1647 mtres, otres);
1648 m.flags_field |= BAD_OVERRIDE;
1645 return; 1649 return;
1646 } 1650 }
1647 } else if (overrideWarner.hasNonSilentLint(LintCategory.UNCHECKED)) { 1651 } else if (overrideWarner.hasNonSilentLint(LintCategory.UNCHECKED)) {
1648 warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree), 1652 warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree),
1649 "override.unchecked.ret", 1653 "override.unchecked.ret",
1659 if (unhandledErased.nonEmpty()) { 1663 if (unhandledErased.nonEmpty()) {
1660 log.error(TreeInfo.diagnosticPositionFor(m, tree), 1664 log.error(TreeInfo.diagnosticPositionFor(m, tree),
1661 "override.meth.doesnt.throw", 1665 "override.meth.doesnt.throw",
1662 cannotOverride(m, other), 1666 cannotOverride(m, other),
1663 unhandledUnerased.head); 1667 unhandledUnerased.head);
1668 m.flags_field |= BAD_OVERRIDE;
1664 return; 1669 return;
1665 } 1670 }
1666 else if (unhandledUnerased.nonEmpty()) { 1671 else if (unhandledUnerased.nonEmpty()) {
1667 warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree), 1672 warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree),
1668 "override.unchecked.thrown", 1673 "override.unchecked.thrown",
1954 } 1959 }
1955 e = e.next(); 1960 e = e.next();
1956 } 1961 }
1957 } 1962 }
1958 1963
1964 public void checkClassOverrideEqualsAndHash(ClassSymbol someClass) {
1965 if (lint.isEnabled(LintCategory.OVERRIDES)) {
1966 boolean hasEquals = false;
1967 boolean hasHashCode = false;
1968
1969 Scope.Entry equalsAtObject = syms.objectType.tsym.members().lookup(names.equals);
1970 Scope.Entry hashCodeAtObject = syms.objectType.tsym.members().lookup(names.hashCode);
1971 for (Symbol s: someClass.members().getElements(new Filter<Symbol>() {
1972 public boolean accepts(Symbol s) {
1973 return s.kind == Kinds.MTH &&
1974 (s.flags() & BAD_OVERRIDE) == 0;
1975 }
1976 })) {
1977 MethodSymbol m = (MethodSymbol)s;
1978 hasEquals |= m.name.equals(names.equals) &&
1979 m.overrides(equalsAtObject.sym, someClass, types, false);
1980
1981 hasHashCode |= m.name.equals(names.hashCode) &&
1982 m.overrides(hashCodeAtObject.sym, someClass, types, false);
1983 }
1984 if (hasEquals && !hasHashCode) {
1985 log.warning(LintCategory.OVERRIDES, (DiagnosticPosition) null,
1986 "override.equals.but.not.hashcode", someClass.fullname);
1987 }
1988 }
1989 }
1990
1959 private boolean checkNameClash(ClassSymbol origin, Symbol s1, Symbol s2) { 1991 private boolean checkNameClash(ClassSymbol origin, Symbol s1, Symbol s2) {
1960 ClashFilter cf = new ClashFilter(origin.type); 1992 ClashFilter cf = new ClashFilter(origin.type);
1961 return (cf.accepts(s1) && 1993 return (cf.accepts(s1) &&
1962 cf.accepts(s2) && 1994 cf.accepts(s2) &&
1963 types.hasSameArgs(s1.erasure(types), s2.erasure(types))); 1995 types.hasSameArgs(s1.erasure(types), s2.erasure(types)));

mercurial