1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  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 jdk.internal.module;
  27 
  28 import java.lang.module.ModuleReference;
  29 import static jdk.internal.module.ClassFileConstants.*;
  30 
  31 /**
  32  * Represents the Module Resolution flags.
  33  */
  34 public final class ModuleResolution {
  35 
  36     final int value;
  37 
  38     public ModuleResolution(int value) {
  39         this.value = value;
  40     }
  41 
  42     public boolean doNotResolveByDefault() {
  43         return (value & DO_NOT_RESOLVE_BY_DEFAULT) != 0;
  44     }
  45 
  46     public boolean hasDeprecatedWarning() {
  47         return (value & WARN_DEPRECATED) != 0;
  48     }
  49 
  50     public boolean hasDeprecatedForRemovalWarning() {
  51         return (value & WARN_DEPRECATED_FOR_REMOVAL) != 0;
  52     }
  53 
  54     public boolean hasIncubatingWarning() {
  55         return (value & WARN_INCUBATING) != 0;
  56     }
  57 
  58     public ModuleResolution withDoNotResolveByDefault() {
  59         return new ModuleResolution(value | DO_NOT_RESOLVE_BY_DEFAULT);
  60     }
  61 
  62     public ModuleResolution withDeprecated() {
  63         if ((value & (WARN_DEPRECATED_FOR_REMOVAL | WARN_INCUBATING)) != 0)
  64             throw new RuntimeException("cannot add deprecated to " + value);
  65         return new ModuleResolution(value | WARN_DEPRECATED);
  66     }
  67 
  68     public ModuleResolution withDeprecatedForRemoval() {
  69         if ((value & (WARN_DEPRECATED | WARN_INCUBATING)) != 0)
  70             throw new RuntimeException("cannot add deprecated for removal to " + value);
  71         return new ModuleResolution(value | WARN_DEPRECATED_FOR_REMOVAL);
  72     }
  73     public ModuleResolution withIncubating() {
  74         if ((value & (WARN_DEPRECATED | WARN_DEPRECATED_FOR_REMOVAL)) != 0)
  75             throw new RuntimeException("cannot add incubating to " + value);
  76         return new ModuleResolution(value | WARN_INCUBATING);
  77     }
  78 
  79     public int value() {
  80         return value;
  81     }
  82 
  83     public static boolean doNotResolveByDefault(ModuleReference mref) {
  84         // get the DO_NOT_RESOLVE_BY_DEFAULT flag, if any
  85         if (!(mref instanceof ModuleReferenceImpl))
  86             return false;
  87 
  88         ModuleResolution mres = ((ModuleReferenceImpl)mref).moduleResolution();
  89         if (mres != null)
  90             return mres.doNotResolveByDefault();
  91 
  92         return false;
  93     }
  94 
  95     public static boolean hasIncubatingWarning(ModuleReference mref) {
  96         if (!(mref instanceof ModuleReferenceImpl))
  97             return false;
  98 
  99         ModuleResolution mres = ((ModuleReferenceImpl)mref).moduleResolution();
 100         if (mres != null)
 101             return mres.hasIncubatingWarning();
 102 
 103         return false;
 104     }
 105 
 106     @Override
 107     public String toString() {
 108         return String.valueOf(value);
 109     }
 110 }