當前位置: 首頁>>代碼示例>>Java>>正文


Java Elements.getPackageOf方法代碼示例

本文整理匯總了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;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:27,代碼來源:ElementJavadoc.java

示例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);
        }
    }
}
 
開發者ID:foreveruseful,項目名稱:smartkey,代碼行數:18,代碼來源:ClassDescription.java

示例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();
}
 
開發者ID:skydoves,項目名稱:Kickback,代碼行數:26,代碼來源:KickbackElementClass.java

示例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();
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:8,代碼來源:Utils.java

示例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);
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:18,代碼來源:EncapsulateFieldRefactoringPlugin.java

示例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();
}
 
開發者ID:jacklongway,項目名稱:FactoryAnnotation,代碼行數:38,代碼來源:FactoryGroupedClasses.java

示例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
  }
}
 
開發者ID:globulus,項目名稱:easyprefs,代碼行數:9,代碼來源:TypeUtils.java

示例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();
}
 
開發者ID:Comcast,項目名稱:resourceprovider,代碼行數:9,代碼來源:Utils.java

示例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()));
    }
}
 
開發者ID:skydoves,項目名稱:PreferenceRoom,代碼行數:37,代碼來源:PreferenceKeyField.java

示例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);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:43,代碼來源:DocCommentTreeApiTester.java

示例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();
}
 
開發者ID:weizongwei5,項目名稱:GeneratorX,代碼行數:10,代碼來源:GeneratorProcessor.java

示例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;
}
 
開發者ID:hanhailong,項目名稱:CompileAnnotationDemo,代碼行數:12,代碼來源:ProxyInfo.java

示例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();
}
 
開發者ID:skydoves,項目名稱:PreferenceRoom,代碼行數:7,代碼來源:InjectorGenerator.java

示例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);
}
 
開發者ID:skydoves,項目名稱:PreferenceRoom,代碼行數:45,代碼來源:PreferenceComponentAnnotatedClass.java

示例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);
            });
}
 
開發者ID:skydoves,項目名稱:Kickback,代碼行數:44,代碼來源:KickbackBoxAnnotatedClass.java


注:本文中的javax.lang.model.util.Elements.getPackageOf方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。