Java Code Examples for javax.lang.model.util.Types#isSameType()
The following examples show how to use
javax.lang.model.util.Types#isSameType() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebServiceVisitor.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
protected boolean sameMethod(ExecutableElement method1, ExecutableElement method2) { if (!method1.getSimpleName().equals(method2.getSimpleName())) return false; Types typeUtils = builder.getProcessingEnvironment().getTypeUtils(); if(!typeUtils.isSameType(method1.getReturnType(), method2.getReturnType()) && !typeUtils.isSubtype(method2.getReturnType(), method1.getReturnType())) return false; List<? extends VariableElement> parameters1 = method1.getParameters(); List<? extends VariableElement> parameters2 = method2.getParameters(); if (parameters1.size() != parameters2.size()) return false; for (int i = 0; i < parameters1.size(); i++) { if (!typeUtils.isSameType(parameters1.get(i).asType(), parameters2.get(i).asType())) return false; } return true; }
Example 2
Source File: MoreElementsTest.java From auto with Apache License 2.0 | 6 votes |
private ExecutableElement getMethod(Class<?> c, String methodName, TypeMirror... parameterTypes) { TypeElement type = compilation.getElements().getTypeElement(c.getCanonicalName()); Types types = compilation.getTypes(); ExecutableElement found = null; for (ExecutableElement method : ElementFilter.methodsIn(type.getEnclosedElements())) { if (method.getSimpleName().contentEquals(methodName) && method.getParameters().size() == parameterTypes.length) { boolean match = true; for (int i = 0; i < parameterTypes.length; i++) { TypeMirror expectedType = parameterTypes[i]; TypeMirror actualType = method.getParameters().get(i).asType(); match &= types.isSameType(expectedType, actualType); } if (match) { assertThat(found).isNull(); found = method; } } } assertWithMessage(methodName + Arrays.toString(parameterTypes)).that(found).isNotNull(); return found; }
Example 3
Source File: Environment.java From sqlitemagic with Apache License 2.0 | 6 votes |
public boolean isValidDataClass(@NonNull List<? extends Element> enclosedElements, @NonNull List<? extends BaseColumnElement> allColumns) { for (Element enclosedElement : enclosedElements) { if (enclosedElement.getKind() == ElementKind.CONSTRUCTOR) { ExecutableElement constructor = (ExecutableElement) enclosedElement; final List<? extends VariableElement> constructorParams = constructor.getParameters(); if (constructorParams.size() != allColumns.size()) { return false; } final Types typeUtils = getTypeUtils(); final Iterator<? extends BaseColumnElement> columnsIterator = allColumns.iterator(); for (VariableElement param : constructorParams) { final BaseColumnElement column = columnsIterator.next(); if (!typeUtils.isSameType(param.asType(), column.getDeserializedType().getTypeMirror())) { return false; } } return true; } } return false; }
Example 4
Source File: TypeProductionFilter.java From netbeans with Apache License 2.0 | 6 votes |
private boolean hasBeanType( TypeMirror arrayComponentType, Element productionElement ) { Collection<TypeMirror> restrictedTypes = RestrictedTypedFilter. getRestrictedTypes(productionElement, getImplementation()); if ( restrictedTypes == null ){ TypeMirror productionType= null; if ( productionElement.getKind() == ElementKind.FIELD){ productionType = productionElement.asType(); } else if ( productionElement.getKind() == ElementKind.METHOD){ productionType = ((ExecutableElement)productionElement). getReturnType(); } return checkArrayBeanType(productionType, arrayComponentType); } Types types = getImplementation().getHelper(). getCompilationController().getTypes(); for( TypeMirror restrictedType : restrictedTypes ){ if ( types.isSameType( restrictedType, getElementType())){ return true; } } return false; }
Example 5
Source File: ConfigurationAnnotations.java From dagger2-sample with Apache License 2.0 | 6 votes |
/** Traverses includes from superclasses and adds them into the builder. */ private static void addIncludesFromSuperclasses(Types types, TypeElement element, ImmutableSet.Builder<TypeElement> builder, TypeMirror objectType) { // Also add the superclass to the queue, in case any @Module definitions were on that. TypeMirror superclass = element.getSuperclass(); while (!types.isSameType(objectType, superclass) && superclass.getKind().equals(TypeKind.DECLARED)) { element = MoreElements.asType(types.asElement(superclass)); Optional<AnnotationMirror> moduleMirror = getAnnotationMirror(element, Module.class) .or(getAnnotationMirror(element, ProducerModule.class)); if (moduleMirror.isPresent()) { builder.addAll(MoreTypes.asTypeElements(getModuleIncludes(moduleMirror.get()))); } superclass = element.getSuperclass(); } }
Example 6
Source File: UseSuperTypeRefactoringPlugin.java From netbeans with Apache License 2.0 | 6 votes |
private void replaceWithSuperType(TreePath path, VariableTree oldVarTree, VariableElement varElement, Element superTypeElement) { Types types = workingCopy.getTypes(); TypeMirror supTypeErasure = types.erasure(superTypeElement.asType()); DeclaredType varType = (DeclaredType) varElement.asType(); TypeMirror theType = null; List<TypeMirror> supertypes = new LinkedList(types.directSupertypes(varType)); while(!supertypes.isEmpty()) { TypeMirror supertype = supertypes.remove(0); if(types.isSameType(types.erasure(supertype), supTypeErasure)) { theType = supertype; break; } supertypes.addAll(types.directSupertypes(supertype)); } if(theType == null) { theType = supTypeErasure; } Tree superTypeTree = make.Type(theType); ExpressionTree oldInitTree = oldVarTree.getInitializer(); ModifiersTree oldModifiers = oldVarTree.getModifiers(); Tree newTree = make.Variable(oldModifiers, oldVarTree.getName(), superTypeTree, oldInitTree); rewrite(oldVarTree, newTree); }
Example 7
Source File: UseSuperTypeRefactoringPlugin.java From netbeans with Apache License 2.0 | 6 votes |
@Override public Tree visitTypeCast(TypeCastTree castTree, Element elementToMatch) { TreePath path = getCurrentPath(); Types types = workingCopy.getTypes(); TypeMirror castTypeErasure = types.erasure(workingCopy.getTrees().getTypeMirror(path)); TypeMirror elToMatchErasure = types.erasure(subTypeElement.asType()); path = path.getParentPath(); Element element = workingCopy.getTrees().getElement(path); if (element instanceof VariableElement && types.isSameType(castTypeErasure, elToMatchErasure)) { VariableElement varElement = (VariableElement)element; TypeMirror varTypeErasure = types.erasure(varElement.asType()); if (types.isSameType(varTypeErasure, elToMatchErasure) && isReplaceCandidate(varElement)) { TypeCastTree newTree = make.TypeCast( make.Identifier(superTypeElement), castTree.getExpression()); rewrite(castTree, newTree); } } return super.visitTypeCast(castTree, elementToMatch); }
Example 8
Source File: ProcessorUtils.java From pandroid with Apache License 2.0 | 6 votes |
public static boolean sameMethods(Types typeUtils, ExecutableElement method1, ExecutableElement method2) { if (!method1.getSimpleName().equals(method2.getSimpleName())) return false; List<? extends VariableElement> parameters1 = method1 .getParameters(); List<? extends VariableElement> parameters2 = method2 .getParameters(); if (parameters1.size() != parameters2.size()) return false; for (int i = 0; i < parameters1.size(); i++) { if (!typeUtils.isSameType(parameters1.get(i).asType(), parameters2.get(i).asType())) return false; } return true; }
Example 9
Source File: JavaCodeTemplateProcessor.java From netbeans with Apache License 2.0 | 5 votes |
private boolean isSameType(TypeMirror t1, TypeMirror t2, Types types) { if (types.isSameType(t1, t2)) { return true; } if (t1.getKind().isPrimitive() && types.isSameType(types.boxedClass((PrimitiveType)t1).asType(), t2)) { return true; } return t2.getKind().isPrimitive() && types.isSameType(t1, types.boxedClass((PrimitiveType)t1).asType()); }
Example 10
Source File: AnnotationMirrors.java From immutables with Apache License 2.0 | 5 votes |
public static ImmutableList<TypeMirror> getTypesFromMirrors( Types types, TypeMirror annotationType, String annotationAttributeName, List<? extends AnnotationMirror> annotationMirrors) { ImmutableList.Builder<TypeMirror> builder = ImmutableList.builder(); for (AnnotationMirror annotationMirror : annotationMirrors) { if (types.isSameType(annotationMirror.getAnnotationType(), annotationType)) { collectTypesFromAnnotationAttribute(annotationAttributeName, builder, annotationMirror); } } return builder.build(); }
Example 11
Source File: AttributeFieldValidation.java From qpid-broker-j with Apache License 2.0 | 5 votes |
@Override public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) { Elements elementUtils = processingEnv.getElementUtils(); Types typeUtils = processingEnv.getTypeUtils(); TypeElement annotationElement = elementUtils.getTypeElement(MANAGED_ATTRIBUTE_FIELD_CLASS_NAME); for (Element e : roundEnv.getElementsAnnotatedWith(annotationElement)) { for(AnnotationMirror am : e.getAnnotationMirrors()) { if(typeUtils.isSameType(am.getAnnotationType(), annotationElement.asType())) { for(Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : am.getElementValues().entrySet()) { String elementName = entry.getKey().getSimpleName().toString(); if(elementName.equals("beforeSet") || elementName.equals("afterSet")) { String methodName = entry.getValue().getValue().toString(); if(!"".equals(methodName)) { TypeElement parent = (TypeElement) e.getEnclosingElement(); if(!containsMethod(parent, methodName)) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Could not find method '" + methodName + "' which is defined as the " + elementName + " action", e); } } } } } } } return false; }
Example 12
Source File: RefactoringUtils.java From netbeans with Apache License 2.0 | 5 votes |
private static int findTypeIndex(Types utils, List<TypeMirror> typeArgs, TypeMirror type) { int i = -1; for (TypeMirror typeArg : typeArgs) { i++; if (utils.isSameType(type, typeArg)) { return i; } } return -1; }
Example 13
Source File: RestrictedTypedFilter.java From netbeans with Apache License 2.0 | 5 votes |
@Override void filter( Set<TypeElement> elements ){ Set<TypeElement> allImplementors = new HashSet<TypeElement>( elements ); for (Iterator<TypeElement> iterator = allImplementors.iterator() ; iterator.hasNext() ; ) { TypeElement typeElement = iterator.next(); Collection<TypeMirror> restrictedTypes = getRestrictedTypes(typeElement, getImplementation()); if ( restrictedTypes == null ){ continue; } boolean hasBeanType = false; TypeElement element = getElement(); TypeMirror type = element.asType(); Types types= getImplementation().getHelper().getCompilationController(). getTypes(); for (TypeMirror restrictedType : restrictedTypes) { if ( types.isSameType( types.erasure( type), types.erasure( restrictedType))) { hasBeanType = true; break; } } if ( !hasBeanType ){ iterator.remove(); } } }
Example 14
Source File: ValidVersionType.java From netbeans with Apache License 2.0 | 5 votes |
public Collection<ErrorDescription> check(JPAProblemContext ctx, HintContext hc, AttributeWrapper attrib) { if (attrib.getModelElement() instanceof Version) { TreeUtilities treeUtils = ctx.getCompilationInfo().getTreeUtilities(); Types types = ctx.getCompilationInfo().getTypes(); TypeMirror attrType = attrib.getType(); for (String typeName : validVersionTypes) { TypeMirror type = treeUtils.parseType(typeName, ctx.getJavaClass()); if (type != null && types.isSameType(attrType, type)) { return null; } } Tree elementTree = ctx.getCompilationInfo().getTrees().getTree(attrib.getJavaElement()); Utilities.TextSpan underlineSpan = Utilities.getUnderlineSpan( ctx.getCompilationInfo(), elementTree); ErrorDescription error = ErrorDescriptionFactory.forSpan( hc, underlineSpan.getStartOffset(), underlineSpan.getEndOffset(), NbBundle.getMessage(ValidVersionType.class, "MSG_InvalidVersionType"));//TODO: may need to have "error" fo some/ warning for another return Collections.singleton(error); } return null; }
Example 15
Source File: AnnotationHelper.java From netbeans with Apache License 2.0 | 5 votes |
public boolean isSameRawType(TypeMirror type1, String type2ElementName) { TypeElement type2Element = getCompilationInfo().getElements().getTypeElement(type2ElementName); if (type2Element != null) { Types types = getCompilationInfo().getTypes(); TypeMirror type2 = types.erasure(type2Element.asType()); return types.isSameType(types.erasure(type1), type2); } return false; }
Example 16
Source File: DelegateAssignabilityChecker.java From netbeans with Apache License 2.0 | 5 votes |
@Override protected boolean handleBeanRawType( Types types, List<? extends TypeMirror> typeArguments, TypeElement objectElement ) { // bean type is a raw. for (TypeMirror typeParam : typeArguments) { /* * From the spec: * A raw bean type is considered assignable to a parameterized * delegate type if the raw types are identical and all type parameters * of the delegate type are either unbounded type variables or java.lang.Object. */ if (typeParam.getKind() == TypeKind.DECLARED) { if (!((TypeElement)((DeclaredType) typeParam).asElement()). getQualifiedName().contentEquals(Object.class.getCanonicalName())) { return false; } } else if ( typeParam.getKind() == TypeKind.TYPEVAR){ TypeMirror lowerBound = ((TypeVariable)typeParam).getLowerBound(); if ( lowerBound != null && lowerBound.getKind() != TypeKind.NULL ){ return false; } TypeMirror upperBound = ((TypeVariable)typeParam).getUpperBound(); if ( upperBound != null && upperBound.getKind() != TypeKind.NULL && objectElement!= null ) { return types.isSameType(upperBound, objectElement.asType()); } } else { return false; } } return true; }
Example 17
Source File: ContentHeaderAnnotationValidator.java From qpid-broker-j with Apache License 2.0 | 5 votes |
private boolean isValidType(TypeMirror type) { Types typeUtils = processingEnv.getTypeUtils(); Elements elementUtils = processingEnv.getElementUtils(); Element typeElement = typeUtils.asElement(type); if (VALID_PRIMITIVE_TYPES.contains(type.getKind())) { return true; } for (TypeKind primitive : VALID_PRIMITIVE_TYPES) { if (typeUtils.isSameType(type, typeUtils.boxedClass(typeUtils.getPrimitiveType(primitive)).asType())) { return true; } } if (typeElement != null && typeElement.getKind() == ElementKind.ENUM) { return true; } if (typeUtils.isSameType(type, elementUtils.getTypeElement("java.lang.String").asType())) { return true; } return false; }
Example 18
Source File: JavaHintsAnnotationProcessor.java From netbeans with Apache License 2.0 | 4 votes |
private boolean verifyOptionField(VariableElement field) { StringBuilder error = new StringBuilder(); Elements elements = processingEnv.getElementUtils(); TypeElement jlString = elements.getTypeElement("java.lang.String"); if (jlString == null) { return true; } Types types = processingEnv.getTypeUtils(); TypeMirror jlStringType = jlString.asType(); //no type params, no need to erasure if (!types.isSameType(field.asType(), jlStringType)) { error.append(ERR_RETURN_TYPE); error.append("\n"); } if (!field.getModifiers().contains(Modifier.STATIC) || !field.getModifiers().contains(Modifier.FINAL)) { error.append(ERR_OPTION_MUST_BE_STATIC_FINAL); error.append("\n"); } Object key = field.getConstantValue(); if (key == null) { error.append("Option field not a compile-time constant"); error.append("\n"); } if (error.length() == 0) { return true; } if (error.charAt(error.length() - 1) == '\n') { error.delete(error.length() - 1, error.length()); } processingEnv.getMessager().printMessage(Kind.ERROR, error.toString(), field); return false; }
Example 19
Source File: ValidBasicType.java From netbeans with Apache License 2.0 | 4 votes |
public Collection<ErrorDescription> check(JPAProblemContext ctx, HintContext hc, AttributeWrapper attrib) { if (!(attrib.getModelElement() instanceof Basic)) { return null; } TreeUtilities treeUtils = ctx.getCompilationInfo().getTreeUtilities(); Types types = ctx.getCompilationInfo().getTypes(); TypeMirror attrType = attrib.getType(); TypeMirror typeSerializable = treeUtils.parseType("java.io.Serializable", //NOI18N ctx.getJavaClass()); TypeMirror typeEnum = treeUtils.parseType("java.lang.Enum", //NOI18N ctx.getJavaClass()); TypeMirror typeCollection = treeUtils.parseType("java.util.Collection", //NOI18N ctx.getJavaClass()); if (types.isAssignable(attrType, typeSerializable) || types.isAssignable(attrType, typeEnum) || types.isAssignable(attrType, typeCollection)) { return null; } for (String typeName : fixedBasicTypes) { TypeMirror type = treeUtils.parseType(typeName, ctx.getJavaClass()); if (type != null && types.isSameType(attrType, type)) { return null; } } if (Utilities.hasAnnotation(attrib.getJavaElement(), JPAAnnotations.ELEMENT_COLLECTION)) { //according to annotation it's not basic type and need to be verified in appropriate validator return null; } if (Utilities.hasAnnotation(attrib.getJavaElement(), JPAAnnotations.EMBEDDED)) { //@Embedded, see also #167419 return null; } Tree elementTree = ctx.getCompilationInfo().getTrees().getTree(attrib.getJavaElement()); Utilities.TextSpan underlineSpan = Utilities.getUnderlineSpan( ctx.getCompilationInfo(), elementTree); ErrorDescription error = ErrorDescriptionFactory.forSpan( hc, underlineSpan.getStartOffset(), underlineSpan.getEndOffset(), NbBundle.getMessage(ValidBasicType.class, "MSG_ValidBasicType"));//TODO: may need to have "error" as default return Collections.singleton(error); }
Example 20
Source File: JpaControllerUtil.java From netbeans with Apache License 2.0 | 4 votes |
public static ExecutableElement getOtherSideOfRelation(CompilationController controller, ExecutableElement executableElement, boolean isFieldAccess) { TypeMirror passedReturnType = executableElement.getReturnType(); if (TypeKind.DECLARED != passedReturnType.getKind() || !(passedReturnType instanceof DeclaredType)) { return null; } Types types = controller.getTypes(); TypeMirror passedReturnTypeStripped = stripCollection((DeclaredType)passedReturnType, types); if (passedReturnTypeStripped == null) { return null; } TypeElement passedReturnTypeStrippedElement = (TypeElement) types.asElement(passedReturnTypeStripped); //try to find a mappedBy annotation element on the possiblyAnnotatedElement Element possiblyAnnotatedElement = isFieldAccess ? JpaControllerUtil.guessField(executableElement) : executableElement; String mappedBy = null; AnnotationMirror persistenceAnnotation = JpaControllerUtil.findAnnotation(possiblyAnnotatedElement, "javax.persistence.OneToOne"); //NOI18N" if (persistenceAnnotation == null) { persistenceAnnotation = JpaControllerUtil.findAnnotation(possiblyAnnotatedElement, "javax.persistence.OneToMany"); //NOI18N" } if (persistenceAnnotation == null) { persistenceAnnotation = JpaControllerUtil.findAnnotation(possiblyAnnotatedElement, "javax.persistence.ManyToOne"); //NOI18N" } if (persistenceAnnotation == null) { persistenceAnnotation = JpaControllerUtil.findAnnotation(possiblyAnnotatedElement, "javax.persistence.ManyToMany"); //NOI18N" } if (persistenceAnnotation != null) { mappedBy = JpaControllerUtil.findAnnotationValueAsString(persistenceAnnotation, "mappedBy"); //NOI18N } for (ExecutableElement method : JpaControllerUtil.getEntityMethods(passedReturnTypeStrippedElement)) { if (mappedBy != null && mappedBy.length() > 0) { String tail = mappedBy.length() > 1 ? mappedBy.substring(1) : ""; String getterName = "get" + mappedBy.substring(0,1).toUpperCase() + tail; if (getterName.equals(method.getSimpleName().toString())) { return method; } } else { TypeMirror iteratedReturnType = method.getReturnType(); iteratedReturnType = stripCollection(iteratedReturnType, types); TypeMirror executableElementEnclosingType = executableElement.getEnclosingElement().asType(); if (types.isSameType(executableElementEnclosingType, iteratedReturnType)) { return method; } } } return null; }