Skip to content

Commit

Permalink
Value from Java enum are not correct
Browse files Browse the repository at this point in the history
Fixes redhat-developer#198

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Jan 27, 2020
1 parent 9713bc6 commit 84e93d3
Show file tree
Hide file tree
Showing 30 changed files with 1,094 additions and 251 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ Bundle-ActivationPolicy: lazy
Export-Package: com.redhat.microprofile.commons,
com.redhat.microprofile.commons.metadata,
com.redhat.microprofile.jdt.core,
com.redhat.microprofile.jdt.core.utils
com.redhat.microprofile.jdt.core.utils,
io.quarkus.runtime.util
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,21 @@ public ItemHint getHint(String... hint) {
return null;
}

/**
* Returns true if the given <code>value</code> is a valid enumeration for the
* given <code>metadata</code> and false otherwise.
*
* @param metadata the property.
* @param value the value to check.
* @return true if the given <code>value</code> is a valid enumeration for the
* given <code>metadata</code> and false otherwise.
*/
public boolean isValidEnum(ItemMetadata metadata, String value) {
ItemHint itemHint = getHint(metadata);
if (itemHint == null) {
return true;
}
return itemHint.getValue(value) != null;
return itemHint.getValue(value, metadata.getConverterKinds()) != null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*******************************************************************************
* Copyright (c) 2019 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package com.redhat.microprofile.commons.metadata;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.quarkus.runtime.util.StringUtil;

/**
* Converter kind.
*
* @author Angelo ZERR
*
*/
public enum ConverterKind {

KEBAB_CASE(1), VERBATIM(2);

private final int value;

ConverterKind(int value) {
this.value = value;
}

public int getValue() {
return value;
}

public static ConverterKind forValue(int value) {
ConverterKind[] allValues = ConverterKind.values();
if (value < 1 || value > allValues.length)
throw new IllegalArgumentException("Illegal enum value: " + value);
return allValues[value - 1];
}

private static final String HYPHEN = "-";
private static final Pattern PATTERN = Pattern.compile("([-_]+)");

public static String convert(String value, ConverterKind converterKind) {
if (converterKind == null || value == null) {
return value;
}

switch (converterKind) {
case KEBAB_CASE:
return hyphenate(value);
default:
return value;
}

}

/**
* Convert the given value to kebab case.
*
* @param value
* @return
* @see https://github.com/quarkusio/quarkus/blob/1457e12766d46507674f8e8d8391fc5a5e8f0103/core/runtime/src/main/java/io/quarkus/runtime/configuration/HyphenateEnumConverter.java#L54
*/
private static String hyphenate(String value) {
StringBuffer target = new StringBuffer();
String hyphenate = StringUtil.hyphenate(value);
Matcher matcher = PATTERN.matcher(hyphenate);
while (matcher.find()) {
matcher.appendReplacement(target, HYPHEN);
}
matcher.appendTail(target);
return target.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,39 @@ public static class ValueHint {

private String sourceType;

/**
* Returns the value.
*
* @return the value.
*/
public String getValue() {
return value;
}

/**
* Returns the converted value by using the given converter.
*
* @param converterKind the converter
* @return the converted value by using the given converter.
*/
public String getValue(ConverterKind converterKind) {
return ConverterKind.convert(getValue(), converterKind);
}

/**
* Returns the preferred value according the given converters.
*
* @param converterKinds supported converters and null otherwise.
*
* @return the preferred value according the given converters.
*/
public String getPreferredValue(List<ConverterKind> converterKinds) {
ConverterKind preferredConverter = converterKinds != null && !converterKinds.isEmpty()
? converterKinds.get(0)
: null;
return getValue(preferredConverter);
}

public void setValue(String value) {
this.value = value;
}
Expand Down Expand Up @@ -101,17 +130,32 @@ public boolean equals(Object obj) {
return false;
return true;
}

}

public ValueHint getValue(String value) {
/**
* Returns the value hint from the given <code>value</code> and supported
* converters <code>converterKinds</code> and null otherwise.
*
* @param value the value
* @param converterKinds the supported converters.
* @return the value hint from the given <code>value</code> and supported
* converters <code>converterKinds</code> and null otherwise.
*/
public ValueHint getValue(String value, List<ConverterKind> converterKinds) {
if (values == null || value == null) {
return null;
}
for (ValueHint valueHint : values) {
if (value.equals(valueHint.getValue())) {
if (converterKinds != null) {
for (ConverterKind converterKind : converterKinds) {
if (value.equals(valueHint.getValue(converterKind))) {
return valueHint;
}
}
} else if (value.equals(valueHint.getValue())) {
return valueHint;
}

}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*******************************************************************************/
package com.redhat.microprofile.commons.metadata;

import java.util.List;

/**
* Configuration item metadata.
*
Expand Down Expand Up @@ -47,6 +49,8 @@ public class ItemMetadata extends ItemBase {
private boolean required;
private int phase;

private List<ConverterKind> converterKinds;

public String getType() {
return type;
}
Expand Down Expand Up @@ -103,6 +107,14 @@ public void setPhase(int phase) {
this.phase = phase;
}

public List<ConverterKind> getConverterKinds() {
return converterKinds;
}

public void setConverterKinds(List<ConverterKind> converterKinds) {
this.converterKinds = converterKinds;
}

public boolean isAvailableAtRun() {
return phase == CONFIG_PHASE_BUILD_AND_RUN_TIME_FIXED || phase == CONFIG_PHASE_RUN_TIME;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
*******************************************************************************/
package com.redhat.microprofile.jdt.core;

import static com.redhat.microprofile.jdt.core.utils.JDTTypeUtils.findType;
import static com.redhat.microprofile.jdt.core.utils.JDTTypeUtils.getOptionalTypeParameter;
import static com.redhat.microprofile.jdt.core.utils.JDTTypeUtils.getPropertyType;

import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
Expand Down Expand Up @@ -147,26 +145,13 @@ protected ItemMetadata addItemMetadata(IPropertiesCollector collector, String na
* @return the hint name.
* @throws JavaModelException
*/
protected String updateHint(IPropertiesCollector collector, IType type, String typeName, IJavaProject javaProject)
throws JavaModelException {
// type name is the string of the JDT type (which could be null if type is not
// retrieved)
String enclosedType = typeName;
protected String updateHint(IPropertiesCollector collector, IType type) throws JavaModelException {
if (type == null) {
// JDT type is null, in some case it's because type is optional (ex :
// java.util.Optional<MyType>)
// try to extract the enclosed type from the optional type (to get 'MyType' )
enclosedType = getOptionalTypeParameter(typeName);
if (enclosedType != null) {
type = findType(javaProject, enclosedType);
}
if (type == null) {
return null;
}
return null;
}
if (type.isEnum()) {
// Register Enumeration in "hints" section
String hint = enclosedType;
String hint = getPropertyType(type, null);
if (!collector.hasItemHint(hint)) {
ItemHint itemHint = collector.getItemHint(hint);
itemHint.setSourceType(hint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static String getPropertyType(IType type, String typeName) {
}

public static String getSourceType(IMember member) {
return member.getDeclaringType().getFullyQualifiedName('.');
return getPropertyType(member.getDeclaringType(), null);
}

public static String getSourceField(IField field) {
Expand Down Expand Up @@ -133,6 +133,23 @@ public static String getOptionalTypeParameter(String typeName) {
return typeName.substring(start + 1, end);
}

public static IType getEnclosedType(IType type, String typeName, IJavaProject javaProject)
throws JavaModelException {
// type name is the string of the JDT type (which could be null if type is not
// retrieved)
String enclosedType = typeName;
if (type == null) {
// JDT type is null, in some case it's because type is optional (ex :
// java.util.Optional<MyType>)
// try to extract the enclosed type from the optional type (to get 'MyType' )
enclosedType = getOptionalTypeParameter(typeName);
if (enclosedType != null) {
type = findType(javaProject, enclosedType);
}
}
return type;
}

public static String[] getRawTypeParameters(String fieldTypeName) {
int start = fieldTypeName.indexOf("<") + 1;
int end = fieldTypeName.lastIndexOf(">");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import static com.redhat.microprofile.jdt.core.utils.AnnotationUtils.getAnnotationMemberValue;
import static com.redhat.microprofile.jdt.core.utils.JDTTypeUtils.findType;
import static com.redhat.microprofile.jdt.core.utils.JDTTypeUtils.getEnclosedType;
import static com.redhat.microprofile.jdt.core.utils.JDTTypeUtils.getPropertyType;
import static com.redhat.microprofile.jdt.core.utils.JDTTypeUtils.getResolvedTypeName;
import static com.redhat.microprofile.jdt.core.utils.JDTTypeUtils.getSourceField;
Expand Down Expand Up @@ -66,7 +67,8 @@ protected void processAnnotation(IJavaElement javaElement, IAnnotation configPro
String extensionName = null;

// Enumerations
super.updateHint(collector, fieldClass, type, field.getJavaProject());
IType enclosedType = getEnclosedType(fieldClass, type, field.getJavaProject());
super.updateHint(collector, enclosedType);

addItemMetadata(collector, name, type, description, sourceType, sourceField, null, defaultValue,
extensionName, field.isBinary());
Expand Down
Loading

0 comments on commit 84e93d3

Please sign in to comment.