FSInfo.java revision 3392:04fcbc7234a4
1/*
2 * Copyright (c) 2008, 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
26package com.sun.tools.javac.file;
27
28import java.io.IOException;
29import java.nio.file.FileSystems;
30import java.nio.file.Files;
31import java.nio.file.Path;
32import java.nio.file.Paths;
33import java.util.ArrayList;
34import java.util.Collections;
35import java.util.List;
36import java.util.StringTokenizer;
37import java.util.jar.Attributes;
38import java.util.jar.JarFile;
39import java.util.jar.Manifest;
40
41import com.sun.tools.javac.util.Context;
42
43/**
44 * Get meta-info about files. Default direct (non-caching) implementation.
45 * @see CacheFSInfo
46 *
47 * <p><b>This is NOT part of any supported API.
48 * If you write code that depends on this, you do so at your own risk.
49 * This code and its internal interfaces are subject to change or
50 * deletion without notice.</b>
51 */
52public class FSInfo {
53
54    /** Get the FSInfo instance for this context.
55     *  @param context the context
56     *  @return the Paths instance for this context
57     */
58    public static FSInfo instance(Context context) {
59        FSInfo instance = context.get(FSInfo.class);
60        if (instance == null)
61            instance = new FSInfo();
62        return instance;
63    }
64
65    protected FSInfo() {
66    }
67
68    protected FSInfo(Context context) {
69        context.put(FSInfo.class, this);
70    }
71
72    public Path getCanonicalFile(Path file) {
73        try {
74            return file.toRealPath();
75        } catch (IOException e) {
76            return file.toAbsolutePath().normalize();
77        }
78    }
79
80    public boolean exists(Path file) {
81        return Files.exists(file);
82    }
83
84    public boolean isDirectory(Path file) {
85        return Files.isDirectory(file);
86    }
87
88    public boolean isFile(Path file) {
89        return Files.isRegularFile(file);
90    }
91
92    public List<Path> getJarClassPath(Path file) throws IOException {
93        Path parent = file.getParent();
94        try (JarFile jarFile = new JarFile(file.toFile())) {
95            Manifest man = jarFile.getManifest();
96            if (man == null)
97                return Collections.emptyList();
98
99            Attributes attr = man.getMainAttributes();
100            if (attr == null)
101                return Collections.emptyList();
102
103            String path = attr.getValue(Attributes.Name.CLASS_PATH);
104            if (path == null)
105                return Collections.emptyList();
106
107            List<Path> list = new ArrayList<>();
108
109            for (StringTokenizer st = new StringTokenizer(path);
110                 st.hasMoreTokens(); ) {
111                String elt = st.nextToken();
112                Path f = FileSystems.getDefault().getPath(elt);
113                if (!f.isAbsolute() && parent != null)
114                    f = parent.resolve(f).toAbsolutePath();
115                list.add(f);
116            }
117
118            return list;
119        }
120    }
121}
122