I'm porting an SDK from Android to plain Java and have run into an AutoParcel annotation that I don't understand.
Here's the original class and a snippet below:
#AutoParcel.Builder
public abstract static class Builder {
public abstract Builder id(String id);
...
public abstract SimpleFeature build();
}
public static Builder builder() {
return new AutoParcel_SimpleFeature.Builder();
}
I am able to pretty much port everything to AutoValue without incident, except that last function, as I don't understand what it is or it's equivalent in AutoValue.
Can someone explain what this is, and what its equivalent is in AutoValue?
The build annotation allows you to construct the immutable POJOs using the builder pattern i.e. something like
SimpleFeature.builder().id("test").build()
The equivalent annotation (not surprisingly since AutoParcel is a port of Autovalue with android specific features i.e. Parcelable)
#AutoValue.Builder
You should be able to find much more comprehensive documentation at https://github.com/google/auto/tree/master/value#builders
As JohnWowUs' comment suggests, this was largely an Eclipse issue.
The link he mentioned was only part of the solution, but I didn't need to drop more JARs into the project. With the help of an issue in the AutoValue repo and specifically configuring the maven-compiler-plugin, setting JDK1.7 as a target, with the following section added to the pom.xml:
<annotationProcessors>
<annotationProcessor>com.google.auto.value.processor.AutoValueProcessor</annotationProcessor>
</annotationProcessors>
Related
Let's have a simple MongoDB Document class with Lombok annotations
import lombok.Data;
import com.mongodb.lang.NonNull;
import org.springframework.data.mongodb.core.mapping.Document;
#Document
#Data
public class Car {
#NonNull
private String plate;
}
and simple test
#Test
public void test() {
Car c = new Car();
c.setPlate(null);
}
With Lombok v1.18.22 this worked well and test succeeded. However since v1.18.24, the test starts to fail since apparently Lombok now understands more annotations that indicate that field/param shouldn't be null
https://projectlombok.org/changelog
I'm not sure if MongoDB uses these #NonNull annotations for anything (I think not?) but at least for a documentation purposes it's nice to have them. Also the field can be null in some cases for MongoDB documents f.e. when using Spring Data's filtering using Example
repository.findByExample(Example.of(car))
where I might filter by only some specific field, leaving also mandatory fields to null.
If it'd be about #lombok.NonNull then I'd simply remove it but I don't want to remove Mongo annotations just because of Lombok. Thus my question:
Question 1: Can I avoid these checks? Or can I turn this feature off? So either that Lombok will consider only lombok.* annotations or just suppress generation of null checks in general.
Second issue is that Intellij Idea Lombok plugin apparently doesn't know about this feature yet so it considers that code to be valid. The build then fails as Lombok v1.18.24 generates constructor with all the #NonNull fields so it can't call new Car(). With #lombok.NonNull it works well in Intellij but with Mongo's NonNull it doesn't. I'm using IntelliJ IDEA 2022.1.3 (Community Edition)
Question 2: Is this a bug in Intellij Idea or its Lombok plugin? If yes is it known? I haven't found any info about it which leads me to the idea that the error is on my side
I need to add an annotation to a class which is in a sperate dependency (in a separate jar) of the project that I am working on. I am wondering that can I do this in Java?
As an example, I need to add an annotation HisClass without touching the source code of HisClass.
public class HisClass{
// ...
}
So, that the above class should be look like below at the build time,
#MyAnnot ( me = MyClass.class )
public class HisClass{
// ...
}
There are many ways:
Write a compiler plugin for javac, and make it add the annotations. This will be quite difficult, as the plugin API has nearly no documentation.
2.(Maybe not possible) Inject the annotation after compiling. Add an extra buildstep after compiling and use a library like ASM to add this annotation to the classfile.
I try to use the #NotNull annotation from package com.sun.istack.internal.
I am using IDE Intellij IDEA Community Edition.
when I build a program using IDE no problem. When I try to compile a file from the command line using javac, I get an error "cannot find symbol".
package ibkr;
import com.sun.istack.internal.NotNull;
public class Main {
public static void main(String[] args) {
test("Test");
}
public static void test(#NotNull String text) {
System.out.println(text);
}
}
I don't understand why i can't compile this code using javac and how Intellij IDEA make compilation and run it.
The annotation is an internal class, it's not for public use. The closest alternative is Jetbrains' stuff:
https://www.jetbrains.com/help/idea/nullable-and-notnull-annotations.html
Even if the class is in the classpath, it doesn't necessarily mean that you can safely refer to that class. The compiler can have some limitations upon accessing some classes/packages.
In most cases, as here, it's obvious whether the package is internal or not: com.sun.istack.internal. Oracle discourages developers from using classes from such packages.
Ok i know this question is a bit old, but if my info is correct, the reason for this is the fact that intellij uses full rt.jar for compilation while javac uses incomplete version, because of ct.sym
This is an annotation used to identify non-nullable values, also this will let static analyzer have their checks in place. In case you are using IntelliJ you can use its annotation but it would make it very tool-specific, same is the case for eclipse
One can also you
https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl
This provides the same non-null annotation but you need to add an extra dependency there.
still learning to master akka java with play framework. I have a code snippet below. It was working fine but has decided to give some headaches.
public class Application extends Controller {
static ActorRef masterActor;
RubineActor rubineactor;
public static Result index() {
return ok(index.render(null));
........ somecode
}
it was working fine but now my eclipse juno complains that it cannot resolve the index object in the return line . I am new to both akka and play framework . Can someone please explain what is happening to me. cos have to submit the project as my final year project. thanks
Your problem is not related to Akka, it's a template concern.
The variable index is provided by a template import, certainly import views.html.*;
Eclipse sometimes cannot resolve this object because it is generated automatically by Play after the first request.
Templates are compiled as standard Scala functions, following a simple naming convention. If you create a views/Application/index.scala.html template file, it will generate a views.html.Application.index class that has a render() method.
See the hello word sample for a concrete exemple.
I have a java project that is referenced in j2me project and in android project.
In this project i would like to use conditional compilation.
Something like...
//#if android
...
//#endif
//if j2me
...
//#endif
I have been reading about this but i did not find anything useful yet.
You could use Antenna (there is a plugin for Eclipse, and you can use it with the Ant build system).
I'm using it in my projects in a way you've described and it works perfectly :)
EDIT: here is the example related to #WhiteFang34 solution that is a way to go:
In your core project:
//base class Base.java
public abstract class Base {
public static Base getInstance()
{
//#ifdef ANDROID
return new AndroidBaseImpl();
//#elif J2ME
return new J2MEBaseImpl();
//#endif
}
public abstract void doSomething();
}
//Android specific implementation AndroidBaseImpl.java
//#ifdef ANDROID
public class AndroidBaseImpl extends Base {
public void doSomething() {
//Android code
}
}
//#endif
//J2ME specific implementation J2MEBaseImpl.java
//#ifdef J2ME
public class J2MEBaseImpl extends Base {
public void doSomething() {
// J2Me code
}
}
//#endif
In your project that uses the core project:
public class App {
public void something {
// Depends on the preprocessor symbol you used to build a project
Base.getInstance().doSomething();
}
}
Than if you want to build for the Android, you just define ANDROID preprocessor symbol or J2ME if you want to do a build for a J2ME platform...
Anyway, I hope it helps :)
Perhaps you should consider creating interfaces around the logic that's specific to a profile (J2ME, Android or other in the future). Then create concrete implementations of your interface for each profile. Any common parts you could split out into an abstract base class for both implementations to extend. This way your logic for each profile is nicely separated for different concerns. For each profile just build the appropriate set of classes (you could separate them by package for example). It'll be easier to maintain, debug, test and understand in the long run.
Eclipse MTJ project provides preprocessing support as documented . This support was mainly targeted for tackling fragmentation problems on JavaME. I have not tested the preprocessing support together with the Android tooling but it may just work.