3

I'm trying to build a class in this way (using the JavaPoet lib):

theClass = TypeSpec.classBuilder(classe.getName())
                                    .addModifiers(javax.lang.model.element.Modifier.valueOf(classe.getProte().toString().toUpperCase()), Modifier.FINAL) //todo
                                    .addMethods(methods)
                                    .superclass(father)
                                    .addFields(fields)
                                    .build();

Where the field "father" can be nothing. I tried using to put NULL inside it, but it gives an exception this way. Isn't there a smart way to write it?

I could write something like

if (father!=null){
   theClass = TypeSpec.classBuilder(classe.getName())
                                .addModifiers(javax.lang.model.element.Modifier.valueOf(classe.getProte().toString().toUpperCase()), Modifier.FINAL) //todo
                                .addMethods(methods)
                                .superclass(father)
                                .addFields(fields)
                                .build();
}
else{
   theClass = TypeSpec.classBuilder(classe.getName())
                                .addModifiers(javax.lang.model.element.Modifier.valueOf(classe.getProte().toString().toUpperCase()), Modifier.FINAL) //todo
                                .addMethods(methods)
                                .addFields(fields)
                                .build();
}

But that wouldn't be nice. Any advice?

  • 1
    Does a father == null ? Object.class : father work ? – muzzlator Mar 26 '17 at 20:16
  • *Smart* and *nice* meanings are not very clear in this context. Please add some details in what do you mean by them. For instance, _it wouldn't be nice_ **because it adds duplicate code** – Gonzalo Matheu Mar 26 '17 at 21:27
  • muzzlator-> Almost right: (father==null?ClassName.OBJECT:father) but thank you very much! @GonzaloMatheu Thanks! I will remember that! –  Mar 27 '17 at 21:21

2 Answers2

3

One thing you can do immediately to shorten the code is to populate the builder with most of the fields you want first:

public TypeSpec aNiceMethod(
     ClassModel classe, TypeName father, ArrayList methods, ArrayList fields) {

   TypeSpec.Builder theClassBuilder = TypeSpec.classBuilder(classe.getName())
       .addModifiers(
           javax.lang.model.element.Modifier.valueOf(
               classe.getProte().toString().toUpperCase()),
               Modifier.FINAL)
       .addMethods(methods)
       .addFields(fields);
   if (father != null) {
        theClassBuilder.superclass(father);
   }
   return theClassBuilder.build();
}
muzzlator
  • 732
  • 4
  • 9
  • Pretty sure that wouldn't really work as you wrote it, yet the "inline if" you wrote as a comment was pretty useful. Thanks! .superclass(father==null?ClassName.OBJECT:father) –  Mar 27 '17 at 21:23
  • Glad the comment helped. I'm confused though as to why you think this answer wouldn't work? I assume you're aware I switched `theClass` with `theClassBuilder` so you'd need to declare that variable, and I also used `return` to suggest that that's the object you want to end with – muzzlator Mar 27 '17 at 23:03
  • Yeah yeah, I just couldn't make it work because I was using the wrong Builder object. I proposed an edit, containing a Whole method instead of the previous code, thank you very much –  Mar 28 '17 at 08:21
  • Cool, glad you got it working. Thanks for the edit, I removed some line wrapping too – muzzlator Mar 28 '17 at 08:23
1

Try this:

if (father == null) {
  father = ClassName.OBJECT;
}

JavaPoet will do the right thing.

Jesse Wilson
  • 33,489
  • 5
  • 98
  • 112
  • .superclass(father==null?ClassName.OBJECT:father) this is probably the best way, thank you very much! –  Mar 27 '17 at 21:22