org.raml.v2.api.model.v10.datamodel.TypeDeclaration Java Examples

The following examples show how to use org.raml.v2.api.model.v10.datamodel.TypeDeclaration. 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: UnionTypeInterpreter.java    From springmvc-raml-plugin with Apache License 2.0 6 votes vote down vote up
private TypeDeclaration getParent(UnionTypeDeclaration objectType, String typeName, RamlRoot document) {
	Map<String, RamlDataType> types = document.getTypes();

	TypeDeclaration parent = null;

	if (!RamlTypeHelper.isBaseObject(typeName)) {
		parent = types.get(typeName).getType();
	} else if (objectType.parentTypes() != null && objectType.parentTypes().size() > 0) {
		TypeDeclaration tempParent = objectType.parentTypes().get(0); // java
																		// doesnt
																		// support
																		// multiple
																		// parents
																		// take
																		// first;
		if (!RamlTypeHelper.isBaseObject(tempParent.name())) {
			parent = types.get(tempParent.name()).getType();
		}
	} else {
		parent = null;
	}
	return parent;
}
 
Example #2
Source File: CovariantListPlugin.java    From raml-java-tools with Apache License 2.0 6 votes vote down vote up
@Override
public MethodSpec.Builder setterBuilt(ObjectPluginContext objectPluginContext, TypeDeclaration declaration, MethodSpec.Builder incoming, EventType eventType) {

    if ( eventType == EventType.INTERFACE) {

        if ( arguments.size() == 1 ) {
            return null;
        }

        if (isNotTargetDefaultType(objectPluginContext, declaration, arguments)) {
            return incoming;
        } else {

            return null;
        }
    } else {

        return incoming;
    }
}
 
Example #3
Source File: Jsr303Extension.java    From raml-java-tools with Apache License 2.0 6 votes vote down vote up
@Override
public FieldSpec.Builder fieldBuilt(UnionPluginContext unionPluginContext, TypeDeclaration ramlType, FieldSpec.Builder fieldSpec, EventType eventType) {
    AnnotationAdder adder = new AnnotationAdder() {

        @Override
        public TypeName typeName() {
            return fieldSpec.build().type;
        }

        @Override
        public void addAnnotation(AnnotationSpec spec) {
            // ignore not null (we are in a union, of course they can be null)
            if (!Objects.equals(spec.type, TypeName.get(NotNull.class))) {
                fieldSpec.addAnnotation(spec);
            }
        }
    };

    addAnnotations(ramlType, adder);
    return fieldSpec;
}
 
Example #4
Source File: RamlInterpreterFactory.java    From springmvc-raml-plugin with Apache License 2.0 6 votes vote down vote up
public static RamlTypeInterpreter getInterpreterForType(TypeDeclaration type) {
	RamlTypeInterpreter interpreter = interpreterCache.get(identifyByClass(type));
	if (interpreter != null) {
		return interpreter;
	}

	for (Class<? extends TypeDeclaration> key : interpreters.keySet()) {
		if (key.isAssignableFrom(type.getClass())) {
			interpreter = interpreters.get(key);
			interpreterCache.put(type.getClass(), interpreter);
			break;
		}
	}
	if (interpreter == null) {
		logger.error("Missing Interpreter for type " + identifyByClass(type) + ":" + type.type());
		interpreter = DEFAULT_INTERPRETER;
	}
	return interpreter;
}
 
Example #5
Source File: AnyTypeInterpreter.java    From springmvc-raml-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public RamlInterpretationResult interpret(RamlRoot document, TypeDeclaration type, JCodeModel builderModel, boolean property) {

	AnyTypeDeclaration anyTypeDeclaration = (AnyTypeDeclaration) type;

	RamlInterpretationResult result = new RamlInterpretationResult(type.required());
	String objectName;
	if ("array".equalsIgnoreCase(anyTypeDeclaration.type())) {
		objectName = Object.class.getSimpleName();
	} else {
		objectName = Void.class.getSimpleName();
	}

	result.setResolvedClass(CodeModelHelper.findFirstClassBySimpleName(builderModel, objectName));
	return result;
}
 
