2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.compiler
.impl
.javaCompiler
.api
;
18 import com
.intellij
.openapi
.diagnostic
.Logger
;
19 import com
.intellij
.openapi
.util
.io
.FileUtil
;
20 import com
.intellij
.openapi
.util
.text
.StringUtil
;
21 import com
.intellij
.openapi
.vfs
.JarFileSystem
;
22 import com
.intellij
.openapi
.vfs
.LocalFileSystem
;
23 import com
.intellij
.openapi
.vfs
.VirtualFile
;
24 import com
.intellij
.util
.SmartList
;
25 import com
.sun
.tools
.javac
.util
.Context
;
26 import com
.sun
.tools
.javac
.util
.JavacFileManager
;
27 import gnu
.trove
.THashSet
;
28 import gnu
.trove
.TObjectHashingStrategy
;
32 import java
.io
.IOException
;
39 @SuppressWarnings({"Since15"})
40 class MyFileManager
extends JavacFileManager
{
41 private static final Logger LOG
= Logger
.getInstance("#com.intellij.compiler.impl.javaCompiler.api.MyFileManager");
43 private final String myOutputDir
;
44 private final CompAPIDriver myCompAPIDriver
;
46 MyFileManager(CompAPIDriver compAPIDriver
, String outputDir
) {
47 super(new Context(), false, null);
48 myCompAPIDriver
= compAPIDriver
;
49 myOutputDir
= outputDir
;
53 public Iterable
<?
extends JavaFileObject
> getJavaFileObjectsFromFiles(Iterable
<?
extends File
> files
) {
54 int size
= ((Collection
)files
).size();
55 List
<JavaFileObject
> result
= new ArrayList
<JavaFileObject
>(size
);
57 for (File file
: files
) {
58 JavaFileObject fileObject
= new JavaVirtualByIoFile(file
, JavaFileObject
.Kind
.SOURCE
);
59 result
.add(fileObject
);
66 public JavaFileObject
getJavaFileForOutput(Location location
, String name
, JavaFileObject
.Kind kind
, FileObject fileObject
) {
67 URI uri
= toURI(myOutputDir
, name
);
68 return new Output(uri
, myCompAPIDriver
);
71 static URI
createUri(String url
) {
72 return URI
.create(url
.replaceAll(" ","%20"));
75 private static URI
toURI(String outputDir
, String name
) {
76 return createUri("file:///" + outputDir
.replace('\\','/') + "/" + name
.replace('.', '/') + JavaFileObject
.Kind
.CLASS
.extension
);
80 public Iterable
<JavaFileObject
> list(Location location
, String packageName
, Set
<JavaFileObject
.Kind
> kinds
, boolean recurse
) throws IOException
{
82 return super.list(location
, packageName
, kinds
, recurse
);
84 Iterable
<?
extends File
> path
= getLocation(location
);
85 if (path
== null) return Collections
.emptyList();
87 String subdirectory
= packageName
.replace('.', '/');
88 List
<JavaFileObject
> results
= null;
90 for (File directory
: path
) {
91 VirtualFile dir
= LocalFileSystem
.getInstance().findFileByIoFile(directory
);
92 if (dir
== null) continue;
93 if (!dir
.isDirectory()) {
94 dir
= JarFileSystem
.getInstance().getJarRootForLocalFile(dir
);
95 if (dir
== null) continue;
97 VirtualFile virtualFile
= StringUtil
.isEmptyOrSpaces(subdirectory
) ? dir
: dir
.findFileByRelativePath(subdirectory
);
98 if (virtualFile
== null) continue;
100 if (!virtualFile
.isDirectory()) continue;
101 for (VirtualFile child
: virtualFile
.getChildren()) {
102 JavaFileObject
.Kind kind
= getKind("."+child
.getExtension());
103 if (kinds
.contains(kind
)) {
104 if (results
== null) results
= new SmartList
<JavaFileObject
>();
105 if (kind
== JavaFileObject
.Kind
.SOURCE
&& child
.getFileSystem() instanceof JarFileSystem
) continue; //for some reasdon javac looks for java files inside jar
107 // do not use VFS to read .class content
108 JavaFileObject fileObject
=
109 kind
== JavaFileObject
.Kind
.CLASS
&& child
.getFileSystem() == LocalFileSystem
.getInstance() ?
110 new JavaIoFile(new File(child
.getPath()), kind
) : new JavaVirtualFile(child
, kind
);
111 results
.add(fileObject
);
116 List
<JavaFileObject
> ret
= results
== null ? Collections
.<JavaFileObject
>emptyList() : results
;
118 if (LOG
.isDebugEnabled()) {
119 // for testing consistency
120 Collection c
= (Collection
)super.list(location
, packageName
, kinds
, recurse
);
121 Collection
<JavaFileObject
> sup
= new HashSet(c
);
122 assert sup
.size() == c
.size();
123 assert new HashSet(c
).equals(sup
);
125 THashSet
<JavaFileObject
> s
= new THashSet
<JavaFileObject
>(new TObjectHashingStrategy
<JavaFileObject
>() {
126 public int computeHashCode(JavaFileObject object
) {
127 return object
.getName().hashCode();
130 public boolean equals(JavaFileObject o1
, JavaFileObject o2
) {
131 return o1
.getKind() == o2
.getKind() && o1
.toUri().equals(o2
.toUri());
137 if (ret
.size() != sup
.size()) {
138 assert false : "our implementation differs from javac'";
146 public String
inferBinaryName(Location location
, JavaFileObject file
) {
147 return FileUtil
.getNameWithoutExtension(new File(file
.getName()).getName());