Skip to content

Commit

Permalink
Merge pull request #6728 from matthiasblaesing/jakarta2
Browse files Browse the repository at this point in the history
Improve code REST code generation/integration in JavaEE 9+ environments (jakarta namespace change)
  • Loading branch information
matthiasblaesing committed Dec 9, 2023
2 parents 120f212 + bf55d4a commit fc952e1
Show file tree
Hide file tree
Showing 24 changed files with 594 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ public static String findUri(JavaSource rSrc) {
List<? extends AnnotationMirror> annotations = JavaSourceHelper.getClassAnnotations(rSrc);
for (AnnotationMirror annotation : annotations) {
String cAnonType = annotation.getAnnotationType().toString();
if (RestConstants.PATH.equals(cAnonType) ) {
if (RestConstants.PATH_JAKARTA.equals(cAnonType)
|| RestConstants.PATH.equals(cAnonType)) {
path = getValueFromAnnotation(annotation);
}
}
Expand All @@ -253,23 +254,25 @@ public static String findUri(JavaSource rSrc) {

public static boolean isStaticResource(JavaSource src) {
List<? extends AnnotationMirror> annotations = JavaSourceHelper.getClassAnnotations(src);
if (annotations != null && annotations.size() > 0) {
if (annotations != null) {
for (AnnotationMirror annotation : annotations) {
String classAnonType = annotation.getAnnotationType().toString();
if (RestConstants.PATH.equals(classAnonType)) {
if (RestConstants.PATH_JAKARTA.equals(classAnonType)
|| RestConstants.PATH.equals(classAnonType)) {
return true;
}
}
}
}
return false;
}

public static boolean isConverter(JavaSource src) {
List<? extends AnnotationMirror> annotations = JavaSourceHelper.getClassAnnotations(src);
if (annotations != null && annotations.size() > 0) {
if (annotations != null) {
for (AnnotationMirror annotation : annotations) {
String classAnonType = annotation.getAnnotationType().toString();
if (Constants.XML_ROOT_ELEMENT.equals(classAnonType)) {
if (Constants.XML_ROOT_ELEMENT.equals(classAnonType)
|| Constants.XML_ROOT_ELEMENT_JAKARTA.equals(classAnonType)) {
return true;
}
}
Expand All @@ -281,12 +284,16 @@ public static boolean isDynamicResource(JavaSource src) {
List<MethodTree> trees = JavaSourceHelper.getAllMethods(src);
for (MethodTree tree : trees) {
List<? extends AnnotationTree> mAnons = tree.getModifiers().getAnnotations();
if (mAnons != null && mAnons.size() > 0) {
if (mAnons != null) {
for (AnnotationTree mAnon : mAnons) {
String mAnonType = mAnon.getAnnotationType().toString();
if (RestConstants.PATH_ANNOTATION.equals(mAnonType) || RestConstants.PATH.equals(mAnonType)) {
if (RestConstants.PATH_ANNOTATION.equals(mAnonType)
|| RestConstants.PATH_JAKARTA.equals(mAnonType)
|| RestConstants.PATH.equals(mAnonType)) {
return true;
} else if (RestConstants.GET_ANNOTATION.equals(mAnonType) || RestConstants.GET.equals(mAnonType)) {
} else if (RestConstants.GET_ANNOTATION.equals(mAnonType)
|| RestConstants.GET_JAKARTA.equals(mAnonType)
|| RestConstants.GET.equals(mAnonType)) {
return true;
}
}
Expand All @@ -298,7 +305,7 @@ public static boolean isDynamicResource(JavaSource src) {
public static String findElementName(MethodTree tree) {
String eName = "";
List<? extends AnnotationTree> mAnons = tree.getModifiers().getAnnotations();
if (mAnons != null && mAnons.size() > 0) {
if (mAnons != null) {
for (AnnotationTree mAnon : mAnons) {
eName = mAnon.toString();
if (eName.indexOf("\"") != -1) {
Expand All @@ -318,13 +325,16 @@ public static MethodTree findGetAsXmlMethod(JavaSource rSrc) {
boolean isHttpGetMethod = false;
boolean isXmlMime = false;
List<? extends AnnotationTree> mAnons = tree.getModifiers().getAnnotations();
if (mAnons != null && mAnons.size() > 0) {
if (mAnons != null) {
for (AnnotationTree mAnon : mAnons) {
String mAnonType = mAnon.getAnnotationType().toString();
if (RestConstants.GET_ANNOTATION.equals(mAnonType) || RestConstants.GET.equals(mAnonType)) {
if (RestConstants.GET_ANNOTATION.equals(mAnonType)
|| RestConstants.GET_JAKARTA.equals(mAnonType)
|| RestConstants.GET.equals(mAnonType)) {
isHttpGetMethod = true;
} else if (RestConstants.PRODUCE_MIME_ANNOTATION.equals(mAnonType) ||
RestConstants.PRODUCE_MIME.equals(mAnonType)) {
} else if (RestConstants.PRODUCE_MIME_ANNOTATION.equals(mAnonType)
|| RestConstants.PRODUCE_MIME_JAKARTA.equals(mAnonType)
|| RestConstants.PRODUCE_MIME.equals(mAnonType)) {
List<String> mimes = getMimeAnnotationValue(mAnon);
if (mimes.contains(Constants.MimeType.JSON.value()) ||
mimes.contains(Constants.MimeType.XML.value())) {
Expand Down Expand Up @@ -437,7 +447,8 @@ public static boolean hasClass(Project project, String fqn ) throws IOException
}
return false;
}


// @todo: Needs to be adjusted for jakartaEE
public static FileObject createApplicationConfigClass(final RestSupport restSupport, FileObject packageFolder,
String name ) throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,32 +121,34 @@ public static void generateJerseyClient(Node resourceNode,
cp.findResource("javax/ws/rs/core/Application.class") != null;
boolean jaxRs2Available = cp != null &&
cp.findResource("javax/ws/rs/client/Client.class") != null;
boolean jakartaRsClientAvailable = cp != null &&
cp.findResource("jakarta/ws/rs/client/Client.class") != null;
JaxRsStackSupport support = JaxRsStackSupport.getInstance(project);
boolean jersey1AvailableOnServer = support != null &&
support.isBundled("com.sun.jersey.api.client.WebResource");
boolean jersey2AvailableOnServer = support != null &&
support.isBundled("org.glassfish.jersey.spi.Contract");
ClientGenerationStrategy strategy = null;
if (jersey2Available || jersey2AvailableOnServer) {
strategy = new JaxRsGenerationStrategy();
strategy = new JaxRsGenerationStrategy(jakartaRsClientAvailable);
} else if (jersey1Available || jersey1AvailableOnServer) {
strategy = new JerseyGenerationStrategy();
}
if (project != null && strategy == null) {

if (jaxRs2Available) {
strategy = new JaxRsGenerationStrategy();
if (jaxRs2Available || jakartaRsClientAvailable) {
strategy = new JaxRsGenerationStrategy(jakartaRsClientAvailable);
} else if (jaxRs1Available) {
// JAX-RS 1.0 is on classpath but no Jersey; in this case project
// classpath needs to be enhanced with Jersey library but IDE has
// only Jersey 2.0. That's why JaxRsGenerationStrategy strategy is
// going to be used here:
strategy = new JaxRsGenerationStrategy();
strategy = new JaxRsGenerationStrategy(jakartaRsClientAvailable);
}
}
// if all other tests were negative then generate the code using JAX-RS 2:
if (strategy == null) {
strategy = new JaxRsGenerationStrategy();
strategy = new JaxRsGenerationStrategy(jakartaRsClientAvailable);
}
ProgressHandle handle = null;
if (support == null) {
Expand All @@ -164,7 +166,7 @@ public static void generateJerseyClient(Node resourceNode,
handle = ProgressHandleFactory.createHandle(NbBundle.getMessage(ClientJavaSourceHelper.class, "MSG_creatingRESTClient"));
handle.start();
// add REST and Jersey dependencies
if (!jaxRs2Available && !jaxRs1Available) {
if (!jaxRs2Available && !jaxRs1Available && !jakartaRsClientAvailable) {
support.addJsr311Api(project);
support.extendsJerseyProjectClasspath(project);
}
Expand Down
Loading

0 comments on commit fc952e1

Please sign in to comment.