Example #6
Source File: JaxbUnionExtension.java    From raml-java-tools with Apache License 2.0 6 votes vote down vote up
@Override
public FieldSpec.Builder anyFieldCreated(UnionPluginContext context, UnionTypeDeclaration union, TypeSpec.Builder typeSpec, FieldSpec.Builder anyType, EventType eventType) {

    AnnotationSpec.Builder elementsAnnotation = AnnotationSpec.builder(XmlElements.class);
    for (TypeDeclaration typeDeclaration : union.of()) {

        TypeName unionPossibility = context.unionClass(typeDeclaration).getJavaName(EventType.IMPLEMENTATION);

        elementsAnnotation.addMember("value",
                "$L",
                AnnotationSpec
                        .builder(XmlElement.class)
                        .addMember("name", "$S", typeDeclaration.name())
                        .addMember("type",
                                "$T.class", unionPossibility
                                )
                        .build());
    }

    anyType.addAnnotation(elementsAnnotation.build());

    return anyType;
}
 
Example #7
Source File: TypeDeclarationTypeTest.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void internalIntIsNotNewInlineType() {

    Api api = RamlLoader.load(this.getClass().getResourceAsStream("inline-types.raml"), ".");
    ObjectTypeDeclaration decl = RamlLoader.findTypes("foo", api.types());
    TypeDeclaration property = findProperty(decl, "internalInt");

    assertFalse(TypeDeclarationType.isNewInlineType(property));
}
 
Example #8
Source File: ApiBodyMetadata.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
public ApiBodyMetadata(String name, TypeDeclaration type, boolean array, JCodeModel codeModel) {
	super();
	this.schema = null;
	this.type = type;
	this.name = name;
	this.codeModel = codeModel;
	this.array = array;
	// array detection. i think we can default this to false since we should
	// already be generating lists from the type. nope we need it within
	// rules for narrowing to List
}
 
Example #9
Source File: RJP10V2RamlModelFactory.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public RamlHeader createRamlHeader(Object haeder) {
	if (haeder == null) {
		return null;
	}
	return new RJP10V2RamlHeader((TypeDeclaration) haeder);
}
 
Example #10
Source File: EnumerationTypeHandlerPlugin.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public TypeSpec.Builder enumValue(EnumerationPluginContext enumerationPluginContext, TypeDeclaration declaration, TypeSpec.Builder incoming, String value, EventType eventType) {
    for (EnumerationTypeHandlerPlugin plugin : plugins) {
        if ( incoming == null ) {
            break;
        }
        incoming = plugin.enumValue(enumerationPluginContext, declaration, incoming, value, eventType);
    }

    return incoming;
}
 
Example #11
Source File: TypeDeclarationTypeTest.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void extendedObjectIsNotNewInlineType() {

    Api api = RamlLoader.load(this.getClass().getResourceAsStream("inline-types.raml"), ".");
    ObjectTypeDeclaration decl = RamlLoader.findTypes("foo", api.types());
    TypeDeclaration property = findProperty(decl, "extendedFromOne");

    assertFalse(TypeDeclarationType.isNewInlineType(property));
}
 
Example #12
Source File: GsonObjectExtension.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public FieldSpec.Builder fieldBuilt(ObjectPluginContext objectPluginContext, TypeDeclaration declaration, FieldSpec.Builder incoming, EventType eventType) {
    return incoming
            .addAnnotation(
                    AnnotationSpec.builder(SerializedName.class)
                            .addMember("value", "$S", objectPluginContext.creationResult().getJavaName(EventType.IMPLEMENTATION)).build())
            .addAnnotation(AnnotationSpec.builder(Expose.class).build());
}
 
