Auto-Value-Gson : Cannot resolve symbol GsonTypeAdapter - auto-value

I have so far implemented a Parcelable class with AutoValue and now I'd like to be able to deserialize it Gson.
So I have found the AutoValue Extension named auto-value-gson and I have tried to implement a class just like the examples on the github page.
So my class changed to :
#AutoValue
public abstract class UploadPhotoResponse implements Parcelable{
public abstract String getError();
public abstract UploadPhotoResponseNestedItem getPost();
public static Builder builder() {
return new AutoValue_UploadPhotoResponse.Builder();
}
#AutoValue.Builder
public static abstract class Builder {
public abstract Builder setError(String error);
public abstract Builder setPost(UploadPhotoResponseNestedItem post);
public abstract UploadPhotoResponse build();
}
// This is the new code I have added
public static TypeAdapter<UploadPhotoResponse> typeAdapter(Gson gson) {
return new AutoValue_UploadPhotoResponse.GsonTypeAdapter(gson);
}
}
but I'm getting error : "Cannot resolve symbol GsonTypeAdapter"
I'm using the following dependencies (just in case something is missing) :
provided 'com.google.auto.value:auto-value:1.3'
apt 'com.google.auto.value:auto-value:1.3'
apt 'com.ryanharter.auto.value:auto-value-parcel:0.2.5'
apt 'com.ryanharter.auto.value:auto-value-gson:0.4.5'
provided 'com.ryanharter.auto.value:auto-value-gson:0.4.5'
annotationProcessor 'com.gabrielittner.auto.value:auto-value-with:1.0.0'

Related

Class fixture type 'SetupFixture' had one or more unresolved constructor arguments [duplicate]

I am using XUnit and need to perform some action before running a test suit. so, I try to use IClassFixture feature of XUnit. but I cannot find a way to inject dependencies into the Fixture class. my code structure is such as below:
public class MyFixture
{
IDependency _dep;
public MyFixture(IDependency dep)
{
_dep = dep;
}
void DoSomeJob()
{
//// some code there
dep.DoSome();
}
}
And this is my test class code:
public class MyTest : IClassFixture<MyFixture>
{
[Fact]
public void test_my_code()
{
////simply just test the code
}
}
but when I run the test I am getting the exception
Xunit.Sdk.TestClassException Class fixture type 'MyFixture' had one or more unresolved constructor
Your Fixture class depends on IDependency dep, which has not been configured. You could use the Fixture class to setup a service provider; However it is not the best solution, as you have to end up using service locator patter such as
serviceProvider.GetRequiredService<T>()
Suggest to use xunit.di, it is an extension built into xunit framework to support constructor dependency injection, which allows us to achieve Inversion of Control (IoC) between test classes and their dependencies.
Install-Package Xunit.Di
To use xunit.di:
Install the xunit.di nuget package
Create a Setup.cs class to configure dependencies, (optional) and inherits the Xunit.Di.Setup.cs
Configure dependencies in the Setup.cs class.
Find full instructions and demos from xunit.di GET-STARTED
Your test project has the following:
Setup class that has a public IServiceProvider, which configures all the dependencies
Test class with constructor injecting the dependencies
Your Setup.cs class looks like below:
private IServiceProvider _services;
private bool _built = false;
private readonly IHostBuilder _defaultBuilder;
public Setup()
{
_defaultBuilder = Host.CreateDefaultBuilder();
}
public IServiceProvider Services => _services ?? Build();
private IServiceProvider Build()
{
if (_built)
throw new InvalidOperationException("Build can only be called once.");
_built = true;
_defaultBuilder.ConfigureServices((context, services) =>
{
services.AddSingleton<TextReaderService>();
services.AddSingleton<IDependency, DependencyImpl>();
// where DependencyImpl implements IDependency
// ... add other services needed
});
_services = _defaultBuilder.Build().Services;
return _services;
}
Then your test class looks like below:
public class MyTest
{
private readonly IDependency _dependency;
public MyTest(IDependency dependency)
{
_dependency = dependency;
}
[Fact]
public void test_my_code()
{
var result = _dependency.DoStuff();
Assert.NotNull(result);
////simply just test the code
}
}

JavaEE CDI in Weld: Generic Events?

