Java Code Examples for javax.lang.model.element.TypeElement.equals()

The following are Jave code examples for showing how to use equals() of the javax.lang.model.element.TypeElement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: JavadocUtilities.java   View Source Code Vote up 6 votes
public static AnnotationMirror findDeprecated(CompilationInfo javac, Element elm) {
    TypeElement deprAnn = javac.getElements().getTypeElement("java.lang.Deprecated"); //NOI18N
    if (deprAnn == null) {
        String msg = String.format("Even though the source level of %s" + //NOI18N
                " is set to JDK5 or later, java.lang.Deprecated cannot" + //NOI18N
                " be found on the bootclasspath: %s", //NOI18N
                javac.getClasspathInfo().getClassPath(PathKind.SOURCE),
                javac.getClasspathInfo().getClassPath(PathKind.BOOT));
        Logger.getLogger(JavadocUtilities.class.getName()).warning(msg);
        return null;
    }
    for (AnnotationMirror annotationMirror : javac.getElements().getAllAnnotationMirrors(elm)) {
        if (deprAnn.equals(annotationMirror.getAnnotationType().asElement())) {
            return annotationMirror;
        }
    }
    return null;
}
 
Example 2
Project: qualifier   File: StaticQualifierMetamodelProcessor.java   View Source Code Vote up 6 votes
/** The super qualifier user to extend this property metadata. */
@Override public @Nullable PropertyReference extend() {
    if (mixinMetamodel != null && mixinProperty != null) {
        return new PropertyReference(mixinMetamodel, mixinProperty);
    }

    // apply bean Qualify(mixin) if this property has no explicit @Extend
    if (isProperty()) {
        Qualify beanQualify = beanElement.getAnnotation(Qualify.class);
        TypeElement classRepresenter = qualifyExtendValue(beanQualify);
        if (classRepresenter != null && !classRepresenter.equals(typeElementFor(Object.class))) {
            return new PropertyReference(
                    ClassName.bestGuess(getQualifierName(getFlatName(classRepresenter))),
                    isNullOrEmpty(mixinProperty) ? name : mixinProperty);
        }
    }

    return null;
}
 
Example 3
Project: java-code-templates   File: Inventory.java   View Source Code Vote up 6 votes
@Override
public ImmutableMap.Builder<TypeName,TypeName> visitType(final TypeElement element, final ImmutableMap.Builder<TypeName,TypeName> builder) {

	if (Util.isDeclaredType(element) && !element.equals(objectTypeElement)) {
		if (traverseParentTypes) {
			// Climb the inheritance DAGs for interfaces and classes
			visitIfDeclaredType(element.getSuperclass(), builder);
			element.getInterfaces().stream().forEach(face -> visitIfDeclaredType(face, builder));
		}

		// Capture the current type
		final ClassName type = ClassName.get(element);
		if (!alreadyComputed.contains(type)) { // Ensure that we don't cover old ground
			builder.put(type, computeSubstitution(type));
		}

		// Continue the search for nested types
		element.getEnclosedElements().forEach(child -> visit(child, builder));
	}

	return builder;
}
 
Example 4
Project: incubator-netbeans   File: PullUpTransformer.java   View Source Code Vote up 5 votes
static ModifiersTree removeAnnotations(WorkingCopy workingCopy, TreeMaker make,ModifiersTree oldOne, TreePath path) {
    TypeElement override = workingCopy.getElements().getTypeElement("java.lang.Override");
    if(override == null) {
        return oldOne;
    }
    List<AnnotationTree> newAnnotations = new LinkedList<>();
    for (AnnotationTree annotationTree : oldOne.getAnnotations()) {
        Element el = workingCopy.getTrees().getElement(new TreePath(path, annotationTree));
        if(!override.equals(el)) {
            newAnnotations.add(annotationTree);
        }
    }
    return make.Modifiers(oldOne, newAnnotations);
}
 
