谢谢!我早先让它几乎以相同的方式工作,但我的代码有点复杂,因为它使用返回和参数类型解析。作为基础,我使用了 JPA 函数支持的方法。这是我的代码(通过前面的链接获取第一部分):
2)在容器构建器类中添加一个新方法
public ContainerBuilder addFunctionImport(
final FunctionImport functionImport) {
functionImports.add(functionImport);
return this;
}
3) 修改 AnnotationEdmProvider 类中的 handleEntityContainer 方法如下。
private void handleEntityContainer(final Class<?> aClass) {
EdmEntityType entityType = aClass.getAnnotation(EdmEntityType.class);
if (entityType != null) {
FullQualifiedName typeName = createFqnForEntityType(aClass);
String containerName = ANNOTATION_HELPER
.extractContainerName(aClass);
ContainerBuilder builder = containerName2ContainerBuilder
.get(containerName);
if (builder == null) {
builder = ContainerBuilder.init(typeName.getNamespace(),
containerName);
containerName2ContainerBuilder.put(containerName, builder);
}
EdmEntitySet entitySet = aClass.getAnnotation(EdmEntitySet.class);
if (entitySet != null) {
builder.addEntitySet(createEntitySet(typeName, aClass));
}
buildFunctionImports(aClass, builder);
}
}
private void buildFunctionImports(Class aClass, ContainerBuilder builder) {
for (int i = 0; i < aClass.getMethods().length; i++) {
Method method = aClass.getMethods()[i];
EdmFunctionImport edmAnnotationFunctionImport = method
.getAnnotation(EdmFunctionImport.class);
if (edmAnnotationFunctionImport != null
&& edmAnnotationFunctionImport.returnType() != null) {
try {
FunctionImport fi = new FunctionImport();
fi.setEntitySet(edmAnnotationFunctionImport.entitySet());
fi.setHttpMethod(edmAnnotationFunctionImport.httpMethod().name());
fi.setName(edmAnnotationFunctionImport.name());
builder.addFunctionImport(fi);
builder.addFunctionImport(buildEdmFunctionImport(method,
edmAnnotationFunctionImport));
} catch (ODataAnnotationException e) {
}
}
}
}
private FunctionImport buildEdmFunctionImport(final Method method,
final EdmFunctionImport edmAnnotationFunctionImport)
throws ODataAnnotationException {
if (edmAnnotationFunctionImport != null
&& edmAnnotationFunctionImport.returnType() != null) {
FunctionImport functionImport = new FunctionImport();
if (edmAnnotationFunctionImport.name().equals("")) {
functionImport.setName(method.getName());
} else {
functionImport.setName(edmAnnotationFunctionImport.name());
}
functionImport.setHttpMethod(edmAnnotationFunctionImport
.httpMethod().name().toString());
buildEdmReturnType(functionImport, method,
edmAnnotationFunctionImport);
buildEdmParameter(functionImport, method);
return functionImport;
}
return null;
}
private void buildEdmParameter(final FunctionImport functionImport,
final Method method) throws ODataAnnotationException {
Annotation[][] annotations = method.getParameterAnnotations();
Class<?>[] parameterTypes = method.getParameterTypes();
List<FunctionImportParameter> funcImpList = new ArrayList<FunctionImportParameter>();
int j = 0;
for (Annotation[] annotationArr : annotations) {
Class<?> parameterType = parameterTypes[j++];
for (Annotation element : annotationArr) {
if (element instanceof EdmFunctionImportParameter) {
EdmFunctionImportParameter annotation = (EdmFunctionImportParameter) element;
FunctionImportParameter functionImportParameter = new FunctionImportParameter();
if (annotation.name().equals("")) {
throw new ODataAnnotationException(
"annotation.name().equals(\"\")");
} else {
functionImportParameter.setName(annotation.name());
}
functionImportParameter.setType(TypeBuilder
.getEdmSimpleType(parameterType));
Facets facets = new Facets();
if (annotation.facets().maxLength() > 0) {
facets.setMaxLength(annotation.facets().maxLength());
}
if (annotation.facets().nullable() == false) {
facets.setNullable(false);
} else {
facets.setNullable(true);
}
if (annotation.facets().precision() > 0) {
facets.setPrecision(annotation.facets().precision());
}
if (annotation.facets().scale() >= 0) {
facets.setScale(annotation.facets().scale());
}
functionImportParameter.setFacets(facets);
funcImpList.add(functionImportParameter);
}
}
}
if (!funcImpList.isEmpty()) {
functionImport.setParameters(funcImpList);
}
}
private void buildEdmReturnType(final FunctionImport functionImport,
final Method method,
final EdmFunctionImport edmAnnotationFunctionImport)
throws ODataAnnotationException {
ReturnType returnType = edmAnnotationFunctionImport.returnType();
if (returnType != null) {
org.apache.olingo.odata2.api.edm.provider.ReturnType functionReturnType = new org.apache.olingo.odata2.api.edm.provider.ReturnType();
if (returnType.isCollection()) {
functionReturnType.setMultiplicity(EdmMultiplicity.MANY);
} else {
functionReturnType.setMultiplicity(EdmMultiplicity.ONE);
}
if (returnType.type() == ReturnType.Type.ENTITY) {
String entitySet = edmAnnotationFunctionImport.entitySet();
if (entitySet.equals("")) {
throw new ODataAnnotationException("entitySet.equals(\"\")");
}
functionImport.setEntitySet(entitySet);
}
Class<?> methodReturnType = method.getReturnType();
if (methodReturnType == null
|| methodReturnType.getName().equals("void")) {
throw new ODataAnnotationException("Wrong return type");
}
switch (returnType.type()) {
case ENTITY:
EntityType edmEntityType = null;
String returnTypeName = null;
if (returnType.isCollection() == false) {
returnTypeName = methodReturnType.getSimpleName();
} else {
returnTypeName = getReturnTypeSimpleName(method);
}
for (Class<?> clazz : annotatedClasses) {
if (returnTypeName.equals(clazz.getName())) {
edmEntityType = getEntityType(createFqnForEntityType(clazz));
break;
}
}
if (edmEntityType == null) {
throw new ODataAnnotationException(
"Unable to find an entity type");
}
functionReturnType.setTypeName(edmEntityType.getBaseType());
break;
case SIMPLE:
EdmSimpleTypeKind edmSimpleTypeKind = TypeBuilder
.getEdmSimpleType(methodReturnType);
functionReturnType.setTypeName(edmSimpleTypeKind
.getFullQualifiedName());
break;
case COMPLEX:
String embeddableTypeName = null;
ComplexType complexType = null;
boolean exists = false;
if (returnType.isCollection() == false) {
embeddableTypeName = methodReturnType.getName();
} else {
embeddableTypeName = getReturnTypeName(method);
}
FullQualifiedName fqn = null;
for (Class<?> clazz : annotatedClasses) {
if (embeddableTypeName.equals(clazz.getName())) {
fqn = ANNOTATION_HELPER.extractComplexTypeFqn(clazz);
break;
}
}
if (fqn == null) {
throw new ODataAnnotationException(
"Unable to find an complex type");
}
functionReturnType.setTypeName(fqn);
break;
default:
break;
}
functionImport.setReturnType(functionReturnType);
}
}
private String getReturnTypeName(final Method method) {
try {
ParameterizedType pt = (ParameterizedType) method
.getGenericReturnType();
Type t = pt.getActualTypeArguments()[0];
return ((Class<?>) t).getName();
} catch (ClassCastException e) {
return method.getReturnType().getName();
}
}
private String getReturnTypeSimpleName(final Method method) {
try {
ParameterizedType pt = (ParameterizedType) method
.getGenericReturnType();
Type t = pt.getActualTypeArguments()[0];
return ((Class<?>) t).getSimpleName();
} catch (ClassCastException e) {
return method.getReturnType().getSimpleName();
}
}