< prev index next >

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

Print this page

        

@@ -27,10 +27,11 @@
 
 import java.util.*;
 import java.util.function.Supplier;
 
 import javax.lang.model.element.ElementKind;
+import javax.lang.model.element.NestingKind;
 import javax.tools.JavaFileManager;
 
 import com.sun.tools.javac.code.*;
 import com.sun.tools.javac.code.Attribute.Compound;
 import com.sun.tools.javac.code.Directive.ExportsDirective;

@@ -3825,10 +3826,57 @@
             log.warning(pos,
                         Warnings.AuxiliaryClassAccessedFromOutsideOfItsSourceFile(c, c.sourcefile));
         }
     }
 
+    /**
+     * Check for a default constructor in an exported package.
+     */
+    void checkDefaultConstructor(ClassSymbol c, DiagnosticPosition pos) {
+        if (lint.isEnabled(Lint.LintCategory.DEFAULT_CTOR) &&
+            (c.flags() & ENUM) == 0 &&
+            !c.isAnonymous() &&
+            ((c.flags() & PUBLIC) != 0) &&
+            Feature.MODULES.allowedInSource(source)) {
+            NestingKind nestingKind = c.getNestingKind();
+            switch (nestingKind) {
+                case ANONYMOUS,
+                     LOCAL -> {return;}
+                case TOP_LEVEL -> {;} // No additional checks needed
+                case MEMBER -> {
+                    // For nested member classes, all the enclosing
+                    // classes must be public.
+                    Symbol owner = c.owner;
+                    while (owner != null && owner.kind == TYP) {
+                        if ((owner.flags() & PUBLIC) == 0)
+                            return;
+                        owner = owner.owner;
+                    }
+                } 
+            }
+
+            // Only check classes in named packages exported by its module
+            PackageSymbol pkg = c.packge();
+            if (!pkg.isUnnamed()) {
+                ModuleSymbol modle = pkg.modle;
+                for (ExportsDirective exportDir : modle.exports) {
+                    // Report warning only if the containing
+                    // package is unconditionally exported
+                    if (exportDir.packge.equals(pkg)) {
+                        if (exportDir.modules == null || exportDir.modules.isEmpty()) {
+                            log.warning(LintCategory.DEFAULT_CTOR,
+                                        pos, Warnings.DefaultCtor(c, pkg, modle));
+                        } else {
+                            return;
+                        }
+                    }
+                }
+            }
+        }
+        return;
+    }
+
     private class ConversionWarner extends Warner {
         final String uncheckedKey;
         final Type found;
         final Type expected;
         public ConversionWarner(DiagnosticPosition pos, String uncheckedKey, Type found, Type expected) {
< prev index next >