Example #13
Source File: UnionTypesHelper.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
public static boolean isAmbiguous(List<TypeDeclaration> typeDeclarations, Function<TypeDeclaration, TypeName> converter) {
    Set<TypeName> types = new HashSet<>();
    for (TypeDeclaration typeDeclaration : typeDeclarations) {
        if (!types.add(converter.apply(typeDeclaration))) {
            return true;
        }
    }
    return false;
}
 
Example #14
Source File: GenerationContextImpl.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public EnumerationTypeHandlerPlugin pluginsForEnumerations(TypeDeclaration... typeDeclarations) {
    List<PluginDef> data = Annotations.PLUGINS.get(Collections.<PluginDef>emptyList(), api, typeDeclarations);
    Set<EnumerationTypeHandlerPlugin> plugins = new HashSet<>();
    loadBasePlugins(plugins, EnumerationTypeHandlerPlugin.class);

    for (PluginDef datum : data) {
        plugins.addAll(pluginManager.getClassesForName(datum.getPluginName(), datum.getArguments() , EnumerationTypeHandlerPlugin.class));
    }
    return new EnumerationTypeHandlerPlugin.Composite(plugins);
}
 
Example #15
Source File: TypeFinders.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
public static TypeFinder inResources() {

        return new TypeFinder() {
            @Override
            public Iterable<TypeDeclaration> findTypes(Api api) {
                return resourceTypes(api.resources());
            }
        };
    }
 
Example #16
Source File: RamlTypeHelper.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Safely get description from a type with null checks
 * 
 * @param type
 *            The RAML TypeDeclaration to check
 * @return The description if defined or null if empty
 */
public static String getDescription(TypeDeclaration type) {
	if (type == null || type.description() == null) {
		return null;
	} else {
		return type.description().value();
	}
}
 
Example #17
Source File: TypeDeclarationTypeTest.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void multiInheritanceWithExtraPropertiesIsNewInlineType() {

    Api api = RamlLoader.load(this.getClass().getResourceAsStream("inline-types.raml"), ".");
    ObjectTypeDeclaration decl = RamlLoader.findTypes("foo", api.types());
    TypeDeclaration property = findProperty(decl, "multiInheritanceWithExtraProperty");

    assertTrue(TypeDeclarationType.isNewInlineType(property));
}
 
Example #18
Source File: ReferenceTypeHandlerPlugin.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public TypeName typeName(ReferencePluginContext referencePluginContext, TypeDeclaration ramlType, TypeName currentSuggestion) {
    for (ReferenceTypeHandlerPlugin plugin : plugins) {
        currentSuggestion = plugin.typeName(referencePluginContext, ramlType, currentSuggestion);
    }

    return currentSuggestion;
}
 
Example #19
Source File: EnumerationTypeHandlerPlugin.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public TypeSpec.Builder enumValue(EnumerationPluginContext enumerationPluginContext, TypeDeclaration declaration, TypeSpec.Builder incoming, Number value, EventType eventType) {
    for (EnumerationTypeHandlerPlugin plugin : plugins) {
        if ( incoming == null ) {
            break;
        }
        incoming = plugin.enumValue(enumerationPluginContext, declaration, incoming, value, eventType);
    }

    return incoming;
}
 
Example #20
Source File: TypeFetchers.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
public static TypeFetcher fromLibraries() {

        return new TypeFetcher() {

            Iterable<TypeDeclaration> foundInApi;

            @Override
            public TypeDeclaration fetchType(Api api, final String name) throws GenerationException {
                return FluentIterable.from(Optional.fromNullable(foundInApi).or(Utils.goThroughLibraries(new ArrayList<TypeDeclaration>(), new HashSet<String>(), api.uses())))
                        .firstMatch(namedPredicate(name)).or(fail(name));
            }
        };
    }
 
Example #21
Source File: FileTypeInterpreter.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public Set<Class<? extends TypeDeclaration>> getSupportedTypes() {
	if (set == null) {
		set = new LinkedHashSet<>(1);
		set.add(FileTypeDeclaration.class);
	}
	return set;
}
 
