< prev index next >

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

Print this page




  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.comp;
  27 
  28 import java.util.*;
  29 import java.util.function.Supplier;
  30 
  31 import javax.lang.model.element.ElementKind;

  32 import javax.tools.JavaFileManager;
  33 
  34 import com.sun.tools.javac.code.*;
  35 import com.sun.tools.javac.code.Attribute.Compound;
  36 import com.sun.tools.javac.code.Directive.ExportsDirective;
  37 import com.sun.tools.javac.code.Directive.RequiresDirective;
  38 import com.sun.tools.javac.code.Source.Feature;
  39 import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata;
  40 import com.sun.tools.javac.jvm.*;
  41 import com.sun.tools.javac.resources.CompilerProperties.Errors;
  42 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  43 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
  44 import com.sun.tools.javac.tree.*;
  45 import com.sun.tools.javac.util.*;
  46 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
  47 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
  48 import com.sun.tools.javac.util.JCDiagnostic.Error;
  49 import com.sun.tools.javac.util.JCDiagnostic.Fragment;
  50 import com.sun.tools.javac.util.JCDiagnostic.Warning;
  51 import com.sun.tools.javac.util.List;


3808             while (tree.hasTag(SELECT)) {
3809                 JCFieldAccess s = (JCFieldAccess) tree;
3810                 if (s.sym.owner.getQualifiedName() != TreeInfo.symbol(s.selected).getQualifiedName())
3811                     return false;
3812                 tree = s.selected;
3813             }
3814             return true;
3815         }
3816 
3817     /** Check that an auxiliary class is not accessed from any other file than its own.
3818      */
3819     void checkForBadAuxiliaryClassAccess(DiagnosticPosition pos, Env<AttrContext> env, ClassSymbol c) {
3820         if (lint.isEnabled(Lint.LintCategory.AUXILIARYCLASS) &&
3821             (c.flags() & AUXILIARY) != 0 &&
3822             rs.isAccessible(env, c) &&
3823             !fileManager.isSameFile(c.sourcefile, env.toplevel.sourcefile))
3824         {
3825             log.warning(pos,
3826                         Warnings.AuxiliaryClassAccessedFromOutsideOfItsSourceFile(c, c.sourcefile));
3827         }





















































3828     }
3829 
3830     private class ConversionWarner extends Warner {
3831         final String uncheckedKey;
3832         final Type found;
3833         final Type expected;
3834         public ConversionWarner(DiagnosticPosition pos, String uncheckedKey, Type found, Type expected) {
3835             super(pos);
3836             this.uncheckedKey = uncheckedKey;
3837             this.found = found;
3838             this.expected = expected;
3839         }
3840 
3841         @Override
3842         public void warn(LintCategory lint) {
3843             boolean warned = this.warned;
3844             super.warn(lint);
3845             if (warned) return; // suppress redundant diagnostics
3846             switch (lint) {
3847                 case UNCHECKED:




  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.comp;
  27 
  28 import java.util.*;
  29 import java.util.function.Supplier;
  30 
  31 import javax.lang.model.element.ElementKind;
  32 import javax.lang.model.element.NestingKind;
  33 import javax.tools.JavaFileManager;
  34 
  35 import com.sun.tools.javac.code.*;
  36 import com.sun.tools.javac.code.Attribute.Compound;
  37 import com.sun.tools.javac.code.Directive.ExportsDirective;
  38 import com.sun.tools.javac.code.Directive.RequiresDirective;
  39 import com.sun.tools.javac.code.Source.Feature;
  40 import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata;
  41 import com.sun.tools.javac.jvm.*;
  42 import com.sun.tools.javac.resources.CompilerProperties.Errors;
  43 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  44 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
  45 import com.sun.tools.javac.tree.*;
  46 import com.sun.tools.javac.util.*;
  47 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
  48 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
  49 import com.sun.tools.javac.util.JCDiagnostic.Error;
  50 import com.sun.tools.javac.util.JCDiagnostic.Fragment;
  51 import com.sun.tools.javac.util.JCDiagnostic.Warning;
  52 import com.sun.tools.javac.util.List;


3809             while (tree.hasTag(SELECT)) {
3810                 JCFieldAccess s = (JCFieldAccess) tree;
3811                 if (s.sym.owner.getQualifiedName() != TreeInfo.symbol(s.selected).getQualifiedName())
3812                     return false;
3813                 tree = s.selected;
3814             }
3815             return true;
3816         }
3817 
3818     /** Check that an auxiliary class is not accessed from any other file than its own.
3819      */
3820     void checkForBadAuxiliaryClassAccess(DiagnosticPosition pos, Env<AttrContext> env, ClassSymbol c) {
3821         if (lint.isEnabled(Lint.LintCategory.AUXILIARYCLASS) &&
3822             (c.flags() & AUXILIARY) != 0 &&
3823             rs.isAccessible(env, c) &&
3824             !fileManager.isSameFile(c.sourcefile, env.toplevel.sourcefile))
3825         {
3826             log.warning(pos,
3827                         Warnings.AuxiliaryClassAccessedFromOutsideOfItsSourceFile(c, c.sourcefile));
3828         }
3829     }
3830 
3831     /**
3832      * Check for a default constructor in an exported package.
3833      */
3834     void checkDefaultConstructor(ClassSymbol c, DiagnosticPosition pos) {
3835         if (lint.isEnabled(LintCategory.MISSING_DECLARED_CTOR) &&
3836             ((c.flags() & (ENUM | RECORD)) == 0) &&
3837             !c.isAnonymous() &&
3838             ((c.flags() & PUBLIC) != 0) &&
3839             Feature.MODULES.allowedInSource(source)) {
3840             NestingKind nestingKind = c.getNestingKind();
3841             switch (nestingKind) {
3842                 case ANONYMOUS,
3843                      LOCAL -> {return;}
3844                 case TOP_LEVEL -> {;} // No additional checks needed
3845                 case MEMBER -> {
3846                     // For nested member classes, all the enclosing
3847                     // classes must be public.
3848                     Symbol owner = c.owner;
3849                     while (owner != null && owner.kind == TYP) {
3850                         if ((owner.flags() & PUBLIC) == 0)
3851                             return;
3852                         owner = owner.owner;
3853                     }
3854                 }
3855             }
3856 
3857             // Only check classes in named packages exported by its module
3858             PackageSymbol pkg = c.packge();
3859             if (!pkg.isUnnamed()) {
3860                 ModuleSymbol modle = pkg.modle;
3861                 for (ExportsDirective exportDir : modle.exports) {
3862                     // Report warning only if the containing
3863                     // package is unconditionally exported
3864                     if (exportDir.packge.equals(pkg)) {
3865                         if (exportDir.modules == null || exportDir.modules.isEmpty()) {
3866                             // Warning may be suppressed by
3867                             // annotations; check again for being
3868                             // enabled in the deferred context.
3869                             deferredLintHandler.report(() -> {
3870                                 if (lint.isEnabled(LintCategory.MISSING_DECLARED_CTOR))
3871                                    log.warning(LintCategory.MISSING_DECLARED_CTOR,
3872                                                pos, Warnings.MissingDeclaredCtor(c, pkg, modle));
3873                                                        });
3874                         } else {
3875                             return;
3876                         }
3877                     }
3878                 }
3879             }
3880         }
3881         return;
3882     }
3883 
3884     private class ConversionWarner extends Warner {
3885         final String uncheckedKey;
3886         final Type found;
3887         final Type expected;
3888         public ConversionWarner(DiagnosticPosition pos, String uncheckedKey, Type found, Type expected) {
3889             super(pos);
3890             this.uncheckedKey = uncheckedKey;
3891             this.found = found;
3892             this.expected = expected;
3893         }
3894 
3895         @Override
3896         public void warn(LintCategory lint) {
3897             boolean warned = this.warned;
3898             super.warn(lint);
3899             if (warned) return; // suppress redundant diagnostics
3900             switch (lint) {
3901                 case UNCHECKED:


< prev index next >