Skip to content

Commit

Permalink
fix bug: annotation parser parseSingleValue
Browse files Browse the repository at this point in the history
  • Loading branch information
ningpp committed Aug 17, 2024
1 parent b59f426 commit dd286a5
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import me.ningpp.mmegp.annotationparser.handler.IntAnnotationValueHandler;
import me.ningpp.mmegp.annotationparser.handler.LongAnnotationValueHandler;
import me.ningpp.mmegp.annotationparser.handler.ShortAnnotationValueHandler;
import me.ningpp.mmegp.annotationparser.handler.StringAnnotationValueHandler;

import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
Expand All @@ -43,6 +44,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
Expand All @@ -57,10 +59,15 @@ public class ModelBasedAnnotationParser {

public static final char DOT = '.';

public static <M, N extends Node> M parse(Class<? extends Annotation> annotationClass, Class<M> clazz,
NodeWithAnnotations<N> node, Collection<ImportDeclaration> importDeclars) {
return parse(annotationClass, clazz, node, importDeclars, clazz.getPackageName());
}

public static <M, N extends Node> M parse(Class<? extends Annotation> annotationClass,
Class<M> clazz,
NodeWithAnnotations<N> node,
NodeList<ImportDeclaration> importDeclars,
Collection<ImportDeclaration> importDeclars,
String modelPackage) {
Optional<AnnotationExpr> optionalColumnAnno = node.getAnnotationByClass(annotationClass);
Map<String, String> importMap = importDeclars.stream()
Expand Down Expand Up @@ -93,13 +100,17 @@ public static <M> M parse(Class<? extends Annotation> annotationClass,
throw new IllegalArgumentException("Model don't match Annotation, they have different fields.");
}

Map<String, MemberValuePair> memberValuePairs = getMemberValuePairs(optionalColumnAnno);
Object[] args = initargs(annotationClass, memberValuePairs, importMap, modelPackage);
try {
return (M) constructor.newInstance(args);
} catch (Exception e) {
throw new IllegalStateException(e);
M result = null;
if (optionalColumnAnno.isPresent()) {
Map<String, MemberValuePair> memberValuePairs = getMemberValuePairs(optionalColumnAnno);
Object[] args = initargs(annotationClass, memberValuePairs, importMap, modelPackage);
try {
result = (M) constructor.newInstance(args);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
return result;
}

private static Map<String, MemberValuePair> getMemberValuePairs(Optional<AnnotationExpr> optionalExpr) {
Expand Down Expand Up @@ -335,7 +346,7 @@ private static Object parseArrayValue(Method method, MemberValuePair valuePair,
return array;
}

private static Object parseSingleValue(Class<?> returnType, Expression expr,
public static Object parseSingleValue(Class<?> returnType, Expression expr,
Map<String, String> importMap, String modelPackage) {
Object val = null;
for (AnnotationValueHandler handler : HANDLERS) {
Expand All @@ -353,6 +364,7 @@ private static Object parseSingleValue(Class<?> returnType, Expression expr,
private static final List<AnnotationValueHandler> HANDLERS;
static {
List<AnnotationValueHandler> handlers = new ArrayList<>();
handlers.add(new StringAnnotationValueHandler());
handlers.add(new BooleanAnnotationValueHandler());
handlers.add(new ByteAnnotationValueHandler());
handlers.add(new CharAnnotationValueHandler());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2021-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.ningpp.mmegp.annotationparser.handler;

import com.github.javaparser.ast.expr.Expression;
import me.ningpp.mmegp.annotationparser.AnnotationValueHandler;

import java.util.Map;

public class StringAnnotationValueHandler implements AnnotationValueHandler {

@Override
public boolean support(Class<?> returnType) {
return returnType == String.class;
}

@Override
public Object handle(Class<?> returnType, Expression expr, Map<String, String> importMap, String modelPackage) {
if (expr.isStringLiteralExpr()) {
return expr.asStringLiteralExpr().asString();
} else if (expr.isTextBlockLiteralExpr()) {
return expr.asTextBlockLiteralExpr().asString();
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ParserConfiguration.LanguageLevel;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.IntegerLiteralExpr;
import com.github.javaparser.ast.expr.StringLiteralExpr;
import com.github.javaparser.ast.expr.TextBlockLiteralExpr;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.util.Map;

import static me.ningpp.mmegp.annotationparser.ModelBasedAnnotationParser.parseSingleValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

class ModelBasedAnnotationParserTest {

Expand Down Expand Up @@ -134,8 +142,7 @@ public class Abc {}
ParserDemoAnnotationModel model = ModelBasedAnnotationParser.parse(ParserDemoAnnotation.class,
ParserDemoAnnotationModel.class,
compilationUnit.getType(0),
compilationUnit.getImports(),
ParserDemoAnnotationModel.class.getPackageName());
compilationUnit.getImports());
assertNotNull(model);
ParserDemoAnnotationsModel multiModel = ModelBasedAnnotationParser.parse(ParserDemoAnnotations.class,
ParserDemoAnnotationsModel.class,
Expand All @@ -145,6 +152,20 @@ public class Abc {}
assertNotNull(multiModel);
}

@Test
void parseSingleValueTest() {
Expression exp = new StringLiteralExpr("mmegp");
Object parsed = parseSingleValue(String.class, exp, Map.of(), "me.ningpp");
assertEquals("mmegp", parsed);

exp = new TextBlockLiteralExpr("mmegp");
parsed = parseSingleValue(String.class, exp, Map.of(), "me.ningpp");
assertEquals("mmegp", parsed);

assertThrows(IllegalArgumentException.class, () -> parseSingleValue(
String.class, new IntegerLiteralExpr("1"), Map.of(), "me.ningpp"));
}

private static JavaParser newParser() {
ParserConfiguration jpc = new ParserConfiguration();
jpc.setLanguageLevel(LanguageLevel.JAVA_17);
Expand Down

0 comments on commit dd286a5

Please sign in to comment.