Skip to content

Commit

Permalink
Exclude the element from generation (#4474)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZheSun88 authored and SomeoneToIgnore committed Aug 2, 2018
1 parent d83a785 commit 31ffb76
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 871 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.vaadin.generator;

import javax.annotation.Generated;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -35,9 +36,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils;
import org.jboss.forge.roaster.Roaster;
Expand All @@ -53,6 +51,9 @@
import org.jboss.forge.roaster.model.source.ParameterSource;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vaadin.flow.component.AbstractSinglePropertyField;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.ComponentEvent;
Expand Down Expand Up @@ -87,6 +88,7 @@
import com.vaadin.generator.registry.PropertyNameRemapRegistry;

import elemental.json.JsonObject;

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

Expand Down Expand Up @@ -369,6 +371,7 @@ public String generateClass(File jsonFile, String basePackage,
*/
private JavaClassSource generateClassSource(ComponentMetadata metadata,
String basePackage) {

String targetPackage = basePackage;
String baseUrl = metadata.getBaseUrl();
if (StringUtils.isNotBlank(baseUrl)) {
Expand Down Expand Up @@ -965,8 +968,10 @@ public void generateClass(ComponentMetadata metadata, File targetPath,
throw new ComponentGenerationException(
"Could not create target directory \"" + targetPath + "\"");
}
writeClass(metadata.getName(), targetPath,
generateClassSource(metadata, basePackage), licenseNote);
if (!ExclusionRegistry.isTagExcluded(metadata.getTag())) {
writeClass(metadata.getName(), targetPath,
generateClassSource(metadata, basePackage), licenseNote);
}
}

private <T extends Named & Packaged<?>> void writeClass(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class ExclusionRegistry {
private static final Map<String, Set<String>> METHOD_EXCLUSION_REGISTRY = new HashMap<>();
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<>();

static {
excludeProperty("vaadin-combo-box", "value");
Expand All @@ -61,6 +62,11 @@ public class ExclusionRegistry {
excludeInterface("vaadin-dialog", HasStyle.class);
excludeInterface("vaadin-notification", HasStyle.class);

// this is a workaround
// current generator generates wrong file for this element
// https://github.com/vaadin/flow/issues/4477
// https://github.com/vaadin/flow/issues/4479
excludeTag("vaadin-time-picker-text-field");
// Polymer lifecycle callbacks
excludeMethod(null, "connectedCallback");
excludeMethod(null, "disconnectedCallback");
Expand All @@ -70,6 +76,32 @@ public class ExclusionRegistry {
private ExclusionRegistry() {
}

/**
* Excludes the element generation denoted by its tag.
*
* @param elementTag
* the tag of the element, which is going to be skipped
* generation.
*/
public static void excludeTag(String elementTag) {
Objects.requireNonNull(elementTag, "elementTag cannot be null");
if (!TAG_EXCLUSION_REGISTRY.contains(elementTag)) {
TAG_EXCLUSION_REGISTRY.add(elementTag);
}
}

/**
* Gets whether an Element should be excluded or not from the generation.
*
* @param elementTag
* the tag of the element
* @return <code>true</code> if the element should be excluded,
* <code>false</code> otherwise
*/
public static boolean isTagExcluded(String elementTag) {
return TAG_EXCLUSION_REGISTRY.contains(elementTag);
}

private static void put(String elementTag, String name,
Map<String, Set<String>> map) {
Objects.requireNonNull(name, "elementTag cannot be null.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,16 @@ public void excludeInterface() {
.isInterfaceExcluded("some-other-tag", HasStyle.class));
}

@Test
public void excludeTag() {
ExclusionRegistry.excludeTag("some-tag");
Assert.assertTrue(ExclusionRegistry.isTagExcluded("some-tag"));
Assert.assertFalse(ExclusionRegistry.isTagExcluded("some-other-tag"));
}

@Test(expected = NullPointerException.class)
public void excludeTagWithNullValue() {
ExclusionRegistry.excludeTag(null);
}

}
Loading

0 comments on commit 31ffb76

Please sign in to comment.