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