1 /*
   2  * Copyright (c) 2006, 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 javax.tools;
  27 
  28 import javax.tools.JavaFileManager.Location;
  29 
  30 import java.util.concurrent.*;
  31 
  32 /**
  33  * Standard locations of file objects.
  34  *
  35  * @author Peter von der Ahé
  36  * @since 1.6
  37  */
  38 public enum StandardLocation implements Location {
  39 
  40     /**
  41      * Location of new class files.
  42      */
  43     CLASS_OUTPUT,
  44 
  45     /**
  46      * Location of new source files.
  47      */
  48     SOURCE_OUTPUT,
  49 
  50     /**
  51      * Location to search for user class files.
  52      */
  53     CLASS_PATH,
  54 
  55     /**
  56      * Location to search for existing source files.
  57      */
  58     SOURCE_PATH,
  59 
  60     /**
  61      * Location to search for annotation processors.
  62      */
  63     ANNOTATION_PROCESSOR_PATH,
  64 
  65     /**
  66      * Location to search for modules containing annotation processors.
  67      * @since 9
  68      */
  69     ANNOTATION_PROCESSOR_MODULE_PATH,
  70 
  71     /**
  72      * Location to search for platform classes.  Sometimes called
  73      * the boot class path.
  74      */
  75     PLATFORM_CLASS_PATH,
  76 
  77     /**
  78      * Location of new native header files.
  79      * @since 1.8
  80      */
  81     NATIVE_HEADER_OUTPUT,
  82 
  83     /**
  84      * Location to search for the source code of modules.
  85      * @since 9
  86      */
  87     MODULE_SOURCE_PATH,
  88 
  89     /**
  90      * Location to search for upgradeable system modules.
  91      * @since 9
  92      */
  93     UPGRADE_MODULE_PATH,
  94 
  95     /**
  96      * Location to search for system modules.
  97      * @since 9
  98      */
  99     SYSTEM_MODULES,
 100 
 101     /**
 102      * Location to search for precompiled user modules.
 103      * @since 9
 104      */
 105     MODULE_PATH;
 106 
 107     /**
 108      * Returns a location object with the given name.  The following
 109      * property must hold: {@code locationFor(x) ==
 110      * locationFor(y)} if and only if {@code x.equals(y)}.
 111      * The returned location will be an output location if and only if
 112      * name ends with {@code "_OUTPUT"}. It will be considered to
 113      * be a module-oriented location if the name contains the word
 114      * {@code "MODULE"}.
 115      *
 116      * @param name a name
 117      * @return a location
 118      */
 119     public static Location locationFor(final String name) {
 120         if (locations.isEmpty()) {
 121             // can't use valueOf which throws IllegalArgumentException
 122             for (Location location : values())
 123                 locations.putIfAbsent(location.getName(), location);
 124         }
 125         name.getClass(); /* null-check */
 126         locations.putIfAbsent(name, new Location() {
 127                 @Override
 128                 public String getName() { return name; }
 129                 @Override
 130                 public boolean isOutputLocation() { return name.endsWith("_OUTPUT"); }
 131             });
 132         return locations.get(name);
 133     }
 134     //where
 135         private static final ConcurrentMap<String,Location> locations
 136             = new ConcurrentHashMap<>();
 137 
 138     @Override
 139     public String getName() { return name(); }
 140 
 141     @Override
 142     public boolean isOutputLocation() {
 143         switch (this) {
 144             case CLASS_OUTPUT:
 145             case SOURCE_OUTPUT:
 146             case NATIVE_HEADER_OUTPUT:
 147                 return true;
 148             default:
 149                 return false;
 150         }
 151     }
 152 
 153     @Override
 154     public boolean isModuleOrientedLocation() {
 155         switch (this) {
 156             case MODULE_SOURCE_PATH:
 157             case ANNOTATION_PROCESSOR_MODULE_PATH:
 158             case UPGRADE_MODULE_PATH:
 159             case SYSTEM_MODULES:
 160             case MODULE_PATH:
 161                 return true;
 162             default:
 163                 return false;
 164         }
 165     }
 166 }