Example #22
Source File: Utils.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
static public  List<TypeDeclaration> allParents(TypeDeclaration target, List<TypeDeclaration> found) {

        found.add(target);
        for (TypeDeclaration typeDeclaration : target.parentTypes()) {
            allParents(typeDeclaration, found);
        }

        return found;
    }
 
Example #23
Source File: UnionTypeHandlerTest.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
private static UnionTypeDeclaration findTypes(final String name, List<TypeDeclaration> types) {
    return (UnionTypeDeclaration) FluentIterable.from(types).firstMatch(new Predicate<TypeDeclaration>() {

        @Override
        public boolean apply(@Nullable TypeDeclaration input) {
            return input.name().equals(name);
        }

    }).get();
}
 
Example #24
Source File: ActionImpl.java    From raml-java-client-generator with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, MimeType> getBody()
{
    Map<String, MimeType> result = new LinkedHashMap<>();
    for (TypeDeclaration typeDeclaration : method.body())
    {
        result.put(typeDeclaration.name(),  new MimeTypeImpl(typeDeclaration));
    }
    return result;
}
 
Example #25
Source File: JacksonBasicExtension.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public FieldSpec.Builder fieldBuilt(ObjectPluginContext objectPluginContext, TypeDeclaration declaration, FieldSpec.Builder fieldSpec, EventType eventType) {
    AnnotationSpec.Builder annotation = AnnotationSpec.builder(JsonProperty.class)
            .addMember("value", "$S", declaration.name());
    if (declaration.defaultValue() != null) {
        annotation.addMember("defaultValue", "$S", declaration.defaultValue());

    }
    return fieldSpec.addAnnotation(
            annotation.build());
}
 
Example #26
Source File: CovariantListPlugin.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
private  boolean isNotTargetDefaultType(ObjectPluginContext objectPluginContext, TypeDeclaration declaration, List<String> arguments) {
    if (!(declaration instanceof ArrayTypeDeclaration)) {

        return true;
    }

    ArrayTypeDeclaration arrayTypeDeclaration = (ArrayTypeDeclaration) declaration;
    TypeDeclaration itemTypes = arrayTypeDeclaration.items();
    if (!(itemTypes instanceof ObjectTypeDeclaration)) {

        return true;
    }
    return arguments.size() == 0 && objectPluginContext.childClasses(itemTypes.name()).isEmpty();
}
 
Example #27
Source File: AnnotationsTest.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void abstractAnnotationsReading() throws IOException {

    Api api = getApi();
    TypeDeclaration fooType = RamlLoader.findTypes("foo", api.types());

    boolean b  = Annotations.ABSTRACT.get(fooType);
    assertEquals(true, b);
}
 
Example #28
Source File: UnionTypeInterpreter.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
private String getClassName(TypeDeclaration type) {
	UnionTypeDeclaration objectType = (UnionTypeDeclaration) type;
	String name = StringUtils.capitalize(objectType.name());

	// For mime types we need to take the type not the name
	try {
		MimeType.valueOf(name);
		name = objectType.type();
	} catch (Exception ex) {
		// not a valid mimetype do nothing
		logger.debug("mime: " + name);
	}
	return name;
}
 
Example #29
Source File: TypeDeclarationTypeTest.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void numberTypeWithByteFormat() {

    when(numberTypeDeclaration.format()).thenReturn("int8");
    createIntegerTypeNameSetup(numberTypeDeclaration, TypeDeclarationType.NUMBER);

    verify(plugin).typeName(any(ReferencePluginContext.class), any(TypeDeclaration.class), eq(TypeName.BYTE));
}
 
Example #30
Source File: TypeDeclarationTypeTest.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleObjectIsNotNewInlineType() {

    Api api = RamlLoader.load(this.getClass().getResourceAsStream("inline-types.raml"), ".");
    ObjectTypeDeclaration decl = RamlLoader.findTypes("foo", api.types());
    TypeDeclaration property = findProperty(decl, "unextended");

    assertFalse(TypeDeclarationType.isNewInlineType(property));
}