本文整理汇总了Java中javax.lang.model.util.Elements.getPackageOf方法的典型用法代码示例。如果您正苦于以下问题:Java Elements.getPackageOf方法的具体用法?Java Elements.getPackageOf怎么用?Java Elements.getPackageOf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.lang.model.util.Elements
的用法示例。
在下文中一共展示了Elements.getPackageOf方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getContainingClassOrPackageHeader
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
private StringBuilder getContainingClassOrPackageHeader(Element el, Elements elements, ElementUtilities eu) {
StringBuilder sb = new StringBuilder();
if (el.getKind() != ElementKind.PACKAGE && el.getKind() != ElementKind.MODULE) {
TypeElement cls = eu.enclosingTypeElement(el);
if (cls != null) {
switch(cls.getEnclosingElement().getKind()) {
case ANNOTATION_TYPE:
case CLASS:
case ENUM:
case INTERFACE:
case PACKAGE:
sb.append("<font size='+0'><b>"); //NOI18N
createLink(sb, cls, makeNameLineBreakable(cls.getQualifiedName().toString()));
sb.append("</b></font>"); //NOI18N)
}
} else {
PackageElement pkg = elements.getPackageOf(el);
if (pkg != null) {
sb.append("<font size='+0'><b>"); //NOI18N
createLink(sb, pkg, makeNameLineBreakable(pkg.getQualifiedName().toString()));
sb.append("</b></font>"); //NOI18N)
}
}
}
return sb;
}
示例2: ClassDescription
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
public ClassDescription(TypeElement clz, Elements elems) {
elements = new LinkedHashSet<>();
this.clz = clz;
pkg = elems.getPackageOf(clz);
pkgName = pkg.getQualifiedName().toString();
simpleClzName = clz.getSimpleName().toString();
clzName = clz.getQualifiedName().toString();
for (Element elem : clz.getEnclosedElements()) {
if (ElementKind.FIELD.equals(elem.getKind())
&& !(elem.getModifiers().contains(Modifier.STATIC))
&& !(elem.getModifiers().contains(Modifier.FINAL))
&& (elem.getModifiers().contains(Modifier.PUBLIC))) {
ElementDescription des = new ElementDescription((VariableElement) elem);
elements.add(des);
}
}
}
示例3: KickbackElementClass
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
public KickbackElementClass(VariableElement variableElement, Elements elementUtils) throws VerifyException {
KickbackElement kickbackElement = variableElement.getAnnotation(KickbackElement.class);
Weak weak = variableElement.getAnnotation(Weak.class);
Soft soft = variableElement.getAnnotation(Soft.class);
PackageElement packageElement = elementUtils.getPackageOf(variableElement);
this.variableElement = variableElement;
this.packageName = packageElement.isUnnamed() ? null : packageElement.getQualifiedName().toString();
this.typeName = TypeName.get(variableElement.asType());
this.clazzName = variableElement.getSimpleName().toString();
this.value = variableElement.getConstantValue();
if(weak != null) this.isWeak = true;
else this.isWeak = false;
if(soft != null) this.isSoft = true;
else this.isSoft = false;
if(kickbackElement != null) {
this.elementName = StringUtils.toUpperCamel(Strings.isNullOrEmpty(kickbackElement.name()) ? this.clazzName : kickbackElement.name());
} else {
this.elementName = StringUtils.toUpperCamel(this.clazzName);
}
checkPrimitiveType();
checkModifierValidate();
checkAnnotationValidate();
}
示例4: getPackageName
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
public static String getPackageName(Elements elements, TypeElement typeElement) throws NoPackageNameException {
PackageElement pkg = elements.getPackageOf(typeElement);
if (pkg.isUnnamed()) {
throw new NoPackageNameException(typeElement);
}
return pkg.getQualifiedName().toString();
}
示例5: isAccessible
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
/**
* returns true if elm is accessible from clazz. elm must be member of clazz
* or its superclass
*/
private static boolean isAccessible(CompilationInfo javac, TypeElement clazz, Element elm) {
if (clazz == elm.getEnclosingElement()) {
return true;
}
Set<Modifier> mods = elm.getModifiers();
if (mods.contains(Modifier.PUBLIC) || mods.contains(Modifier.PROTECTED)) {
return true;
} else if (mods.contains(Modifier.PRIVATE)) {
return false;
}
Elements utils = javac.getElements();
return utils.getPackageOf(elm) == utils.getPackageOf(clazz);
}
示例6: generateCode
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
public void generateCode(Elements elementUtils, Filer filer) throws IOException {
TypeElement superClassName = elementUtils.getTypeElement(qualifiedClassName);
String factoryClassName = superClassName.getSimpleName() + SUFFIX;
JavaFileObject jfo = filer.createSourceFile(qualifiedClassName + SUFFIX);
Writer writer = jfo.openWriter();
JavaWriter jw = new JavaWriter(writer);
// 写包名
PackageElement pkg = elementUtils.getPackageOf(superClassName);
if (!pkg.isUnnamed()) {
jw.emitPackage(pkg.getQualifiedName().toString());
jw.emitEmptyLine();
} else {
jw.emitPackage("");
}
jw.beginType(factoryClassName, "class", EnumSet.of(Modifier.PUBLIC));
jw.emitEmptyLine();
jw.beginMethod(qualifiedClassName, "create", EnumSet.of(Modifier.PUBLIC), "String", "id");
jw.beginControlFlow("if (id == null)");
jw.emitStatement("throw new IllegalArgumentException(\"id is null!\")");
jw.endControlFlow();
for (FactoryAnnotatedClass item : itemsMap.values()) {
jw.beginControlFlow(String.format("if (\"%s\".equals(id))", item.getId()));
jw.emitStatement("return new %s()", item.getTypeElement().getQualifiedName().toString());
jw.endControlFlow();
jw.emitEmptyLine();
}
jw.emitStatement("throw new IllegalArgumentException(\"Unknown id = \" + id)");
jw.endMethod();
jw.endType();
jw.close();
}
示例7: getPackageName
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
public static String getPackageName(Elements elementUtils, TypeElement type) throws IOException {
PackageElement pkg = elementUtils.getPackageOf(type);
if (!pkg.isUnnamed()) {
return pkg.getQualifiedName().toString();
} else {
return ""; // Default package
}
}
示例8: getPackageName
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
static String getPackageName(Elements elementUtils, TypeElement type)
throws UnnamedPackageException {
PackageElement pkg = elementUtils.getPackageOf(type);
if (pkg.isUnnamed()) {
throw new UnnamedPackageException(type);
}
return pkg.getQualifiedName().toString();
}
示例9: PreferenceKeyField
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
public PreferenceKeyField(@NonNull VariableElement variableElement, @NonNull Elements elementUtils) throws IllegalAccessException {
KeyName annotation_keyName = variableElement.getAnnotation(KeyName.class);
this.variableElement = variableElement;
PackageElement packageElement = elementUtils.getPackageOf(variableElement);
this.packageName = packageElement.isUnnamed() ? null : packageElement.getQualifiedName().toString();
this.typeName = TypeName.get(variableElement.asType());
this.clazzName = variableElement.getSimpleName().toString();
this.value = variableElement.getConstantValue();
setTypeStringName();
if(annotation_keyName != null)
this.keyName = Strings.isNullOrEmpty(annotation_keyName.name()) ? StringUtils.toUpperCamel(this.clazzName) : annotation_keyName.name();
else
this.keyName = StringUtils.toUpperCamel(this.clazzName);
if(this.isObjectField) {
variableElement.getAnnotationMirrors().stream()
.filter(annotationMirror -> TypeName.get(annotationMirror.getAnnotationType()).equals(TypeName.get(TypeConverter.class)))
.forEach(annotationMirror -> {
annotationMirror.getElementValues().forEach((type, value) -> {
String[] split = value.getValue().toString().split("\\.");
StringBuilder builder = new StringBuilder();
for (int i=0; i<split.length-1; i++)
builder.append(split[i] + ".");
this.converterPackage = builder.toString().substring(0, builder.toString().length()-1);
this.converter = split[split.length-1];
});
});
}
if(variableElement.getModifiers().contains(Modifier.PRIVATE)) {
throw new IllegalAccessException(String.format("Field \'%s\' should not be private.", variableElement.getSimpleName()));
} else if(!this.isObjectField && !variableElement.getModifiers().contains(Modifier.FINAL)) {
throw new IllegalAccessException(String.format("Field \'%s\' should be final.", variableElement.getSimpleName()));
}
}
示例10: runDocTreePath
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
/**
* Tests DocTrees.getDocTreePath(PackageElement p, FileObject fo).
*
* @param javaFileName the java anchor file
* @param pkgFileName the package file name
* @throws Exception e if something goes awry
*/
public void runDocTreePath(String javaFileName, String pkgFileName) throws Exception {
List<File> javaFiles = new ArrayList<>();
javaFiles.add(new File(testSrc, javaFileName));
List<File> dirs = new ArrayList<>();
dirs.add(new File(testSrc));
try (StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null)) {
fm.setLocation(javax.tools.StandardLocation.SOURCE_PATH, dirs);
Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(javaFiles);
final JavacTask t = javac.getTask(null, fm, null, null, null, fos);
final DocTrees trees = DocTrees.instance(t);
final Elements elementUtils = t.getElements();
Iterable<? extends Element> elements = t.analyze();
Element klass = elements.iterator().next();
PackageElement pkg = elementUtils.getPackageOf(klass);
FileObject htmlFo = fm.getFileForInput(javax.tools.StandardLocation.SOURCE_PATH,
t.getElements().getPackageOf(klass).getQualifiedName().toString(),
"package.html");
System.out.println();
DocTreePath treePath = trees.getDocTreePath(htmlFo, pkg);
DocCommentTree dcTree = treePath.getDocComment();
StringWriter sw = new StringWriter();
printer.print(dcTree, sw);
String found = sw.toString();
String expected = getExpected(htmlFo.openReader(true));
astcheck(pkgFileName, expected, found);
}
}
示例11: getPackageName
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
private String getPackageName(Elements elements, TypeElement typeElement) throws
NoPackageNameException {
PackageElement pkg = elements.getPackageOf(typeElement);
if (pkg.isUnnamed()) {
throw new NoPackageNameException(typeElement);
}
return pkg.getQualifiedName().toString();
}
示例12: ProxyInfo
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
public ProxyInfo(Elements elementUtils, TypeElement typeElement) {
this.typeElement = typeElement;
PackageElement packageElement = elementUtils.getPackageOf(typeElement);
String packageName = packageElement.getQualifiedName().toString();
this.packageName = packageName;
String className = getClassName(typeElement, packageName);
this.proxyClassName = className + "$$" + VIEW_PROXY;
}
示例13: InjectorGenerator
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
public InjectorGenerator(@NonNull PreferenceComponentAnnotatedClass annotatedClass, @NonNull TypeElement injectedElement, @NonNull Elements elementUtils) {
this.annotatedClazz = annotatedClass;
this.injectedElement = injectedElement;
PackageElement packageElement = elementUtils.getPackageOf(injectedElement);
this.packageName = packageElement.isUnnamed() ? null : packageElement.getQualifiedName().toString();
}
示例14: PreferenceComponentAnnotatedClass
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
public PreferenceComponentAnnotatedClass(@NonNull TypeElement annotatedElement, @NonNull Elements elementUtils, @NonNull Map<String, String> annotatedEntityTypeMap) throws VerifyException {
PackageElement packageElement = elementUtils.getPackageOf(annotatedElement);
this.packageName = packageElement.isUnnamed() ? null : packageElement.getQualifiedName().toString();
this.annotatedElement = annotatedElement;
this.typeName = TypeName.get(annotatedElement.asType());
this.clazzName = annotatedElement.getSimpleName().toString();
this.entities = new ArrayList<>();
this.keyNames = new ArrayList<>();
this.generatedClazzList = new ArrayList<>();
annotatedElement.getEnclosedElements().stream()
.filter(element -> element instanceof ExecutableElement)
.map(element -> (ExecutableElement) element)
.forEach(method -> {
MethodSpec methodSpec = MethodSpec.overriding(method).build();
if(methodSpec.returnType != TypeName.get(Void.TYPE)) {
throw new VerifyException(String.format("return type should be void : '%s' method with return type '%s'", methodSpec.name, methodSpec.returnType));
} else if(methodSpec.parameters.size() > 1) {
throw new VerifyException(String.format("length of parameter should be 1 : '%s' method with parameters '%s'", methodSpec.name, methodSpec.parameters.toString()));
}
});
Set<String> entitySet = new HashSet<>();
annotatedElement.getAnnotationMirrors().stream()
.filter(annotationMirror -> TypeName.get(annotationMirror.getAnnotationType()).equals(TypeName.get(PreferenceComponent.class)))
.forEach(annotationMirror -> {
annotationMirror.getElementValues().forEach((type, value) -> {
String[] values = value.getValue().toString().split(",");
List<String> valueList = Arrays.asList(values);
entitySet.addAll(valueList);
});
});
entitySet.forEach(value -> {
if(!annotatedEntityTypeMap.containsKey(value))
throw new VerifyException(String.format("%s is not a preference entity.", value));
else {
keyNames.add(annotatedEntityTypeMap.get(value));
generatedClazzList.add(ENTITY_PREFIX + annotatedEntityTypeMap.get(value));
}
});
entities.addAll(entitySet);
}
示例15: KickbackBoxAnnotatedClass
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
public KickbackBoxAnnotatedClass(TypeElement annotatedElement, Elements elementUtils) throws VerifyException {
KickbackBox kickbackBox = annotatedElement.getAnnotation(KickbackBox.class);
PackageElement packageElement = elementUtils.getPackageOf(annotatedElement);
this.packageName = packageElement.isUnnamed() ? null : packageElement.getQualifiedName().toString();
this.annotatedElement = annotatedElement;
this.clazzName = annotatedElement.getSimpleName().toString();
this.boxName = Strings.isNullOrEmpty(kickbackBox.name()) ? StringUtils.toUpperCamel(this.clazzName) : kickbackBox.name();
this.kickbackElementList = new ArrayList<>();
this.kickbackInjectionList = new ArrayList<>();
Map<String, String> checkMap = new HashMap<>();
annotatedElement.getEnclosedElements().stream()
.filter(element -> element instanceof VariableElement)
.map(element -> (VariableElement) element)
.forEach(variable -> {
KickbackElementClass kickbackElement = new KickbackElementClass(variable, elementUtils);
if(checkMap.get(kickbackElement.elementName) != null) {
throw new VerifyException(String.format("\'%s\' key is already used in class.", kickbackElement.elementName));
}
checkMap.put(kickbackElement.elementName, kickbackElement.clazzName);
kickbackElementList.add(kickbackElement);
});
annotatedElement.getEnclosedElements().stream()
.filter(element -> element instanceof ExecutableElement)
.filter(element -> !element.getSimpleName().equals(annotatedElement.getSimpleName()))
.skip(1)
.map(element -> (ExecutableElement) element)
.forEach(method -> {
MethodSpec methodSpec = MethodSpec.overriding(method).build();
if(methodSpec.returnType != TypeName.get(Void.TYPE)) {
throw new VerifyException(String.format("return type must be void : '%s' method with return type '%s'", methodSpec.name, methodSpec.returnType));
} else if(methodSpec.parameters.size() > 1 || methodSpec.parameters.size() == 0) {
throw new VerifyException(String.format("length of parameter must be 1 : '%s' method with parameters '%s'", methodSpec.name, methodSpec.parameters.toString()));
}
ParameterSpec parameterSpec = methodSpec.parameters.get(0);
TypeElement injectedElement = elementUtils.getTypeElement(parameterSpec.type.toString());
kickbackInjectionList.add(injectedElement);
});
}