001    /*
002     * JastAddJ is covered by the modified BSD License. You should have received
003     * a copy of the modified BSD license with this compiler.
004     * 
005     * Copyright (c) 2011, Jesper Öqvist <jesper.oqvist@cs.lth.se>
006     * All rights reserved.
007     */
008    
009    /**
010     * The @Override annotation was changed in Java 6.
011     *
012     * The specification of the @Override annotation since Java 6:
013     *
014     * "If a method declaration is annotated with the annotation @Override,
015     * but the method does not override or implement a method declared in a
016     * supertype, or is not override-equivalent to a public method of Object,
017     * a compile-time error will occur.
018     */
019    aspect Annotations {
020        refine Annotations
021        public void Annotation.checkOverride() {
022                if (decl().fullName().equals("java.lang.Override") &&
023                                enclosingBodyDecl() instanceof MethodDecl) {
024    
025                        MethodDecl method = (MethodDecl)enclosingBodyDecl();
026                        TypeDecl host = method.hostType();
027                        SimpleSet ancestors = host.ancestorMethods(method.signature());
028                        boolean found = false;
029                        for (Iterator iter = ancestors.iterator(); iter.hasNext(); ) {
030                                MethodDecl decl = (MethodDecl)iter.next();
031                                if (method.overrides(decl)) {
032                                        found = true;
033                                        break;
034                                }
035                        }
036                        if (!found) {
037                                TypeDecl typeObject = lookupType("java.lang", "Object");
038                                SimpleSet overrides =
039                                        typeObject.localMethodsSignature(method.signature());
040                                if (overrides.isEmpty() ||
041                                                !((MethodDecl) overrides.iterator().next()).isPublic())
042                                        error("method does not override a method from a supertype");
043                        }
044                }
045        }
046    }