Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude function properties from generated components #4673

Merged
merged 5 commits into from Oct 8, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -15,7 +15,8 @@
*/
package com.vaadin.generator;

import javax.annotation.Generated;
import static com.vaadin.generator.registry.ValuePropertyRegistry.valueName;
import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.File;
import java.io.IOException;
Expand All @@ -36,6 +37,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.annotation.Generated;

import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils;
import org.jboss.forge.roaster.Roaster;
Expand Down Expand Up @@ -89,9 +92,6 @@

import elemental.json.JsonObject;

import static com.vaadin.generator.registry.ValuePropertyRegistry.valueName;
import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Base class of the component generation process. It takes a
* {@link ComponentMetadata} as input and generates the corresponding Java class
Expand Down Expand Up @@ -484,7 +484,8 @@ private void generateEventsForPropertiesWithNotify(
ComponentMetadata metadata) {
metadata.getProperties().stream()
.filter(property -> !ExclusionRegistry.isPropertyExcluded(
metadata.getTag(), property.getName()))
metadata.getTag(), property.getName(),
property.getType()))
.filter(ComponentPropertyData::isNotify).forEachOrdered(
property -> generateEventForPropertyWithNotify(metadata,
property));
Expand Down Expand Up @@ -772,7 +773,8 @@ private void generateGettersAndSetters(ComponentMetadata metadata,

metadata.getProperties().stream()
.filter(property -> !ExclusionRegistry.isPropertyExcluded(
metadata.getTag(), property.getName()))
metadata.getTag(), property.getName(),
property.getType()))
.forEachOrdered(property -> {

boolean isValue = hasValue
Expand Down
Expand Up @@ -23,6 +23,6 @@
*/
public enum ComponentBasicType implements ComponentType {

BOOLEAN, DATE, NUMBER, STRING, ARRAY, OBJECT, UNDEFINED;
BOOLEAN, DATE, NUMBER, STRING, ARRAY, OBJECT, FUNCTION, UNDEFINED;

}
Expand Up @@ -23,6 +23,7 @@
import java.util.Set;

import com.vaadin.flow.component.HasStyle;
import com.vaadin.generator.metadata.ComponentBasicType;

/**
* Registry for all exclusions in the generated files. Excluded properties,
Expand All @@ -40,8 +41,10 @@ public class ExclusionRegistry {
private static final Map<String, Set<String>> BEHAVIOR_EXCLUSION_REGISTRY = new HashMap<>();
private static final Map<String, Set<String>> INTERFACE_EXCLUSION_REGISTRY = new HashMap<>();
private static final Set<String> TAG_EXCLUSION_REGISTRY = new HashSet<>();
private static final Set<ComponentBasicType> PROPERTY_TYPE_EXCLUSION_REGISTRY = new HashSet<>();

static {
excludePropertyType(ComponentBasicType.FUNCTION);
excludeProperty("vaadin-combo-box", "value");
excludeProperty("vaadin-radio-group", "value");
excludeProperty("vaadin-combo-box", "itemLabelPath");
Expand Down Expand Up @@ -90,6 +93,20 @@ public static void excludeTag(String elementTag) {
}
}

/**
* Excludes all the properties that have the given type from all the
* components.
*
* @param type
* the property type to exclude
*/
public static void excludePropertyType(ComponentBasicType type) {
Objects.requireNonNull(type, "type cannot be null");
if (!PROPERTY_TYPE_EXCLUSION_REGISTRY.contains(type)) {
PROPERTY_TYPE_EXCLUSION_REGISTRY.add(type);
}
}

/**
* Gets whether an Element should be excluded or not from the generation.
*
Expand Down Expand Up @@ -204,11 +221,20 @@ private static boolean isExcluded(String elementTag, String name,
* the tag of the element
* @param propertyName
* the name of the property
* @param type
* the type of the property
* @return <code>true</code> if the property should be excluded,
* <code>false</code> otherwise
*/
public static boolean isPropertyExcluded(String elementTag,
String propertyName) {
String propertyName, Set<ComponentBasicType> type) {

boolean hasExcludedType = type != null && type.stream()
.anyMatch(PROPERTY_TYPE_EXCLUSION_REGISTRY::contains);
if (hasExcludedType) {
return true;
}

return isExcluded(elementTag, propertyName,
PROPERTY_EXCLUSION_REGISTRY);
}
Expand Down
@@ -1,17 +1,26 @@
package com.vaadin.generator.registry;

import java.util.Arrays;
import java.util.HashSet;

import org.junit.Assert;
import org.junit.Test;

import com.vaadin.flow.component.HasEnabled;
import com.vaadin.flow.component.HasStyle;
import com.vaadin.generator.metadata.ComponentBasicType;

/**
* Unit tests for the {@link ExclusionRegistry}.
*
*/
public class ExclusionRegistryTest {

private static HashSet<ComponentBasicType> numberType = new HashSet<>(
Arrays.asList(ComponentBasicType.NUMBER));
private static HashSet<ComponentBasicType> functionType = new HashSet<>(
Arrays.asList(ComponentBasicType.FUNCTION));

@Test
public void excludeEvents() {
ExclusionRegistry.excludeEvent("some-tag", "someEvent");
Expand All @@ -24,10 +33,10 @@ public void excludeEvents() {
Assert.assertTrue(ExclusionRegistry.isEventExcluded("some-other-tag",
"someOtherEvent"));

Assert.assertFalse(
ExclusionRegistry.isPropertyExcluded("some-tag", "someEvent"));
Assert.assertFalse(ExclusionRegistry.isPropertyExcluded("some-tag",
"someOtherEvent"));
"someEvent", numberType));
Assert.assertFalse(ExclusionRegistry.isPropertyExcluded("some-tag",
"someOtherEvent", numberType));
Assert.assertFalse(
ExclusionRegistry.isMethodExcluded("some-tag", "someEvent"));
Assert.assertFalse(ExclusionRegistry.isMethodExcluded("some-tag",
Expand All @@ -44,11 +53,11 @@ public void excludeProperties() {
ExclusionRegistry.excludeProperty(null, "someOtherProperty");

Assert.assertTrue(ExclusionRegistry.isPropertyExcluded("some-tag",
"someProperty"));
"someProperty", numberType));
Assert.assertTrue(ExclusionRegistry.isPropertyExcluded("some-tag",
"someOtherProperty"));
"someOtherProperty", numberType));
Assert.assertTrue(ExclusionRegistry.isPropertyExcluded("some-other-tag",
"someOtherProperty"));
"someOtherProperty", numberType));

