Skip to content

NPE when <annotation> is not provided in XSD #2

@Nogyara

Description

@Nogyara

First, an extract of stacktrace:

...
Caused by: java.lang.NullPointerException
    at info.hubbitus.XJCPluginDescriptionAnnotation.fieldGetDescriptionAnnotation (XJCPluginDescriptionAnnotation.java:199)
    at info.hubbitus.XJCPluginDescriptionAnnotation.access$200 (XJCPluginDescriptionAnnotation.java:80)
    at info.hubbitus.XJCPluginDescriptionAnnotation$2.<init> (XJCPluginDescriptionAnnotation.java:116)
    at info.hubbitus.XJCPluginDescriptionAnnotation.lambda$null$2 (XJCPluginDescriptionAnnotation.java:116)

Now, the problem happens, when in an XSD, there's no element (which is perfectly valid). This problem has actually two instances:

  • info.hubbitus.XJCPluginDescriptionAnnotation#fieldGetDescriptionAnnotation
  • info.hubbitus.XJCPluginDescriptionAnnotation#classInfoGetDescriptionAnnotation

I suggest to rewrite these two methods as follows, which fixes both instances of the problem:

	private static String classInfoGetDescriptionAnnotation(CClassInfo classInfo){
		XSAnnotation annotation = classInfo.getSchemaComponent().getAnnotation();
		return resolveDescription(annotation);
	}

	private static String fieldGetDescriptionAnnotation(CPropertyInfo propertyInfo){
		XSAnnotation annotation = resolveXSAnnotation(propertyInfo.getSchemaComponent());
		return resolveDescription(annotation);
	}

	private static String resolveDescription(XSAnnotation annotation) {
		String description = "";
		if (annotation != null && annotation.getAnnotation() != null) {
			description = ((BindInfo) annotation.getAnnotation()).getDocumentation();
			if (description == null) {
				description = "";
			}
		}
		return description.trim();
	}

	private static XSAnnotation resolveXSAnnotation(XSComponent schemaComponent) {
		XSAnnotation annotation;
		//<xs:complexType name="TDocumentRefer">
		//		<xs:attribute name="documentID" use="required">
		//			<xs:annotation>
		//				<xs:documentation>Идентификатор документа</xs:documentation>
		if (schemaComponent instanceof AttributeUseImpl) {
			annotation = ((AttributeUseImpl) schemaComponent).getDecl().getAnnotation();
		}
		// <xs:complexType name="TBasicInterdepStatement">
		//		<xs:element name="header" type="stCom:TInterdepStatementHeader" minOccurs="0">
		//				<xs:annotation>
		//					<xs:documentation>Заголовок заявления</xs:documentation>
		else if (schemaComponent instanceof ParticleImpl) {
			annotation = (((ParticleImpl) schemaComponent).getTerm()).getAnnotation();
		} else {
			throw new UnsupportedOperationException("Unsupported schema component class: " + schemaComponent.getClass().getName());
		}
		return annotation;
	}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions