-
Notifications
You must be signed in to change notification settings - Fork 963
/
InputFilePreprocessor.java
139 lines (126 loc) · 5.23 KB
/
InputFilePreprocessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.devtools.j2objc.pipeline;
import com.google.common.io.Files;
import com.google.devtools.j2objc.Options;
import com.google.devtools.j2objc.ast.Annotation;
import com.google.devtools.j2objc.ast.CompilationUnit;
import com.google.devtools.j2objc.ast.SingleMemberAnnotation;
import com.google.devtools.j2objc.file.InputFile;
import com.google.devtools.j2objc.file.RegularInputFile;
import com.google.devtools.j2objc.util.ErrorUtil;
import com.google.devtools.j2objc.util.FileUtil;
import com.google.devtools.j2objc.util.Parser;
import com.google.devtools.j2objc.util.TypeUtil;
import com.google.j2objc.annotations.ObjectiveCName;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;
/**
* Preprocesses each Java file in the batch.
*/
public class InputFilePreprocessor {
private static final Logger logger = Logger.getLogger(InputFilePreprocessor.class.getName());
private final Parser parser;
private File strippedSourcesDir;
private final Options options;
public InputFilePreprocessor(Parser parser) {
this.parser = parser;
this.options = parser.options();
}
public void processInputs(Iterable<ProcessingContext> inputs) {
for (ProcessingContext input : inputs) {
if (input.getFile().getUnitName().endsWith(".java")) {
processInput(input);
}
}
}
private void processInput(ProcessingContext input) {
try {
if (input.getFile().getUnitName().endsWith("package-info.java")) {
processPackageInfoSource(input);
} else {
processRegularSource(input);
}
} catch (IOException e) {
ErrorUtil.error(e.getMessage());
}
}
public File getStrippedSourcesDir() {
return strippedSourcesDir;
}
private File getCreatedStrippedSourcesDir() {
if (strippedSourcesDir == null) {
strippedSourcesDir = Files.createTempDir();
logger.finest("Created temp dir for stripped sources: " + strippedSourcesDir.getPath());
}
return strippedSourcesDir;
}
private void processRegularSource(ProcessingContext input) throws IOException {
InputFile file = input.getFile();
String source = options.fileUtil().readFile(file);
boolean shouldMapHeaders = options.getHeaderMap().useSourceDirectories();
boolean doIncompatibleStripping = source.contains("J2ObjCIncompatible");
if (!(shouldMapHeaders || doIncompatibleStripping)) {
// No need to parse.
return;
}
Parser.ParseResult parseResult = parser.parseWithoutBindings(file, source);
if (parseResult == null) {
// The parser found and reported one or more errors.
return;
}
String qualifiedName = parseResult.mainTypeName();
if (shouldMapHeaders) {
options.getHeaderMap().put(qualifiedName, input.getGenerationUnit().getOutputPath() + ".h");
}
if (doIncompatibleStripping) {
parseResult.stripIncompatibleSource();
File strippedDir = getCreatedStrippedSourcesDir();
String relativePath = qualifiedName.replace('.', File.separatorChar) + ".java";
File strippedFile = new File(strippedDir, relativePath);
Files.createParentDirs(strippedFile);
Files.write(parseResult.getSource(), strippedFile, options.fileUtil().getCharset());
input.setFile(new RegularInputFile(strippedFile.getPath(), relativePath));
}
}
private void processPackageInfoSource(ProcessingContext input) throws IOException {
InputFile file = input.getFile();
String source = options.fileUtil().readFile(file);
CompilationUnit compilationUnit =
parser.parse(FileUtil.getMainTypeName(file), file.getUnitName(), source);
if (compilationUnit != null) {
extractPackagePrefix(file, compilationUnit);
}
}
private void extractPackagePrefix(InputFile file, CompilationUnit unit) {
// We should only reach here if it's a package-info.java file.
assert file.getUnitName().endsWith("package-info.java");
List<Annotation> annotations = (List<Annotation>) unit.getPackage().getAnnotations();
for (Annotation annotation : annotations) {
// getFullyQualifiedName() might not actually return a fully qualified name.
String name = annotation.getTypeName().getFullyQualifiedName();
if (name.endsWith("ObjectiveCName")) {
// Per Eclipse docs, binding resolution can be a resource hog.
if (TypeUtil.getQualifiedName(annotation.getAnnotationMirror().getAnnotationType()).equals(
ObjectiveCName.class.getCanonicalName())) {
String key = unit.getPackage().getName().getFullyQualifiedName();
String val = (String) ((SingleMemberAnnotation) annotation).getValue().getConstantValue();
options.getPackagePrefixes().addPrefix(key, val);
}
}
}
}
}