Lines 44-53
Link Here
|
44 |
|
44 |
|
45 |
package org.netbeans.modules.apisupport.project; |
45 |
package org.netbeans.modules.apisupport.project; |
46 |
|
46 |
|
47 |
import com.sun.source.tree.AnnotationTree; |
|
|
48 |
import com.sun.source.tree.CompilationUnitTree; |
47 |
import com.sun.source.tree.CompilationUnitTree; |
49 |
import com.sun.source.tree.ExpressionTree; |
48 |
import com.sun.source.tree.ExpressionTree; |
50 |
import java.io.BufferedReader; |
49 |
import java.io.BufferedReader; |
|
|
50 |
import java.io.File; |
51 |
import java.io.IOException; |
51 |
import java.io.IOException; |
52 |
import java.io.InputStream; |
52 |
import java.io.InputStream; |
53 |
import java.io.InputStreamReader; |
53 |
import java.io.InputStreamReader; |
Lines 76-81
Link Here
|
76 |
import javax.script.ScriptEngineManager; |
76 |
import javax.script.ScriptEngineManager; |
77 |
import javax.script.ScriptException; |
77 |
import javax.script.ScriptException; |
78 |
import javax.swing.text.PlainDocument; |
78 |
import javax.swing.text.PlainDocument; |
|
|
79 |
import org.netbeans.api.java.source.GeneratorUtilities; |
79 |
import org.netbeans.api.java.source.JavaSource; |
80 |
import org.netbeans.api.java.source.JavaSource; |
80 |
import org.netbeans.api.java.source.Task; |
81 |
import org.netbeans.api.java.source.Task; |
81 |
import org.netbeans.api.java.source.TreeMaker; |
82 |
import org.netbeans.api.java.source.TreeMaker; |
Lines 1136-1172
Link Here
|
1136 |
return new PackageInfo(project, packageName, annotations); |
1137 |
return new PackageInfo(project, packageName, annotations); |
1137 |
} |
1138 |
} |
1138 |
private static class PackageInfo extends AbstractOperation { |
1139 |
private static class PackageInfo extends AbstractOperation { |
1139 |
private final String packageName; |
|
|
1140 |
private final Map<String,Map<String,Object>> annotations; |
1140 |
private final Map<String,Map<String,Object>> annotations; |
1141 |
private final String srcFilePath; |
1141 |
private final String srcRootPath, folderRelPath, srcRelPath; |
1142 |
PackageInfo(Project project, String packageName, Map<String,Map<String,Object>> annotations) { |
1142 |
PackageInfo(Project project, String packageName, Map<String,Map<String,Object>> annotations) { |
1143 |
super(project); |
1143 |
super(project); |
1144 |
this.packageName = packageName; |
|
|
1145 |
this.annotations = annotations; |
1144 |
this.annotations = annotations; |
1146 |
srcFilePath = getModuleInfo().getResourceDirectoryPath(false) + "/" + packageName.replace('.', '/') + "/package-info.java"; |
1145 |
srcRootPath = getModuleInfo().getResourceDirectoryPath(false); |
1147 |
addCreatedOrModifiedPath(srcFilePath, true); |
1146 |
folderRelPath = packageName.replace('.', '/'); |
|
|
1147 |
srcRelPath = folderRelPath + "/package-info.java"; // NOI18N |
1148 |
addCreatedOrModifiedPath(srcRootPath + '/' + srcRelPath, true); |
1148 |
} |
1149 |
} |
1149 |
public void run() throws IOException { |
1150 |
public @Override void run() throws IOException { |
1150 |
final FileObject top = getProject().getProjectDirectory(); |
1151 |
final FileObject top = getProject().getProjectDirectory(); |
1151 |
top.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() { |
1152 |
top.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() { |
1152 |
public void run() throws IOException { |
1153 |
public @Override void run() throws IOException { |
1153 |
FileObject srcFile = top.getFileObject(srcFilePath); |
1154 |
FileObject srcRoot = FileUtil.createFolder(top, srcRootPath); |
1154 |
final Charset encoding = FileEncodingQuery.getEncoding(srcFile != null ? srcFile : top); |
1155 |
FileObject srcFile = srcRoot.getFileObject(srcRelPath); |
1155 |
if (srcFile == null) { |
1156 |
if (srcFile == null) { |
1156 |
srcFile = FileUtil.createData(top, srcFilePath); |
1157 |
// Cf. #119887; any better way to add license header? Otherwise could use: |
1157 |
OutputStream os = srcFile.getOutputStream(); |
1158 |
// nue = make.CompilationUnit(anns, srcRoot, srcRelPath, Collections.<ImportTree>emptyList(), Collections.<Tree>emptyList()); |
1158 |
try { |
1159 |
srcFile = DataObject.find(FileUtil.getConfigFile("Templates/Classes/package-info.java")).createFromTemplate(DataFolder.findFolder(FileUtil.createFolder(srcRoot, folderRelPath))).getPrimaryFile(); // NOI18N |
1159 |
Writer w = new OutputStreamWriter(os, encoding); |
1160 |
} |
1160 |
w.write("package " + packageName + ";\n"); |
1161 |
if (!annotations.isEmpty()) { |
1161 |
w.close(); |
1162 |
JavaSource source = JavaSource.forFileObject(srcFile); |
1162 |
} finally { |
1163 |
if (source == null) { |
1163 |
os.close(); |
1164 |
throw new IOException("unparsable: " + srcFile); |
1164 |
} |
1165 |
} |
1165 |
} |
1166 |
source.runModificationTask(new Task<WorkingCopy>() { |
1166 |
final FileObject _srcFile = srcFile; |
1167 |
public @Override void run(WorkingCopy wc) throws Exception { |
1167 |
if (!annotations.isEmpty()) { |
|
|
1168 |
JavaSource.forFileObject(srcFile).runModificationTask(new Task<WorkingCopy>() { |
1169 |
public void run(WorkingCopy wc) throws Exception { |
1170 |
wc.toPhase(JavaSource.Phase.RESOLVED); |
1168 |
wc.toPhase(JavaSource.Phase.RESOLVED); |
1171 |
CompilationUnitTree old = wc.getCompilationUnit(); |
1169 |
CompilationUnitTree old = wc.getCompilationUnit(); |
1172 |
CompilationUnitTree nue = old; |
1170 |
CompilationUnitTree nue = old; |
Lines 1181-1205
Link Here
|
1181 |
for (Map.Entry<String,Object> attr : ann.getValue().entrySet()) { |
1179 |
for (Map.Entry<String,Object> attr : ann.getValue().entrySet()) { |
1182 |
arguments.add(make.Assignment(make.Identifier(attr.getKey()), make.Literal(attr.getValue()))); |
1180 |
arguments.add(make.Assignment(make.Identifier(attr.getKey()), make.Literal(attr.getValue()))); |
1183 |
} |
1181 |
} |
1184 |
AnnotationTree annTree = make.Annotation(annotationTypeTree, arguments); |
1182 |
nue = make.addPackageAnnotation(nue, make.Annotation(annotationTypeTree, arguments)); |
1185 |
// XXX #157760 should give some way to attach annTree to nue; in the meantime: |
|
|
1186 |
String text = _srcFile.asText(encoding.name()); |
1187 |
text = text.replaceFirst("(?m)^(package )", annTree + "\n$1"); |
1188 |
OutputStream os = _srcFile.getOutputStream(); |
1189 |
try { |
1190 |
Writer w = new OutputStreamWriter(os, encoding); |
1191 |
w.write(text); |
1192 |
w.close(); |
1193 |
} finally { |
1194 |
os.close(); |
1195 |
} |
1196 |
} |
1183 |
} |
1197 |
/* XXX #157760: |
|
|
1198 |
nue = GeneratorUtilities.get(wc).importFQNs(nue); |
1184 |
nue = GeneratorUtilities.get(wc).importFQNs(nue); |
1199 |
wc.rewrite(old, nue); |
1185 |
wc.rewrite(old, nue); |
1200 |
*/ |
|
|
1201 |
} |
1186 |
} |
1202 |
})/*.commit()*/; |
1187 |
}).commit(); |
|
|
1188 |
SaveCookie sc = DataObject.find(srcFile).getLookup().lookup(SaveCookie.class); |
1189 |
if (sc != null) { |
1190 |
sc.save(); |
1191 |
} |
1203 |
} |
1192 |
} |
1204 |
} |
1193 |
} |
1205 |
}); |
1194 |
}); |