下面列出了javax.annotation.concurrent.Immutable#com.helger.jcodemodel.JDefinedClass 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
@SneakyThrows
public JCodeModel generate() {
final JDefinedClass c = camundaRestService.getDefinedClass();
// class information
c._extends(camundaRestService.getServiceImplClass());
c.annotate(codeModel.ref(Path.class)).param("value", camundaRestService.getPath());
final String tags = TagRespository.lookup(camundaRestService);
camundaRestService.getRestService().setPath(camundaRestService.getPath());
camundaRestService.getRestService().setTags(tags);
camundaRestService.getRestService().setDescription(camundaRestService.getName());
c.annotate(codeModel.ref(Api.class)).param("value", camundaRestService.getName()).param("tags", tags);
// generate constructor
for (final Constructor<?> constructor : camundaRestService.getServiceImplClass().getConstructors()) {
new InvocationStep(c.constructor(constructor.getModifiers())).constructor(constructor);
}
// construct return type information
final Map<Method, ReturnTypeInfo> returnTypes = Arrays.stream(camundaRestService.getServiceInterfaceClass().getDeclaredMethods()) // iterate over interface
// methods
.map(m -> new ReturnTypeInfo(camundaRestService.getModelRepository(), codeModel, m)
.applyImplementationMethods(camundaRestService.getServiceImplClass().getDeclaredMethods())) // apply impl methods
.collect(Collectors.toMap(r -> r.getMethod(), r -> r)); // build the map
generateMethods(c, camundaRestService.getRestService(), returnTypes, NO_PREFIX);
return this.codeModel;
}
private void generateMethods(final JDefinedClass clazz, final RestService restService, final Map<Method, ReturnTypeInfo> methods, final String parentPathPrefix,
final ParentInvocation... parentInvocations) {
for (final Method m : methods.keySet()) {
// create method
final MethodStep methodStep = new MethodStep(camundaRestService.getModelRepository(), restService, clazz);
methodStep.create(methods.get(m), Pair.of(camundaRestService.getPath(), parentPathPrefix), camundaRestService.getModelRepository().getDocumentation().get(), parentInvocations);
// dive into resource processing
if (TypeHelper.isResource(methodStep.getReturnType())) {
// add doc reference
final RestService resourceService = new RestService();
resourceService.setTags(TagRespository.lookup(camundaRestService));
// path is not needed for the sub resource
// resourceService.setPath(camundaRestService.getPath() + methodStep.getPath());
camundaRestService.getModelRepository().addService(methodStep.getReturnType().getName(), resourceService);
generateMethods(clazz, // the class
resourceService,
ResourceMethodGenerationHelper.resourceReturnTypeInfos(camundaRestService.getModelRepository(), codeModel, methodStep.getReturnType()), // info about return types
methodStep.getPath(), // path prefix to generate REST paths
ResourceMethodGenerationHelper.createParentInvocations(parentInvocations, m) // invocation hierarchy
);
}
}
}
@SuppressWarnings("unchecked")
private boolean removeRuntimePermissionsAnnotation(JDefinedClass definedClass) {
try {
Field annotationsField = definedClass.getClass().getDeclaredField("m_aAnnotations");
annotationsField.setAccessible(true);
List<JAnnotationUse> annotations = (List<JAnnotationUse>) annotationsField.get(definedClass);
if (annotations == null) {
return true;
}
annotations.removeIf(jAnnotationUse -> jAnnotationUse.getAnnotationClass().name().equals("RuntimePermissions"));
return true;
} catch (ClassCastException | NoSuchFieldException | IllegalAccessException e) {
return false;
}
}
public MethodStep(final ModelRepository modelRepository, final RestService restService, final JDefinedClass clazz) {
this.modelRepository = modelRepository;
this.restService = restService;
this.clazz = clazz;
}