Assert.assertFalse(
ExclusionRegistry.isEventExcluded("some-tag", "someProperty"));
Expand All @@ -64,6 +73,13 @@ public void excludeProperties() {
.isBehaviorOrMixinExcluded("some-tag", "someOtherProperty"));
}

@Test
public void excludePropertyTypes() {
ExclusionRegistry.excludePropertyType(ComponentBasicType.FUNCTION);
Assert.assertTrue(ExclusionRegistry.isPropertyExcluded("some-tag",
"someProperty", functionType));
}

@Test
public void excludeMethods() {
ExclusionRegistry.excludeMethod("some-tag", "someMethod");
Expand All @@ -80,10 +96,10 @@ public void excludeMethods() {
ExclusionRegistry.isEventExcluded("some-tag", "someMethod"));
Assert.assertFalse(ExclusionRegistry.isEventExcluded("some-tag",
"someOtherMethod"));
Assert.assertFalse(
ExclusionRegistry.isPropertyExcluded("some-tag", "someMethod"));
Assert.assertFalse(ExclusionRegistry.isPropertyExcluded("some-tag",
"someOtherMethod"));
"someMethod", numberType));
Assert.assertFalse(ExclusionRegistry.isPropertyExcluded("some-tag",
"someOtherMethod", numberType));
Assert.assertFalse(ExclusionRegistry
.isBehaviorOrMixinExcluded("some-tag", "someMethod"));
Assert.assertFalse(ExclusionRegistry
Expand Down Expand Up @@ -111,9 +127,9 @@ public void excludeBehaviors() {
Assert.assertFalse(ExclusionRegistry.isMethodExcluded("some-tag",
"someOtherBehavior"));
Assert.assertFalse(ExclusionRegistry.isPropertyExcluded("some-tag",
"someBehavior"));
"someBehavior", numberType));
Assert.assertFalse(ExclusionRegistry.isPropertyExcluded("some-tag",
"someOtherBehavior"));
"someOtherBehavior", numberType));

}

