I'm wondering if there is a way to properly generate models so that the generated models can reference each other?
I have a file structure like this
# Project Structure
/common
- Name.json
/v1
- User.json
/v2
- User.json
My Open API Specification files, both v1 and v2 reference common's Name properly
However the generated code ends up like this:
import v1.Name //created another Name class in v1 package
public class User {
Name name;
//...
}
import v2.Name //created another Name class in v2 package
public class User {
Name name;
//...
}
Ideally I would like to have the generated classes to look something like this
package v1
import common.Name
public class User {
Name name;
}
I have tried the import mappings and type mappings flags but the code generator cannot seem to find the common.Name class during creation of User class.
Any workarounds for this or is this a known limitation of the generator?
Edit: Ah it seems like impossible to have different generated packages in a single generation. So I must break these packages into their projects with their own types then utilize .openapi-ignore or looking at the jar depdendencies that's generated. Darn.
Related
I created a custom maven archetype that has a ClassName.java class that contains a property ${propertyName} with its getter and setter.
package ${package};
// Start of user code (import)
// End of user code (import)
/**
* Block ${BlockName}
*/
public class ${BlockName} extends Block<${BlockName}DTO> {
/**
* Item ${itemName}
*/
private final Item<${itemType}> ${itemName} = new Item.Control<${itemType}>(this, "${itemName}") {
};
// Getter and Setter
}
What I want to achieve when creating a new project from this custom archetype is giving multiple class names (for example: Car, Bicycle ...), and give also multiple properties for each class (for example: Car.door, Car.window, Bicycle.wheel ...), and have as output the classes created from the template ClassName.java as Car.java and Bicycle.java, but in each class have the ${itemName}, getters and setters replaced with the properties given.
You can probably code a groovy script for that and run it post-generation
Is there a way to post-process project generated from archetype?
On the other hand: Generating getters/setters is standard IDE functionality (e.g. easily done in Eclipse), so I am not sure whether this is really useful in an archetype.
I'm working with Google's Protocol Buffer (in combination with the Protocol Buffers maven plugin) which compiles a .proto file into a class. I can use the generated class in the default package perfectly, but not outside of it. I don't really know how to explain it any better so I'm going to show you some pictures.
I've tried subclassing the Hrp class but that doesn't work (the generated class is final). It is also not an option to move the class every time I re-generate the Hrp class.
I'm not sure if this is relevant, but the generated class is public final. It contains an empty, private constructor.
I have also tried setting the generated sources package prefix for the generated sources folder but that also does not work.
Any help would be greatly appreciated.
Try adding a package id to your Protocol Buffers definition. See Protocol Buffers Package
i.e.
syntax = "proto3";
package MyPackage;
option optimize_for = SPEED;
message Product {
repeated ASale sale = 1;
}
Then when you Generate the Java~Protocol~Buffers code (using protoc), it will be in package MyPackage and you will be able to import it into your java code in the normal way.
In java, you can not import anything from the Default package; which I believe is your problem. See How to access java-classes in the default-package?
I want to generate relationship graph among model classes using degraph.
Here is a representative example model, a POJO TaskEntryImpl that relates to Task:
package com.packag.tm.model.pojo;
public class TaskEntryImpl implements TaskEntry,Serializable {
private Integer id;
private Task task;
public Task getTask() {
return this.task;
}
public void setTask(Task v) {
this.task = v;
}
Packages containing models have model.pojo as part of the package name:
com.somepackage.events.model.pojo.DurationImpl
au.com.anotherpackage.ecrm.model.pojo.PayphoneImpl
How do I get a graph of models that meet the abovementioned characteristics?
For the curious: I wish I could have an Entity-Relation diagram instead.
These model classes are wired by Hibernate ORM. The original developers maintained SQL independent of the codebase and have never used foreign keys. So this rules out getting an entity-relation diagram from the database schema.
Create a file pojo.config with the following content
output = pojo.graphml
classpath = yourlib.jar
include = **.model.pojo.**
For getting started I'd put the file straight in the bin directory of Degraph.
The classpath value must point to the byte code you want to analyze, so either a jar-file or the directory, which contains all the class-files.
Now run (again from the bin directory):
degraph -f pojo.config
Degraph should print out the number of classes found and create a file pojo.graphml in the current directory (i.e. bin). You can open that with yed.
See the documentation of Degraph for how to create a useful layout with yed.
Note that Degraph will create boxes for the packages and put the boxes for classes into these package boxes. You can open the package boxes and then delete the package boxes, if you don't like them.
I have this problem in Android/Eclipse.
I´m working in Android Project , this one has to be configured in different ways for different customers, this configurations are not similar(very different methods) between them.
The code is shared in 90%, but each customer has his own requirements.
Each customer has a package with diferent and multiple classes.
I would like only include package of choiced customer in the final compiled file.
But i have conditional object creation in my shared code, and i have to join all package for compiling.
More or less is this (Example)
//import every customer packages
import com.project.customer_1
import com.project.customer_2
import com.project.customer_3
…..
import com.project.customer_98
import com.project.customer_99
……..
final static int customer=2; //Define the customer for this compilation
………
if (customer==1) { //unreachable code
customerClass1 customer1=new customerClass1();
customer1.method1_1;
customer1.method1_2;
customer1.method1_3;
}
else if (customer==2) {
customerClass2 customer2=new customerClass2();
customer2.method2_1;
customer2.method2_2;
customer2.method2_3;
}
…….//unreachable code
else if (customer==98) {//unreachable code
customerClass98 customer98=new customerClass98();
customer98.method98_1;
customer98.method98_2;
customer98.method98_3;
}
else if (customer==99{//unreachable code
customerClass99 customer99=new customerClass99();
customer99.method99_1;
customer99.method99_2;
customer99.method99_3;
}
............
Is clear when customer=X, the other choices are unreachable code. I would like exclude other customer packages/classes than customer is choiced in compiled file. But exist dependency in code
Note: For me is not valid comment code, neither reflection.
You shouldn't do it in way you've described.
You should create additional level of abstraction to access data/logic specific to customers. And move that customer specific logic/data to separate libraries/projects.
To work with separated customer projects, your abstraction level should use dynamic class loading.
Edit. If you can't refactor your project, you can create Proguard config files for each customer to remove unreachable code.
My application allows developers to create some plugins. Сompliance with the requirements determined by base abstract class. Each plugin must have a name. I want to solve this problem by using annotations. So, I defined my own annotation Name as follows:
#Retention(RetentionPolicy.RUNTIME)
public #interface Name {
public String value();
}
I put this annotaion and base abstract class CommonPlugin into separate module and build it as JAR-file to share with developers.
Then I import package and put this annotation before the defenition of me test plugin as follows:
#Name("Test plugin")
public class TestPlugin extends CommonPlugin {
Then I reflect all given plugins through URLClassLoader and can't find necessary annotation at TestPlugin class. But if I define Name annotation into the same package the TestPlugin class is, I can find it. It should be so or am I doing something wrong?
Turning my coment into an answer so it can be accepted.
Make sure that the unqualified name Name refers to the same qualified name in all of your source files. In sources from different packages than the one containing the annotation, there should be a non-wildcard import for Name, and there should be no class with that name in that other package itself.