Example 5
Project: incubator-netbeans   File: Utilities.java   View Source Code Vote up 5 votes
public static Visibility getAccessModifiers(@NonNull CompilationInfo info, @NullAllowed TypeElement source, @NonNull TypeElement target) {
    if (target.getKind().isInterface()) {
        return Visibility.PUBLIC;
    }

    TypeElement outterMostSource = source != null ? info.getElementUtilities().outermostTypeElement(source) : null;
    TypeElement outterMostTarget = info.getElementUtilities().outermostTypeElement(target);

    if (outterMostTarget.equals(outterMostSource)) {
        return Visibility.PRIVATE;
    }

    Element sourcePackage;

    if (outterMostSource != null) {
        sourcePackage = outterMostSource.getEnclosingElement();
    } else if (info.getCompilationUnit().getPackageName() != null) {
        sourcePackage = info.getTrees().getElement(new TreePath(new TreePath(info.getCompilationUnit()), info.getCompilationUnit().getPackageName()));
    } else {
        sourcePackage = info.getElements().getPackageElement("");
    }

    Element targetPackage = outterMostTarget.getEnclosingElement();

    if (sourcePackage != null && sourcePackage.equals(targetPackage)) {
        return Visibility.PACKAGE_PRIVATE;
    }

    //TODO: protected?
    return Visibility.PUBLIC;
}
 
Example 6
Project: OpenJSharp   File: WebServiceVisitor.java   View Source Code Vote up 5 votes
protected void preProcessWebService(WebService webService, TypeElement element) {
    processedMethods = new HashSet<String>();
    seiContext = context.getSeiContext(element);
    String targetNamespace = null;
    if (webService != null)
        targetNamespace = webService.targetNamespace();
    PackageElement packageElement = builder.getProcessingEnvironment().getElementUtils().getPackageOf(element);
    if (targetNamespace == null || targetNamespace.length() == 0) {
        String packageName = packageElement.getQualifiedName().toString();
        if (packageName == null || packageName.length() == 0) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_NO_PACKAGE_CLASS_MUST_HAVE_TARGETNAMESPACE(
                    element.getQualifiedName()), element);
        }
        targetNamespace = RuntimeModeler.getNamespace(packageName);
    }
    seiContext.setNamespaceUri(targetNamespace);
    if (serviceImplName == null)
        serviceImplName = seiContext.getSeiImplName();
    if (serviceImplName != null) {
        seiContext.setSeiImplName(serviceImplName);
        context.addSeiContext(serviceImplName, seiContext);
    }
    portName = ClassNameInfo.getName(element.getSimpleName().toString().replace('$', '_'));
    packageName = packageElement.getQualifiedName();
    portName = webService != null && webService.name() != null && webService.name().length() > 0 ?
            webService.name() : portName;
    serviceName = ClassNameInfo.getName(element.getQualifiedName().toString()) + WebServiceConstants.SERVICE.getValue();
    serviceName = webService != null && webService.serviceName() != null && webService.serviceName().length() > 0 ?
            webService.serviceName() : serviceName;
    wsdlNamespace = seiContext.getNamespaceUri();
    typeNamespace = wsdlNamespace;

    SOAPBinding soapBinding = element.getAnnotation(SOAPBinding.class);
    if (soapBinding != null) {
        pushedSoapBinding = pushSoapBinding(soapBinding, element, element);
    } else if (element.equals(typeElement)) {
        pushedSoapBinding = pushSoapBinding(new MySoapBinding(), element, element);
    }
}
 
Example 7
Project: qualifier   File: StaticQualifierMetamodelProcessor.java   View Source Code Vote up 5 votes
private void processAnnotated(Element element) {
    Qualify.Extend extend = element.getAnnotation(Qualify.Extend.class);
    if (extend != null) {
        if (mixinMetamodel != null) print(Kind.ERROR, "multiple mixin definitions", element);
        TypeElement mixin = qualifyExtendValue(extend);
        if (!mixin.equals(typeElementFor(Object.class))) {
            mixinMetamodel = ClassName.bestGuess(getQualifierName(getFlatName(mixin)));
        }
        mixinProperty = extend.name().isEmpty() ? name : extend.name();
    }
    getProviders().forEach(extension -> extension.processAnnotated(element, metadata()));
}
 
Example 8
Project: openjdk-jdk10   File: VerifierAnnotationProcessor.java   View Source Code Vote up 5 votes
public static AnnotationMirror findAnnotationMirror(ProcessingEnvironment processingEnv, List<? extends AnnotationMirror> mirrors, Class<?> annotationClass) {
    TypeElement expectedAnnotationType = processingEnv.getElementUtils().getTypeElement(annotationClass.getCanonicalName());
    for (AnnotationMirror mirror : mirrors) {
        DeclaredType annotationType = mirror.getAnnotationType();
        TypeElement actualAnnotationType = (TypeElement) annotationType.asElement();
        if (actualAnnotationType.equals(expectedAnnotationType)) {
            return mirror;
        }
    }
    return null;
}
 
Example 9
Project: openjdk-jdk10   File: MethodWriterImpl.java   View Source Code Vote up 5 votes
/**
 * {@inheritDoc}
 */
@Override
public void addComments(TypeMirror holderType, ExecutableElement method, Content methodDocTree) {
    TypeElement holder = utils.asTypeElement(holderType);
    if (!utils.getFullBody(method).isEmpty()) {
        if (holder.equals(typeElement) ||
                !(utils.isPublic(holder) ||
                utils.isLinkable(holder))) {
            writer.addInlineComment(method, methodDocTree);
        } else {
            Content link =
                    writer.getDocLink(LinkInfoImpl.Kind.EXECUTABLE_ELEMENT_COPY,
                    holder, method,
                    utils.isIncluded(holder)
                            ? utils.getSimpleName(holder)
                            : utils.getFullyQualifiedName(holder),
                        false);
            Content codelLink = HtmlTree.CODE(link);
            Content descfrmLabel = HtmlTree.SPAN(HtmlStyle.descfrmTypeLabel,
                    utils.isClass(holder)
                            ? contents.descfrmClassLabel
                            : contents.descfrmInterfaceLabel);
            descfrmLabel.addContent(Contents.SPACE);
            descfrmLabel.addContent(codelLink);
            methodDocTree.addContent(HtmlTree.DIV(HtmlStyle.block, descfrmLabel));
            writer.addInlineComment(method, methodDocTree);
        }
    }
}
 
Example 10
Project: openjdk-jdk10   File: PropertyWriterImpl.java   View Source Code Vote up 5 votes
/**
 * {@inheritDoc}
 */
@Override
public void addComments(ExecutableElement property, Content propertyDocTree) {
    TypeElement holder = (TypeElement)property.getEnclosingElement();
    if (!utils.getFullBody(property).isEmpty()) {
        if (holder.equals(typeElement) ||
                (!utils.isPublic(holder) || utils.isLinkable(holder))) {
            writer.addInlineComment(property, propertyDocTree);
        } else {
            Content link =
                    writer.getDocLink(LinkInfoImpl.Kind.PROPERTY_COPY,
                    holder, property,
                    utils.isIncluded(holder)
                            ? holder.getSimpleName() : holder.getQualifiedName(),
                        false);
            Content codeLink = HtmlTree.CODE(link);
            Content descfrmLabel = HtmlTree.SPAN(HtmlStyle.descfrmTypeLabel,
                    utils.isClass(holder)
                            ? contents.descfrmClassLabel
                            : contents.descfrmInterfaceLabel);
            descfrmLabel.addContent(Contents.SPACE);
            descfrmLabel.addContent(codeLink);
            propertyDocTree.addContent(HtmlTree.DIV(HtmlStyle.block, descfrmLabel));
            writer.addInlineComment(property, propertyDocTree);
        }
    }
}
 
Example 11
Project: openjdk-jdk10   File: WebServiceVisitor.java   View Source Code Vote up 5 votes
protected void preProcessWebService(WebService webService, TypeElement element) {
    processedMethods = new HashSet<String>();
    seiContext = context.getSeiContext(element);
    String targetNamespace = null;
    if (webService != null)
        targetNamespace = webService.targetNamespace();
    PackageElement packageElement = builder.getProcessingEnvironment().getElementUtils().getPackageOf(element);
    if (targetNamespace == null || targetNamespace.length() == 0) {
        String packageName = packageElement.getQualifiedName().toString();
        if (packageName == null || packageName.length() == 0) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_NO_PACKAGE_CLASS_MUST_HAVE_TARGETNAMESPACE(
                    element.getQualifiedName()), element);
        }
        targetNamespace = RuntimeModeler.getNamespace(packageName);
    }
    seiContext.setNamespaceUri(targetNamespace);
    if (serviceImplName == null)
        serviceImplName = seiContext.getSeiImplName();
    if (serviceImplName != null) {
        seiContext.setSeiImplName(serviceImplName);
        context.addSeiContext(serviceImplName, seiContext);
    }
    portName = ClassNameInfo.getName(element.getSimpleName().toString().replace('$', '_'));
    packageName = packageElement.getQualifiedName();
    portName = webService != null && webService.name() != null && webService.name().length() > 0 ?
            webService.name() : portName;
    serviceName = ClassNameInfo.getName(element.getQualifiedName().toString()) + WebServiceConstants.SERVICE.getValue();
    serviceName = webService != null && webService.serviceName() != null && webService.serviceName().length() > 0 ?
            webService.serviceName() : serviceName;
    wsdlNamespace = seiContext.getNamespaceUri();
    typeNamespace = wsdlNamespace;

    SOAPBinding soapBinding = element.getAnnotation(SOAPBinding.class);
    if (soapBinding != null) {
        pushedSoapBinding = pushSoapBinding(soapBinding, element, element);
    } else if (element.equals(typeElement)) {
        pushedSoapBinding = pushSoapBinding(new MySoapBinding(), element, element);
    }
}
 
Example 12
Project: GitHub   File: AccessorAttributesCollector.java   View Source Code Vote up 4 votes
private void processUtilityCandidateMethod(ExecutableElement utilityMethodCandidate, TypeElement originalType) {
  Name name = utilityMethodCandidate.getSimpleName();
  List<? extends VariableElement> parameters = utilityMethodCandidate.getParameters();

  TypeElement definingType = (TypeElement) utilityMethodCandidate.getEnclosingElement();
  boolean nonFinal = !utilityMethodCandidate.getModifiers().contains(Modifier.FINAL);
  boolean nonAbstract = !utilityMethodCandidate.getModifiers().contains(Modifier.ABSTRACT);

  if (definingType.getQualifiedName().contentEquals(Object.class.getName())) {
    // We ignore methods of java.lang.Object
    return;
  }

  if (name.contentEquals(EQUALS_METHOD)
      && parameters.size() == 1
      && parameters.get(0).asType().toString().equals(Object.class.getName())) {

    if (nonAbstract) {
      type.isEqualToDefined = true;

      if (!definingType.equals(originalType) && hasNonInheritedAttributes && nonFinal) {
        report(originalType)
            .warning("Type inherits overriden 'equals' method but have some non-inherited attributes."
                + " Please override 'equals' with abstract method to have it generate. Otherwise override"
                + " with calling super implemtation to use custom implementation");
      }
    }
    return;
  }

  if (name.contentEquals(HASH_CODE_METHOD)
      && parameters.isEmpty()) {
    if (nonAbstract) {
      type.isHashCodeDefined = true;

      // inherited non-abstract implementation
      if (!definingType.equals(originalType) && hasNonInheritedAttributes && nonFinal) {
        report(originalType)
            .warning("Type inherits non-default 'hashCode' method but have some non-inherited attributes."
                + " Please override 'hashCode' with abstract method to have it generated. Otherwise override"
                + " with calling super implemtation to use custom implementation");
      }
    }
    return;
  }

  if (name.contentEquals(TO_STRING_METHOD)
      && parameters.isEmpty()) {
    if (nonAbstract) {
      type.isToStringDefined = true;

      // inherited non-abstract implementation
      if (!definingType.equals(originalType) && hasNonInheritedAttributes && nonFinal) {
        report(originalType)
            .warning("Type inherits non-default 'toString' method but have some non-inherited attributes."
                + " Please override 'toString' with abstract method to have generate it. Otherwise override"
                + " with calling super implementation to use custom implementation");
      }
    }
    return;
  }
}
 
Example 13
Project: incubator-netbeans   File: CreateRegistrationProcessor.java   View Source Code Vote up 4 votes
@Override
public Iterable<? extends Completion> getCompletions(Element annotated, AnnotationMirror annotation, ExecutableElement attr, String userText) {
    if (processingEnv == null || annotated == null || !annotated.getKind().isClass()) {
        return Collections.emptyList();
    }

    if (   annotation == null
        || !"org.netbeans.api.editor.mimelookup.MimeRegistration".contentEquals(((TypeElement) annotation.getAnnotationType().asElement()).getQualifiedName())) {
        return Collections.emptyList();
    }

    if ("mimeType".contentEquals(attr.getSimpleName())) { // NOI18N
        return completeMimePath(annotated, annotation, attr, userText);
    }
    if (!"service".contentEquals(attr.getSimpleName())) {
        return Collections.emptyList();
    }

    TypeElement jlObject = processingEnv.getElementUtils().getTypeElement("java.lang.Object");

    if (jlObject == null) {
        return Collections.emptyList();
    }

    Collection<Completion> result = new LinkedList<Completion>();
    List<TypeElement> toProcess = new LinkedList<TypeElement>();

    toProcess.add((TypeElement) annotated);

    while (!toProcess.isEmpty()) {
        TypeElement c = toProcess.remove(0);

        result.add(new TypeCompletion(c.getQualifiedName().toString() + ".class"));

        List<TypeMirror> parents = new LinkedList<TypeMirror>();

        parents.add(c.getSuperclass());
        parents.addAll(c.getInterfaces());

        for (TypeMirror tm : parents) {
            if (tm == null || tm.getKind() != TypeKind.DECLARED) {
                continue;
            }

            TypeElement type = (TypeElement) processingEnv.getTypeUtils().asElement(tm);

            if (!jlObject.equals(type)) {
                toProcess.add(type);
            }
        }
    }

    return result;
}
 
Example 14
Project: incubator-netbeans   File: ServiceProviderProcessor.java   View Source Code Vote up 4 votes
@Override
public Iterable<? extends Completion> getCompletions(Element annotated, AnnotationMirror annotation, ExecutableElement attr, String userText) {
    if (processingEnv == null || annotated == null || !annotated.getKind().isClass()) {
        return Collections.emptyList();
    }

    if (   annotation == null
        || !"org.openide.util.lookup.ServiceProvider".contentEquals(((TypeElement) annotation.getAnnotationType().asElement()).getQualifiedName())) {
        return Collections.emptyList();
    }

    if (!"service".contentEquals(attr.getSimpleName())) {
        return Collections.emptyList();
    }

    TypeElement jlObject = processingEnv.getElementUtils().getTypeElement("java.lang.Object");

    if (jlObject == null) {
        return Collections.emptyList();
    }
    
    Collection<Completion> result = new LinkedList<Completion>();
    List<TypeElement> toProcess = new LinkedList<TypeElement>();

    toProcess.add((TypeElement) annotated);

    while (!toProcess.isEmpty()) {
        TypeElement c = toProcess.remove(0);

        result.add(new TypeCompletion(c.getQualifiedName().toString() + ".class"));

        List<TypeMirror> parents = new LinkedList<TypeMirror>();

        parents.add(c.getSuperclass());
        parents.addAll(c.getInterfaces());

        for (TypeMirror tm : parents) {
            if (tm == null || tm.getKind() != TypeKind.DECLARED) {
                continue;
            }

            TypeElement type = (TypeElement) processingEnv.getTypeUtils().asElement(tm);

            if (!jlObject.equals(type)) {
                toProcess.add(type);
            }
        }
    }

    return result;
}
 
Example 15
Project: OpenJSharp   File: TypeModeler.java   View Source Code Vote up 4 votes
public static boolean isSubclass(TypeElement subType, TypeElement superType, ProcessingEnvironment env) {
    return !subType.equals(superType) && isSubElement(subType, superType);
}
 
Example 16
Project: openjdk-jdk10   File: GraphNodeProcessor.java   View Source Code Vote up 4 votes
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (roundEnv.processingOver()) {
        return false;
    }

    GraphNodeVerifier verifier = new GraphNodeVerifier(this);

    for (Element element : roundEnv.getElementsAnnotatedWith(NodeInfo.class)) {
        scope = element;
        try {
            if (!isNodeType(element)) {
                errorMessage(element, "%s can only be applied to Node subclasses", NodeInfo.class.getSimpleName());
                continue;
            }

            NodeInfo nodeInfo = element.getAnnotation(NodeInfo.class);
            if (nodeInfo == null) {
                errorMessage(element, "Cannot get %s annotation from annotated element", NodeInfo.class.getSimpleName());
                continue;
            }

            TypeElement typeElement = (TypeElement) element;

            Set<Modifier> modifiers = typeElement.getModifiers();
            if (!modifiers.contains(Modifier.FINAL) && !modifiers.contains(Modifier.ABSTRACT)) {
                // TODO(thomaswue): Reenable this check.
                // errorMessage(element, "%s annotated class must be either final or abstract",
                // NodeInfo.class.getSimpleName());
                // continue;
            }
            boolean found = false;
            for (Element e : typeElement.getEnclosedElements()) {
                if (e.getKind() == ElementKind.FIELD) {
                    if (e.getSimpleName().toString().equals("TYPE")) {
                        found = true;
                        break;
                    }
                }
            }
            if (!found) {
                errorMessage(element, "%s annotated class must have a field named TYPE", NodeInfo.class.getSimpleName());
            }

            if (!typeElement.equals(verifier.Node) && !modifiers.contains(Modifier.ABSTRACT)) {
                verifier.verify(typeElement);
            }
        } catch (ElementException ee) {
            errorMessage(ee.element, ee.getMessage());
        } catch (Throwable t) {
            reportException(isBug367599(t) ? Kind.NOTE : Kind.ERROR, element, t);
        } finally {
            scope = null;
        }
    }
    return false;
}
 
Example 17
Project: openjdk-jdk10   File: TypeModeler.java   View Source Code Vote up 4 votes
public static boolean isSubclass(TypeElement subType, TypeElement superType, ProcessingEnvironment env) {
    return !subType.equals(superType) && isSubElement(subType, superType);
}