Expand Down
Expand Up @@ -13,6 +13,22 @@
"description": "",
"notify": true
},
{
"name": "owner",
"type": [
"OBJECT"
],
"objectType": [],
"description": "Owner element passed with renderer function\n "
},
{
"name": "renderer",
"type": [
"FUNCTION"
],
"objectType": [],
"description": "Custom function for rendering the content of the overlay.\nReceives three arguments:\n\n- `root` The root container DOM element. Append your content to it.\n- `owner` The host element of the renderer function.\n- `model` The object with the properties related with rendering.\n "
},
{
"name": "template",
"type": [
Expand Down Expand Up @@ -47,6 +63,14 @@
"objectType": [],
"description": ""
},
{
"name": "model",
"type": [
"OBJECT"
],
"objectType": [],
"description": "Object with properties that is passed to `renderer` function\n "
},
{
"name": "modeless",
"type": [
Expand Down Expand Up @@ -91,6 +115,12 @@
"description": "",
"parameters": [],
"returns": "UNDEFINED"
},
{
"name": "render",
"description": "Manually invoke existing renderer.\n ",
"parameters": [],
"returns": "UNDEFINED"
}
],
"events": [
Expand Down
Expand Up @@ -13,6 +13,22 @@
"description": "",
"notify": true
},
{
"name": "owner",
"type": [
"OBJECT"
],
"objectType": [],
"description": "Owner element passed with renderer function\n "
},
{
"name": "renderer",
"type": [
"FUNCTION"
],
"objectType": [],
"description": "Custom function for rendering the content of the overlay.\nReceives three arguments:\n\n- `root` The root container DOM element. Append your content to it.\n- `owner` The host element of the renderer function.\n- `model` The object with the properties related with rendering.\n "
},
{
"name": "template",
"type": [
Expand Down Expand Up @@ -47,6 +63,14 @@
"objectType": [],
"description": ""
},
{
"name": "model",
"type": [
"OBJECT"
],
"objectType": [],
"description": "Object with properties that is passed to `renderer` function\n "
},
{
"name": "modeless",
"type": [
Expand Down Expand Up @@ -91,6 +115,12 @@
"description": "",
"parameters": [],
"returns": "UNDEFINED"
},
{
"name": "render",
"description": "Manually invoke existing renderer.\n ",
"parameters": [],
"returns": "UNDEFINED"
}
],
"events": [
Expand Down
Expand Up @@ -13,6 +13,22 @@
"description": "",
"notify": true
},
{
"name": "owner",
"type": [
"OBJECT"
],
"objectType": [],
"description": "Owner element passed with renderer function\n "
},
{
"name": "renderer",
"type": [
"FUNCTION"
],
"objectType": [],
"description": "Custom function for rendering the content of the overlay.\nReceives three arguments:\n\n- `root` The root container DOM element. Append your content to it.\n- `owner` The host element of the renderer function.\n- `model` The object with the properties related with rendering.\n "
},
{
"name": "template",
"type": [
Expand Down Expand Up @@ -47,6 +63,14 @@
"objectType": [],
"description": ""
},
{
"name": "model",
"type": [
"OBJECT"
],
"objectType": [],
"description": "Object with properties that is passed to `renderer` function\n "
},
{
"name": "modeless",
"type": [
Expand Down Expand Up @@ -91,6 +115,12 @@
"description": "",
"parameters": [],
"returns": "UNDEFINED"
},
{
"name": "render",
"description": "Manually invoke existing renderer.\n ",
"parameters": [],
"returns": "UNDEFINED"
}
],
"events": [
Expand Down