Lines 45-52
Link Here
|
45 |
import java.io.FileNotFoundException; |
45 |
import java.io.FileNotFoundException; |
46 |
import java.io.IOException; |
46 |
import java.io.IOException; |
47 |
import java.io.InputStream; |
47 |
import java.io.InputStream; |
|
|
48 |
import java.io.ObjectOutputStream; |
48 |
import java.lang.annotation.Annotation; |
49 |
import java.lang.annotation.Annotation; |
49 |
import java.net.URI; |
50 |
import java.net.URI; |
|
|
51 |
import java.net.URISyntaxException; |
50 |
import java.util.Arrays; |
52 |
import java.util.Arrays; |
51 |
import java.util.LinkedHashMap; |
53 |
import java.util.LinkedHashMap; |
52 |
import java.util.LinkedList; |
54 |
import java.util.LinkedList; |
Lines 66-71
Link Here
|
66 |
import javax.lang.model.type.TypeMirror; |
68 |
import javax.lang.model.type.TypeMirror; |
67 |
import javax.lang.model.util.ElementFilter; |
69 |
import javax.lang.model.util.ElementFilter; |
68 |
import javax.tools.Diagnostic.Kind; |
70 |
import javax.tools.Diagnostic.Kind; |
|
|
71 |
import javax.tools.FileObject; |
72 |
import javax.tools.JavaFileManager.Location; |
69 |
import javax.tools.StandardLocation; |
73 |
import javax.tools.StandardLocation; |
70 |
import org.openide.util.NbBundle.Messages; |
74 |
import org.openide.util.NbBundle.Messages; |
71 |
import org.w3c.dom.Document; |
75 |
import org.w3c.dom.Document; |
Lines 310-315
Link Here
|
310 |
} |
314 |
} |
311 |
|
315 |
|
312 |
/** |
316 |
/** |
|
|
317 |
* Validates a resource named in an annotation. |
318 |
* <p>Note that resources found in the binary classpath (if permitted) |
319 |
* cannot actually be located when running inside javac on JDK 6 (see #196933 for discussion), in which case |
320 |
* no exception is thrown but the return value may not permit {@link FileObject#openInputStream}. |
321 |
* <p>Also remember that the binary compilation classpath for an Ant-based NetBeans module does |
322 |
* not include non-public packages or indeed any non-classfiles from module dependencies. |
323 |
* The processorpath does contain all of these but it is not consulted. |
324 |
* The classpath for a Maven-based module does also contain resources from dependencies. |
325 |
* @param resource an absolute resource path with no leading slash (perhaps the output of {@link #absolutizeResource}) |
326 |
* @param originatingElement the annotated element; used both for error reporting, and (optionally) for its package |
327 |
* @param annotation as in {@link LayerGenerationException#LayerGenerationException(String,Element,ProcessingEnvironment,Annotation,String)} |
328 |
* @param annotationMethod as in {@link LayerGenerationException#LayerGenerationException(String,Element,ProcessingEnvironment,Annotation,String)} |
329 |
* @param searchClasspath true to search in the binary classpath and not just source path (see caveat about JDK 6) |
330 |
* @return the content of the resource, for further validation |
331 |
* @throws LayerGenerationException if no such resource can be found |
332 |
* @since 7.51 |
333 |
*/ |
334 |
public FileObject validateResource(String resource, Element originatingElement, Annotation annotation, String annotationMethod, boolean searchClasspath) throws LayerGenerationException { |
335 |
if (resource.startsWith("/")) { |
336 |
throw new LayerGenerationException("do not use leading slashes on resource paths", originatingElement, processingEnv, annotation, annotationMethod); |
337 |
} |
338 |
if (searchClasspath) { |
339 |
for (Location loc : new Location[] {StandardLocation.SOURCE_PATH, /* #181355 */StandardLocation.CLASS_OUTPUT, StandardLocation.CLASS_PATH, StandardLocation.PLATFORM_CLASS_PATH}) { |
340 |
try { |
341 |
return processingEnv.getFiler().getResource(loc, "", resource); |
342 |
} catch (IOException ex) { |
343 |
continue; |
344 |
} |
345 |
} |
346 |
throw new LayerGenerationException("Cannot find resource " + resource, originatingElement, processingEnv, annotation, annotationMethod); |
347 |
} else { |
348 |
try { |
349 |
try { |
350 |
FileObject f = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "", resource); |
351 |
f.openInputStream().close(); |
352 |
return f; |
353 |
} catch (FileNotFoundException x) { |
354 |
try { |
355 |
FileObject f = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", resource); |
356 |
f.openInputStream().close(); |
357 |
return f; |
358 |
} catch (IOException x2) { |
359 |
throw x; |
360 |
} |
361 |
} |
362 |
} catch (IOException x) { |
363 |
throw new LayerGenerationException("Cannot find resource " + resource, originatingElement, processingEnv, annotation, annotationMethod); |
364 |
} |
365 |
} |
366 |
} |
367 |
|
368 |
/** |
369 |
* Allows a processor to accept relative resource paths. |
370 |
* For example, to produce the output value {@code net/nowhere/lib/icon.png} |
371 |
* given an element in the package {@code net.nowhere.app}, the following inputs are permitted: |
372 |
* <ul> |
373 |
* <li>{@code ../lib/icon.png} |
374 |
* <li>{@code /net/nowhere/lib/icon.png} |
375 |
* </ul> |
376 |
* @param originatingElement the annotated element, used for its package |
377 |
* @param resource a possibly relative resource path |
378 |
* @return an absolute resource path (with no leading slash) |
379 |
* @throws LayerGenerationException in case the resource path is malformed |
380 |
* @since 7.51 |
381 |
*/ |
382 |
public static String absolutizeResource(Element originatingElement, String resource) throws LayerGenerationException { |
383 |
if (resource.startsWith("/")) { |
384 |
return resource.substring(1); |
385 |
} else { |
386 |
try { |
387 |
return new URI(null, findPackage(originatingElement).replace('.', '/') + "/", null).resolve(new URI(null, resource, null)).getPath(); |
388 |
} catch (URISyntaxException x) { |
389 |
throw new LayerGenerationException(x.toString(), originatingElement); |
390 |
} |
391 |
} |
392 |
} |
393 |
private static String findPackage(Element e) { |
394 |
switch (e.getKind()) { |
395 |
case PACKAGE: |
396 |
return ((PackageElement) e).getQualifiedName().toString(); |
397 |
default: |
398 |
return findPackage(e.getEnclosingElement()); |
399 |
} |
400 |
} |
401 |
|
402 |
|
403 |
/** |
313 |
* Builder for creating a single file entry. |
404 |
* Builder for creating a single file entry. |
314 |
*/ |
405 |
*/ |
315 |
public final class File { |
406 |
public final class File { |
Lines 627-655
Link Here
|
627 |
} |
718 |
} |
628 |
} |
719 |
} |
629 |
} |
720 |
} |
630 |
String resource = bundle.replace('.', '/') + ".properties"; |
|
|
631 |
try { |
721 |
try { |
632 |
InputStream is; |
722 |
InputStream is = validateResource(bundle.replace('.', '/') + ".properties", originatingElement, null, null, false).openInputStream(); |
633 |
try { |
|
|
634 |
is = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "", resource).openInputStream(); |
635 |
} catch (FileNotFoundException x) { // #181355 |
636 |
try { |
637 |
is = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", resource).openInputStream(); |
638 |
} catch (IOException x2) { |
639 |
throw x; |
640 |
} |
641 |
} |
642 |
try { |
723 |
try { |
643 |
Properties p = new Properties(); |
724 |
Properties p = new Properties(); |
644 |
p.load(is); |
725 |
p.load(is); |
645 |
if (p.getProperty(key) == null) { |
726 |
if (p.getProperty(key) == null) { |
646 |
throw new LayerGenerationException("No key '" + key + "' found in " + resource, originatingElement, processingEnv, annotation, annotationMethod); |
727 |
throw new LayerGenerationException("No key '" + key + "' found in " + bundle, originatingElement, processingEnv, annotation, annotationMethod); |
647 |
} |
728 |
} |
648 |
} finally { |
729 |
} finally { |
649 |
is.close(); |
730 |
is.close(); |
650 |
} |
731 |
} |
651 |
} catch (IOException x) { |
732 |
} catch (IOException x) { |
652 |
throw new LayerGenerationException("Could not open " + resource + ": " + x, originatingElement, processingEnv, annotation, annotationMethod); |
733 |
throw new LayerGenerationException("Could not open " + bundle + ": " + x, originatingElement, processingEnv, annotation, annotationMethod); |
653 |
} |
734 |
} |
654 |
} |
735 |
} |
655 |
|
736 |
|