uk.autores.handling.ResourceFiling Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of annotations Show documentation
Show all versions of annotations Show documentation
Annotation driven library for handling embedded resources
// Copyright 2023 https://github.com/autores-uk/autores/blob/main/LICENSE.txt
// SPDX-License-Identifier: Apache-2.0
package uk.autores.handling;
import javax.tools.JavaFileManager;
import java.util.Objects;
/**
* Utility type for working with {@link javax.annotation.processing.Filer}.
* Sample usage:
*
* Filer filer = ...;
* String annotationPackage = "foo.bar";
* String resourcePath = "/META-INF/foo.txt";
* String pkg = ResourceFiling.pkg(annotationPackage, resourcePath);
* String relativeName = ResourceFiling.relativeName(resourcePath);
* FileObject fo = filer.getResource(location, pkg, relativeName);
*
*/
public final class ResourceFiling {
private ResourceFiling() {}
/**
* Resolves the package string to be passed to
* {@link javax.annotation.processing.Filer#getResource(JavaFileManager.Location, CharSequence, CharSequence)}.
*
* @param annotationPackage the annotated package
* @param resourcePath non-empty resource path
* @return the package to use in the filer
*/
public static CharSequence pkg(CharSequence annotationPackage, CharSequence resourcePath) {
boolean absolute = resourcePath.charAt(0) == '/';
return absolute ? "" : Objects.requireNonNull(annotationPackage, "pkg");
}
/**
* Resolves the relative name to be passed to
* {@link javax.annotation.processing.Filer#getResource(JavaFileManager.Location, CharSequence, CharSequence)}.
*
* @param resourcePath non-empty resource path as specified in annotation
* @return the resource name to use in the filer
*/
public static CharSequence relativeName(CharSequence resourcePath) {
boolean absolute = resourcePath.charAt(0) == '/';
return absolute ? resourcePath.subSequence(1, resourcePath.length()) : resourcePath;
}
}