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


Java ElementFilter.constructorsIn方法代碼示例

本文整理匯總了Java中javax.lang.model.util.ElementFilter.constructorsIn方法的典型用法代碼示例。如果您正苦於以下問題:Java ElementFilter.constructorsIn方法的具體用法?Java ElementFilter.constructorsIn怎麽用?Java ElementFilter.constructorsIn使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.lang.model.util.ElementFilter的用法示例。


在下文中一共展示了ElementFilter.constructorsIn方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: process

import javax.lang.model.util.ElementFilter; //導入方法依賴的package包/類
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    for (TypeElement root : ElementFilter.typesIn(roundEnv.getRootElements())) {
        System.out.println(processingEnv.getElementUtils().getBinaryName(root));
        for (ExecutableElement constr : ElementFilter.constructorsIn(root.getEnclosedElements())) {
            System.out.print("  (");
            boolean first = true;
            for (VariableElement param : constr.getParameters()) {
                if (!first) {
                    System.out.print(", ");
                }
                first = false;
                System.out.print(param.asType().toString());
            }
            System.out.println(")");
        }
    }

    return false;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:21,代碼來源:LocalClassesModel.java

示例2: run

import javax.lang.model.util.ElementFilter; //導入方法依賴的package包/類
public void run(WorkingCopy copy) throws Exception {
    copy.toPhase(JavaSource.Phase.RESOLVED);
    TreePath tp = copy.getTreeUtilities().pathFor(offset);
    assertTrue(TreeUtilities.CLASS_TREE_KINDS.contains(tp.getLeaf().getKind()));
    ClassTree ct = (ClassTree)tp.getLeaf();
    TypeElement te = (TypeElement)copy.getTrees().getElement(tp);
    assertNotNull(te);
    List<? extends VariableElement> vars = ElementFilter.fieldsIn(te.getEnclosedElements());
    TypeElement sup = (TypeElement)((DeclaredType)te.getSuperclass()).asElement();
    assertNotNull(sup);
    List<? extends ExecutableElement> ctors = sup.getQualifiedName().contentEquals("java.lang.Object")
            ? null : ElementFilter.constructorsIn(sup.getEnclosedElements());
    if (ctors != null)
        assertEquals(numCtors, ctors.size());
    GeneratorUtilities utilities = GeneratorUtilities.get(copy);
    assertNotNull(utilities);
    ClassTree newCt = utilities.insertClassMember(ct, utilities.createConstructor(te, vars, ctors != null ? ctors.get(ctorToUse) : null));
    copy.rewrite(ct, newCt);
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:20,代碼來源:GeneratorUtilitiesTest.java

示例3: verify

import javax.lang.model.util.ElementFilter; //導入方法依賴的package包/類
void verify(TypeElement node) {
    scanFields(node);

    boolean foundValidConstructor = false;
    for (ExecutableElement constructor : ElementFilter.constructorsIn(node.getEnclosedElements())) {
        if (constructor.getModifiers().contains(PRIVATE)) {
            continue;
        } else if (!constructor.getModifiers().contains(PUBLIC) && !constructor.getModifiers().contains(PROTECTED)) {
            throw new ElementException(constructor, "Node class constructor must be public or protected");
        }

        foundValidConstructor = true;
    }

    if (!foundValidConstructor) {
        throw new ElementException(node, "Node class must have at least one protected constructor");
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:19,代碼來源:GraphNodeVerifier.java

示例4: findExecutableMember

import javax.lang.model.util.ElementFilter; //導入方法依賴的package包/類
private ExecutableElement findExecutableMember(TypeElement clazz, String methodName, List<TypeMirror> paramTypes, Types types) {
    List<ExecutableElement> members = methodName.contentEquals(clazz.getSimpleName())
            ? ElementFilter.constructorsIn(clazz.getEnclosedElements())
            : ElementFilter.methodsIn(clazz.getEnclosedElements());
    outer: for (ExecutableElement ee : members) {
        if (ee.getKind() == ElementKind.CONSTRUCTOR || methodName.contentEquals(ee.getSimpleName())) {
            List<? extends TypeMirror> memberParamTypes = ((ExecutableType) ee.asType()).getParameterTypes();
            if (memberParamTypes.size() == paramTypes.size()) {
                Iterator<TypeMirror> it = paramTypes.iterator();
                for (TypeMirror memberParamType : memberParamTypes) {
                    TypeMirror type = it.next();
                    if (types.isSameType(type, memberParamType)) {
                        continue outer;
                    }
                }
                return ee;
            }
        }
    }
    return null;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:22,代碼來源:JavaReference.java

示例5: findAccessibleConstructor

import javax.lang.model.util.ElementFilter; //導入方法依賴的package包/類
/**
 * Finds an non-private constructor in the given class.
 * It tries to find the following types of constructors, in this order:
 * <ol>
 *     <li>no-arg constructor</li>
 *     <li>one-argument constructor without throws-clause</li>
 *     <li>one-argument constructor with a throws-clause</li>
 *     <li>two-argument constructor without throws-clause</li>
 *     <li>two-argument constructor with a throws-clause</li>
 *     <li>etc.</li>
 * </ol>
 *
 * @param  clazz  class to find constructor in
 * @return  accessible (non-private) constructor,
 *          or {@code null} if none was found
 */
private static ExecutableElement findAccessibleConstructor(TypeElement clazz) {
    List<ExecutableElement> ctors
            = ElementFilter.constructorsIn(clazz.getEnclosedElements());
    if (ctors.isEmpty()) {
        return null;
    }

    ExecutableElement best = null;
    int bestArgsCount = -1;
    boolean bestHasThrown = false;

    for (ExecutableElement ctor : ctors) {
        if (ctor.getModifiers().contains(PRIVATE)) {
            continue;
        }

        List args = ctor.getParameters();
        if (args.isEmpty()) {
            return ctor;        //this is the best constructor we could find
        }

        int argsCount = args.size();
        boolean hasThrown = !ctor.getThrownTypes().isEmpty();
        if ((best == null)
                || (argsCount < bestArgsCount)
                || (argsCount == bestArgsCount) && bestHasThrown && !hasThrown) {
            best = ctor;
            bestArgsCount = argsCount;
            bestHasThrown = hasThrown;
        }
    }
    return best;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:50,代碼來源:AbstractTestGenerator.java

示例6: run

import javax.lang.model.util.ElementFilter; //導入方法依賴的package包/類
@Override
public List<Fix> run(CompilationInfo info, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
    if (!TreeUtilities.CLASS_TREE_KINDS.contains(treePath.getLeaf().getKind())) return Collections.emptyList();
    
    Element el = info.getTrees().getElement(treePath);
    
    if (el == null || !el.getKind().isClass()) return Collections.emptyList();
    
    TypeElement clazz = (TypeElement) el;
    TypeMirror superType = clazz.getSuperclass();
    
    if (superType.getKind() != TypeKind.DECLARED) return Collections.emptyList();
    
    TypeElement superClazz = (TypeElement) info.getTypes().asElement(superType);
    DeclaredType targetType = (DeclaredType) clazz.asType();
    Scope classScope = info.getTrees().getScope(treePath);
    List<Fix> result = new ArrayList<Fix>();
    
    for (ExecutableElement constr : ElementFilter.constructorsIn(superClazz.getEnclosedElements())) {
        if (!info.getTrees().isAccessible(classScope, constr, (DeclaredType) superType)) continue;
        
        StringBuilder name = new StringBuilder();
        
        name.append(clazz.getSimpleName()).append("(");
        
        ExecutableType target = (ExecutableType) info.getTypes().asMemberOf(targetType, constr);
        boolean firstParam = true;
        
        for (TypeMirror p : target.getParameterTypes()) {
            if (!firstParam) name.append(", ");
            firstParam = false;
            name.append(info.getTypeUtilities().getTypeName(p));
        }
        
        name.append(")");
        result.add(new FixImpl(info, treePath, constr, StringEscapeUtils.escapeHtml(name.toString())).toEditorFix());
    }
    
    return result;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:41,代碼來源:AddConstructor.java

示例7: validate

import javax.lang.model.util.ElementFilter; //導入方法依賴的package包/類
public void validate(CompilationInfo info) {
    TypeElement test = info.getElements().getTypeElement("test.Test");
    VariableElement var = ElementFilter.fieldsIn(test.getEnclosedElements()).get(0);
    TypeElement sup = (TypeElement)((DeclaredType)test.getSuperclass()).asElement();
    ExecutableElement supCtor = sup.getQualifiedName().contentEquals("java.lang.Object")
            ? null : ElementFilter.constructorsIn(sup.getEnclosedElements()).get(0);

    List<? extends ExecutableElement> ctors = ElementFilter.constructorsIn(test.getEnclosedElements());
    assertEquals(1, ctors.size());
    ExecutableElement ctor = ctors.get(0);

    assertEquals(supCtor == null ? 1 : 2, ctor.getParameters().size());
    if (supCtor != null)
        assertEquals(supCtor.isVarArgs(), ctor.isVarArgs());
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:16,代碼來源:GeneratorUtilitiesTest.java

示例8: findAccessibleConstructor

import javax.lang.model.util.ElementFilter; //導入方法依賴的package包/類
/**
 * Finds an non-private constructor in the given class.
 * It tries to find the following types of constructors, in this order:
 * <ol>
 *     <li>no-arg constructor</li>
 *     <li>one-argument constructor without throws-clause</li>
 *     <li>one-argument constructor with a throws-clause</li>
 *     <li>two-argument constructor without throws-clause</li>
 *     <li>two-argument constructor with a throws-clause</li>
 *     <li>etc.</li>
 * </ol>
 *
 * @param  clazz  class to find constructor in
 * @return  accessible (non-private) constructor,
 *          or {@literal null} if none was found
 */
private static ExecutableElement findAccessibleConstructor(TypeElement clazz) {
    List<ExecutableElement> ctors
            = ElementFilter.constructorsIn(clazz.getEnclosedElements());
    if (ctors.isEmpty()) {
        return null;
    }

    ExecutableElement best = null;
    int bestArgsCount = -1;
    boolean bestHasThrown = false;

    for (ExecutableElement ctor : ctors) {
        if (ctor.getModifiers().contains(PRIVATE)) {
            continue;
        }

        List args = ctor.getParameters();
        if (args.isEmpty()) {
            return ctor;        //this is the best constructor we could find
        }

        int argsCount = args.size();
        boolean hasThrown = !ctor.getThrownTypes().isEmpty();
        if ((best == null)
                || (argsCount < bestArgsCount)
                || (argsCount == bestArgsCount) && bestHasThrown && !hasThrown) {
            best = ctor;
            bestArgsCount = argsCount;
            bestHasThrown = hasThrown;
        }
    }
    return best;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:50,代碼來源:AbstractTestGenerator.java

示例9: hasDefaultConstructor

import javax.lang.model.util.ElementFilter; //導入方法依賴的package包/類
public boolean hasDefaultConstructor(TypeElement t) {
    if (t == null || !t.getKind().equals(ElementKind.CLASS))
        return false;

    for (ExecutableElement init : ElementFilter.constructorsIn(env.getElementUtils().getAllMembers(t))) {
        if (init.getParameters().isEmpty())
            return true;
    }
    return false;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:11,代碼來源:ApNavigator.java

示例10: getLongestContructor

import javax.lang.model.util.ElementFilter; //導入方法依賴的package包/類
public static ExecutableElement getLongestContructor(TypeElement typeElement) {
    List<ExecutableElement> constructors = ElementFilter.constructorsIn(typeElement.getEnclosedElements());
    int max = -1;
    ExecutableElement current = null;
    for (ExecutableElement constructor : constructors) {
        if (constructor.getParameters().size() > max) {
            max = constructor.getParameters().size();
            current = constructor;
        }
    }
    return current;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:13,代碼來源:JavaSourceHelper.java

示例11: findConstructors

import javax.lang.model.util.ElementFilter; //導入方法依賴的package包/類
private List<ExecutableElement> findConstructors(TypeElement nodeClass, TypeMirror[] signature, Map<ExecutableElement, String> nonMatches, boolean requiresInjectedStamp) {
    List<ExecutableElement> constructors = ElementFilter.constructorsIn(nodeClass.getEnclosedElements());
    List<ExecutableElement> found = new ArrayList<>(constructors.size());
    for (ExecutableElement constructor : constructors) {
        if (matchSignature(0, constructor, signature, nonMatches, requiresInjectedStamp)) {
            found.add(constructor);
        }
    }
    return found;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:11,代碼來源:NodeIntrinsicVerifier.java

示例12: getDefaultConstructor

import javax.lang.model.util.ElementFilter; //導入方法依賴的package包/類
public static MethodTree getDefaultConstructor(CompilationController controller) {
    TypeElement classElement = getTopLevelClassElement(controller);
    List<ExecutableElement> constructors = ElementFilter.constructorsIn(classElement.getEnclosedElements());

    for (ExecutableElement constructor : constructors) {
        if (constructor.getParameters().size() == 0) {
            return controller.getTrees().getTree(constructor);
        }
    }

    return null;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:13,代碼來源:JavaSourceHelper.java

示例13: collectIncludedBy

import javax.lang.model.util.ElementFilter; //導入方法依賴的package包/類
void collectIncludedBy(PackageElement element) {
  final DeclaringPackage declaringPackage = interners.forPackage(ImmutableProto.DeclaringPackage.builder()
      .environment(environment())
      .interner(interners)
      .element(wrapElement(element))
      .build());

  if (declaringPackage.include().isPresent()) {
    for (TypeElement sourceElement : declaringPackage.includedTypes()) {
      builder.add(interners.forProto(
          ImmutableProto.Protoclass.builder()
              .environment(environment())
              .packageOf(declaringPackage)
              .sourceElement(wrapElement(sourceElement))
              .kind(Kind.INCLUDED_IN_PACKAGE)
              .build()));
    }
  }

  if (declaringPackage.builderInclude().isPresent()) {
    for (TypeElement includedType : declaringPackage.builderIncludedTypes()) {
      for (ExecutableElement m : ElementFilter.methodsIn(includedType.getEnclosedElements())) {
        if (DeclaringType.suitableForBuilderFactory(m)) {
          builder.add(interners.forProto(
              ImmutableProto.Protoclass.builder()
                  .environment(environment())
                  .packageOf(declaringPackage)
                  .sourceElement(wrapElement(m))
                  .kind(Kind.INCLUDED_FACTORY_IN_PACKAGE)
                  .build()));
        }
      }
      for (ExecutableElement c : ElementFilter.constructorsIn(includedType.getEnclosedElements())) {
        if (DeclaringType.suitableForBuilderConstructor(c)) {
          builder.add(interners.forProto(
              ImmutableProto.Protoclass.builder()
                  .environment(environment())
                  .packageOf(declaringPackage)
                  .sourceElement(wrapElement(c))
                  .kind(Kind.INCLUDED_CONSTRUCTOR_IN_PACKAGE)
                  .build()));
          // stop on first suitable
          // don't have any good idea how to handle multiple
          // constructor. CBuilder, C2Builder, C3Builder for
          // class C seems even more crazy than stop on first suitable
          // but this is debatable
          break;
        }
      }
    }
  }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:53,代碼來源:Round.java

示例14: isLegalImplementation

import javax.lang.model.util.ElementFilter; //導入方法依賴的package包/類
protected boolean isLegalImplementation(WebService webService, TypeElement classElement) {
    boolean isStateful = isStateful(classElement);

    Collection<Modifier> modifiers = classElement.getModifiers();
    if (!modifiers.contains(Modifier.PUBLIC)) {
        builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_CLASS_NOT_PUBLIC(classElement.getQualifiedName()), classElement);
        return false;
    }
    if (modifiers.contains(Modifier.FINAL) && !isStateful) {
        builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_CLASS_IS_FINAL(classElement.getQualifiedName()), classElement);
        return false;
    }
    if (modifiers.contains(Modifier.ABSTRACT) && !isStateful) {
        builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_CLASS_IS_ABSTRACT(classElement.getQualifiedName()), classElement);
        return false;
    }
    boolean hasDefaultConstructor = false;
    for (ExecutableElement constructor : ElementFilter.constructorsIn(classElement.getEnclosedElements())) {
        if (constructor.getModifiers().contains(Modifier.PUBLIC) &&
                constructor.getParameters().isEmpty()) {
            hasDefaultConstructor = true;
            break;
        }
    }
    if (!hasDefaultConstructor && !isStateful) {
        if (classElement.getEnclosingElement() != null && !modifiers.contains(Modifier.STATIC)) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_CLASS_IS_INNERCLASS_NOT_STATIC(
                    classElement.getQualifiedName()), classElement);
            return false;
        }

        builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_NO_DEFAULT_CONSTRUCTOR(
                classElement.getQualifiedName()), classElement);
        return false;
    }
    if (webService.endpointInterface().isEmpty()) {
        if (!methodsAreLegal(classElement))
            return false;
    } else {
        TypeElement interfaceElement = getEndpointInterfaceElement(webService.endpointInterface(), classElement);
        if (!classImplementsSei(classElement, interfaceElement))
            return false;
    }

    return true;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:47,代碼來源:WebServiceVisitor.java

示例15: apply

import javax.lang.model.util.ElementFilter; //導入方法依賴的package包/類
@TriggerPatterns(value = {
    @TriggerPattern(value = JPAAnnotations.ENTITY),
    @TriggerPattern(value = JPAAnnotations.EMBEDDABLE),
    @TriggerPattern(value = JPAAnnotations.ID_CLASS)})
public static ErrorDescription apply(HintContext hc) {
    if (hc.isCanceled() || (hc.getPath().getLeaf().getKind() != Tree.Kind.IDENTIFIER || hc.getPath().getParentPath().getLeaf().getKind() != Tree.Kind.ANNOTATION)) {//NOI18N
        return null;//we pass only if it is an annotation
    }
    
    JPAProblemContext ctx = ModelUtils.getOrCreateCachedContext(hc);
    if (ctx == null || hc.isCanceled()) {
        return null;
    }

    TypeElement subject = ctx.getJavaClass();
    
    // If the class is not public, no need to give this warning, yet. 
    // See issue 110170
    if (!subject.getModifiers().contains(Modifier.PUBLIC)) {
        return null;
    }

    boolean hasDefaultContructor = true;

    for (ExecutableElement constr : ElementFilter.constructorsIn(subject.getEnclosedElements())) {
        hasDefaultContructor = false;

        if (constr.getParameters().isEmpty()
                && (constr.getModifiers().contains(Modifier.PUBLIC)
                || constr.getModifiers().contains(Modifier.PROTECTED))) {
            return null; // found appropriate constructor
        }
    }

    if (hasDefaultContructor) {
        return null; // OK
    }

    Fix fix = new CreateDefaultConstructor(ctx.getFileObject(),
            ElementHandle.create(ctx.getJavaClass()));
    
    TreePath par = hc.getPath();
    while(par!=null && par.getParentPath()!=null && par.getLeaf().getKind()!= Tree.Kind.CLASS){
        par = par.getParentPath();
    }
    
    Utilities.TextSpan underlineSpan = Utilities.getUnderlineSpan(
                       ctx.getCompilationInfo(), par.getLeaf());

    return ErrorDescriptionFactory.forSpan(
                hc,
                underlineSpan.getStartOffset(),
                underlineSpan.getEndOffset(),
                NbBundle.getMessage(HasNoArgConstructor.class, "MSG_HasNoNoArgConstructor"),
                fix);
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:57,代碼來源:HasNoArgConstructor.java


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