I have an idea for a specific event handling based on generics, but seems like Weld can't handle them. I asked google but couldn't find an alternative CDI extension for this.
Question: is there a CDI extension, that can handle event propagation of generic-typed events?
In the following the explicit problem I have.
I have three general events, EntityCreated, EntityChanged and EntityDeleted. The base class for them is defined like this:
public abstract class DatabaseEvent<TargetType> {
public TargetType target;
public DatabaseEvent(TargetType target) {
this.target = target;
}
}
The events then are simple inherited classes:
public class EntityCreatedEvent<TargetType> extends DatabaseEvent<TargetType> {
public EntityCreatedEvent(TargetType target) {
super(target);
}
}
I fire them like this:
public abstract class MyHome<EntityType> {
private EntityType entity;
#Inject
Event<EntityCreatedEvent<EntityType>> entityCreatedEvent;
public void fireCreatedEvent() {
EntityCreatedEvent<EntityType> payload = new EntityCreatedEvent<EntityType>(entity);
entityCreatedEvent.fire(payload);
}
}
I want to observe them like this:
public void handleProjectCreated(#Observes EntityCreatedEvent<Project> event) { ... }
When launching the server Weld tells me it can't handle generic-typed events. The CDI-way of doing things would be to use additional qualifiers instead of the generics to distiguish them, e.g.:
public void handleProjectCreated(#Observes #ProjectEvent EntityCreatedEvent event) { ... }
However, I fire the events from that MyHome base class, where I can't just fire with the #ProjectEvent: it might not be a project but another type.
My solution up to now is to skip that typing altogether and handle them like this:
public void handleProjectCreated(#Observes EntityCreatedEvent event) {
if(event.target instanceof Project) { ... }
}
This solution is okay, but not perfect.
I guess you can do this with dinamically binding qualifier members. This is what your code would look like:
public abstract class MyHome {
private EntityType entity;
#Inject
Event<EntityCreatedEvent> entityCreatedEvent;
public void fireCreatedEvent() {
entityCreatedEvent.select(getTypeBinding()).fire(new EntityCreatedEvent(entity));
}
private TypeBinding getTypeBinding() {
return new TypeBinding() {
public Class<? extends EntityType> value() {return entity.getClass();}
};
}
#Qualifier
#Target({ PARAMETER, FIELD })
#Retention(RUNTIME)
public #interface EntityTypeQualifier {
Class<? extends EntityType> value();
}
public abstract class TypeBinding extends AnnotationLiteral<EntityTypeQualifier> implements EntityTypeQualifier {}
//Observers
public void handleEntityType1Created(#Observes #EntityTypeQualifier(EntityType1.class) EntityCreatedEvent event) {}
public void handleEntityType2Created(#Observes #EntityTypeQualifier(EntityType2.class) EntityCreatedEvent event) {}
As this CDI issue points it is not possible to fire an without having the type of T at runtime.
But, if you have the type of T (i.e. you have an instance) you can use the Event as an Instance, and select the event to be fired using a dynamic type literal.

How to create count query on MongoRepository

I am creating a MongoRepository and need to create a count query. Can someone provide an example of what is the best way to do this via the SpringData MongoDB MongoRepository facility? All the examples I was able to find reference returning a List but not counts.
Here is what I am trying to do (obviously it does not work):
public interface SchoolRepository extends MongoRepository<School, String> {
#Query("db.school.count({studentStatus: ?0});")
int getCountOfStudents(int studentStatus);
}
Thanks.
-AP_
I found this question as I was trying to do something similar. Unfortunately, given what I see in org.springframework.data.repository.query.parser.PartTree:
private static final Pattern PREFIX_TEMPLATE = Pattern.compile("^(find|read|get)(\\p{Upper}.*?)??By");
It does not appear to be supported.
Instead, we can add custom behaviour to the repository (see reference manual section 1.4.1) by creating a new interface and a class that implements it.
public interface SchoolRepository extends CrudRepository<School, String>, SchoolRepositoryCustom {
// find... read... get...
}
public interface SchoolRepositoryCustom {
int getCountOfStudents(int studentStatus);
}
#Service
public class SchoolRepositoryImpl implements SchoolRepositoryCustom {
#Autowired
private SchoolRepository schoolRepository;
public int getCountOfStudents(int studentStatus) {
// ...
}
}
Note that the class is named SchoolRepositoryImpl, not SchoolRepositoryCustomImpl.

NullPointerException in EnumMap when auto generating wadl with Jersey

I am using Tomcat 7, Jaxb2 and Jersey1.11.
I have a class EnumProperty which inherits from an abstract class Property.
#XmlAccessorType(XmlAccessType.FIELD)
public class EnumProperty extends Property<Enum> {
#XmlElement(name = "property_value", nillable = true)
private Enum value;
public EnumProperty() {
setValueType(PropertyValueTypeEnum.ENUM);
}
#Override
public Enum getValue() {
return value;
}
#Override
public void setValue(Enum value) {
this.value = value;
}
}
There are other sub classes for the Property class. In addition, I have another class, Entity, which holds a collection of properties. I have also a resource which returns in one of its sub resources a Collection. When I try to generate my application wadl I receive a NullPointerException. I isolated the problem to the EnumProperty class. Can anyone please help me understand where the problem is?
java.lang.NullPointerException
java.util.EnumMap.<init>(Unknown Source)
com.sun.xml.bind.v2.model.impl.RuntimeEnumLeafInfoImpl.<init>(RuntimeEnumLeafInfoImpl.java:87)
com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.createEnumLeafInfo(RuntimeModelBuilder.java:109)
com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.createEnumLeafInfo(RuntimeModelBuilder.java:85)
com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:228)
com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo(RuntimeModelBuilder.java:104)
com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo(RuntimeModelBuilder.java:85)
com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:213)
com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo(RuntimeModelBuilder.java:99)
com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo(RuntimeModelBuilder.java:85)
com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:319)
com.sun.xml.bind.v2.model.impl.TypeRefImpl.calcRef(TypeRefImpl.java:96)
com.sun.xml.bind.v2.model.impl.TypeRefImpl.getTarget(TypeRefImpl.java:73)
com.sun.xml.bind.v2.model.impl.RuntimeTypeRefImpl.getTarget(RuntimeTypeRefImpl.java:62)
com.sun.xml.bind.v2.model.impl.RuntimeTypeRefImpl.getTarget(RuntimeTypeRefImpl.java:55)
com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl$1.get(ElementPropertyInfoImpl.java:78)
com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl$1.get(ElementPropertyInfoImpl.java:81)
java.util.AbstractList$Itr.next(Unknown Source)...

Dependencies waiting to be satisifed

I'm trying to abstract some simple tasks for some very simple objects.
In my domain model, there are a number of different objects which basically serve as a way to tag (classify) a "Program." The Business Logic says a program can have as many of these as its wants, but no tags of the same type (e.g., "County") can have the same name, and you can't delete a tag while it has programs linked to it.
This is built on MVC3 with S#arp 2.0.
The domain model has an abstract base class NamedEntity : Entity which defines
public string Name { get; set; }
among other properties.
Specific types extend this class to add whatever makes them unique (if anything), such as Topic, which is a heirarchical structure and so has additional properties for that.
I wanted to create INamedEntityTasks<T> where T: NamedEntity and then have a base version of this for handling routine tasks like bool CheckForDuplicateName(string Name) which would run access its INamedEntityQueries<T> object and call T FindByName(string Name)
If a subclass needed to add more rules prior to delete (e.g. a topic with children can't be deleted), then it just overrides the virtual method from the base class.
Structure:
MyProject.Infrastructure has INamedEntityQueries<T> and NamedEntityQueries<T> as well as ITopicQueries : INamedEntityQueries<Topic> and TopicQueries: NamedEntityQueries<T>, ITopicQueries
MyProject.Domain.Contracts.Tasks has INamedEntityTasks<T> and ITopicTasks : INamedEntityTasks<Topic>
MyProject.Tasks has NamedEntityTasks<T> and TopicTasks: NamedEntityTasks<Topic>, ITopicTasks
My TopicsController won't run because of a missing dependency that I can't figure out.
The exact exception is
Can't create component
'MyProject.web.mvc.controllers.topicscontroller'
as it has dependencies to be
satisfied.
MyProject.web.mvc.controllers.topicscontroller
is waiting for the following
dependencies:
Services:
- MyProject.Infrastructure.Queries.ITopicQueries
which was not registered.
- MyProject.Domain.Contracts.Tasks.ITopicTasks
which was registered but is also
waiting for dependencies.
MyProject.Tasks.TopicTasks is waiting
for the following dependencies:
Services:
- MyProject.Infrastructure.Queries.INamedEntityQueries`1[[MyProject.Domain.Topic,
MyProject.Domain, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null]]
which was not registered.
I checked the container in ComponentRegistrar with a breakpoint and it shows 3 potentially misconfigured:
"MyProject.Tasks.NamedEntityTasks`1" NamedEntityTasks`1
"MyProject.Tasks.TopicTasks" ITopicTasks / TopicTasks
"MyProject.web.mvc.controllers.topicscontroller" TopicsController`TopicsController`
Any help would be appreciated.
You don't need the IFooTasks interface, just use an abstract base class, then IFooBarTasks and IFooBazTasks will be registered with Castle Windsor by the standard ComponentRegistrar in S#arp Architecture:
public abstract class Foo
{
public void Foo1();
public void Foo2();
}
public class FooBar : Foo
{
public void FooBar1();
}
public class FooBaz : Foo
{
public void FooBaz1();
}
public interface IFooTasks
{
void Foo1();
void Foo2();
}
public interface IFooBarTasks : IFooTasks
{
void FooBar1();
}
public interface IFooBazTasks : IFooTasks
{
void FooBaz1();
}
public abstract class FooTasks : IFooTasks
{
public void Foo1()
{
// Foo1 implementation
}
public void Foo2()
{
// Foo2 implementation
}
}
public class FooBarTasks : FooTasks, IFooBarTasks
{
public void FooBar1()
{
// FooBar1 implementation
}
}
public class FooBazTasks : FooTasks, IFooBazTasks
{
public void FooBaz1()
{
// FooBaz1 implementation
}
}

Resources