All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.orfjackal.retrolambda.interfaces.WarnAboutDefaultAndStaticMethods Maven / Gradle / Ivy

// Copyright © 2013-2015 Esko Luontola 
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0

package net.orfjackal.retrolambda.interfaces;

import org.objectweb.asm.*;

import static net.orfjackal.retrolambda.util.Flags.*;

public class WarnAboutDefaultAndStaticMethods extends ClassVisitor {

    private String interfaceName;

    public WarnAboutDefaultAndStaticMethods(ClassVisitor next) {
        super(Opcodes.ASM5, next);
    }

    @Override
    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        if (!isInterface(access)) {
            throw new IllegalArgumentException(name + " is not an interface");
        }
        interfaceName = name;
        super.visit(version, access, name, signature, superName, interfaces);
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
        // Not allowed by Java 7 in interfaces:
        // - default methods
        // - static methods
        // - bridge methods
        // Allowed by Java 7:
        // - static initialization blocks (for initializing constants)
        if (isStaticMethod(access)) {
            if (!isStaticInitializer(name, desc, access) &&
                    !name.startsWith("lambda$")) {
                printWarning("a static method", name);
            }
        } else {
            if (!isAbstractMethod(access)) {
                printWarning("a default method", name);
            }
        }
        return super.visitMethod(access, name, desc, signature, exceptions);
    }

    private void printWarning(String methodKind, String methodName) {
        System.out.println("WARNING: The interface " + interfaceName + " has " + methodKind + " \"" + methodName + "\" " +
                "but backporting default methods is not enabled");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy