private static void verifyTypeOrder(TypeSystemData typeSystem) { Map<String, List<String>> invalidTypes = new HashMap<>(); for (int i = typeSystem.getLegacyTypes().size() - 1; i >= 0; i--) { TypeMirror typeData = typeSystem.getLegacyTypes().get(i); TypeMirror type = typeSystem.boxType(typeData); if (invalidTypes.containsKey(ElementUtils.getQualifiedName(type))) { typeSystem.addError( "Invalid type order. The type(s) %s are inherited from a earlier defined type %s.", invalidTypes.get(ElementUtils.getQualifiedName(type)), ElementUtils.getQualifiedName(type)); } TypeElement element = ElementUtils.fromTypeMirror(type); List<String> nextInvalidTypes = new ArrayList<>(); if (element != null) { nextInvalidTypes.addAll(ElementUtils.getQualifiedSuperTypeNames(element)); } nextInvalidTypes.add(ElementUtils.getQualifiedName(type)); for (String qualifiedName : nextInvalidTypes) { List<String> inheritedTypes = invalidTypes.get(qualifiedName); if (inheritedTypes == null) { inheritedTypes = new ArrayList<>(); invalidTypes.put(qualifiedName, inheritedTypes); } inheritedTypes.add(ElementUtils.getQualifiedName(typeSystem.boxType(typeData))); } } }
private void verifyTypes(TypeSystemData typeSystem) { for (TypeMirror type : typeSystem.getLegacyTypes()) { if (isPrimitiveWrapper(type)) { typeSystem.addError("Types must not contain primitive wrapper types."); } if (ElementUtils.typeEquals(type, context.getType(Object.class))) { typeSystem.addError("Types must not contain the generic type java.lang.Object."); } } verifyTypeOrder(typeSystem); }
private static void verifyNamesUnique(TypeSystemData typeSystem) { Set<String> usedNames = new HashSet<>(); for (TypeMirror type : typeSystem.getLegacyTypes()) { String boxedName = ElementUtils.getSimpleName(typeSystem.boxType(type)); String primitiveName = ElementUtils.getSimpleName(type); if (usedNames.contains(boxedName)) { typeSystem.addError("Two types result in the same boxed name: %s.", boxedName); } else if (usedNames.contains(primitiveName)) { typeSystem.addError("Two types result in the same primitive name: %s.", primitiveName); } usedNames.add(boxedName); usedNames.add(primitiveName); } }
@Override protected TypeSystemData parse(Element element, AnnotationMirror mirror) { TypeElement templateType = (TypeElement) element; AnnotationMirror templateTypeAnnotation = mirror; DSLOptions options = element.getAnnotation(DSLOptions.class); if (options == null) { options = TypeSystemParser.class.getAnnotation(DSLOptions.class); } assert options != null; TypeSystemData typeSystem = new TypeSystemData(context, templateType, templateTypeAnnotation, options, false); // annotation type on class path!? TypeElement annotationTypeElement = processingEnv.getElementUtils().getTypeElement(getAnnotationType().getCanonicalName()); if (annotationTypeElement == null) { typeSystem.addError( "Required class %s is not on the classpath.", getAnnotationType().getName()); } if (templateType.getModifiers().contains(Modifier.PRIVATE)) { typeSystem.addError( "A @%s must have at least package protected visibility.", getAnnotationType().getName()); } if (templateType.getModifiers().contains(Modifier.FINAL)) { typeSystem.addError("The @%s must not be final.", getAnnotationType().getName()); } if (typeSystem.hasErrors()) { return typeSystem; } if (typeSystem.hasErrors()) { return typeSystem; } verifyExclusiveMethodAnnotation(typeSystem, TypeCast.class, TypeCheck.class); List<Element> elements = newElementList(context.getEnvironment().getElementUtils().getAllMembers(templateType)); List<ImplicitCastData> implicitCasts = new ImplicitCastParser(context, typeSystem).parse(elements); List<TypeCastData> casts = new TypeCastParser(context, typeSystem).parse(elements); List<TypeCheckData> checks = new TypeCheckParser(context, typeSystem).parse(elements); if (casts == null || checks == null || implicitCasts == null) { return typeSystem; } List<TypeMirror> legacyTypes = ElementUtils.getAnnotationValueList( TypeMirror.class, typeSystem.getTemplateTypeAnnotation(), "value"); for (int i = 0; i < legacyTypes.size(); i++) { legacyTypes.set(i, ElementUtils.fillInGenericWildcards(legacyTypes.get(i))); } typeSystem.getLegacyTypes().addAll(legacyTypes); verifyTypes(typeSystem); typeSystem.getLegacyTypes().add(context.getType(Object.class)); typeSystem.getLegacyTypes().add(context.getType(void.class)); verifyNamesUnique(typeSystem); typeSystem.getImplicitCasts().addAll(implicitCasts); typeSystem.getCasts().addAll(casts); typeSystem.getChecks().addAll(checks); if (typeSystem.hasErrors()) { return typeSystem; } return typeSystem; }