I'm trying to create a class (MoMoTest.class) which extends a generic abstract class (MappingObject.class). Everything looks good, except that JCodeModel doesn't import the narrowed class (MoTest.class), although I created a JClass of it with codeModel.ref:
MappingObject.class:
package test;
public abstract class MappingObject<T> {
protected T dataObject;
public MappingObject( T dataObject ) {
this.dataObject = dataObject;
}
public abstract T getDataObject();
public abstract String getStandardFormat();
}
MoTest.class:
package test;
public class MoTest {
}
MappingObjectCreator.class:
package test;
import com.sun.codemodel.*;
import java.io.File;
import java.io.IOException;
public class MappingObjectCreator {
public JDefinedClass getMappingObject(JCodeModel codeModel, JPackage jPackage, Class<?> clazz) throws JClassAlreadyExistsException {
JClass ref = codeModel.ref(clazz); // Not imported in MoMoTest.class
JDefinedClass definedClass = jPackage._class("Mo" + ref.name());
JClass superClass = codeModel.ref(MappingObject.class).narrow(ref);
definedClass._extends(superClass);
JFieldRef dataObject = JExpr.ref("dataObject");
JMethod constructor = definedClass.constructor(JMod.PUBLIC);
JVar param = constructor.param(ref, ref.name());
constructor.body().invoke("super").arg(param);
JMethod getDataObject = definedClass.method(JMod.PUBLIC, ref, "getDataObject");
getDataObject.annotate(codeModel.ref(Override.class));
getDataObject.body()._return(dataObject);
JMethod getStandardFormat = definedClass.method(JMod.PUBLIC, String.class, "getStandardFormat");
getStandardFormat.annotate(codeModel.ref(Override.class));
getStandardFormat.body()._return(dataObject.invoke("toString"));
return definedClass;
}
public void getMappingObject(Class clazz, String path) throws JClassAlreadyExistsException, IOException {
JCodeModel codeModel = new JCodeModel();
JPackage jPackage = codeModel._package(clazz.getPackage().getName());
getMappingObject(codeModel, jPackage, clazz);
codeModel.build(new File(path));
}
public static void main(String[] args) throws IOException, JClassAlreadyExistsException {
new MappingObjectCreator().getMappingObject(MoTest.class, "src/main/java");
}
}
Result (MoMoTest.class):
package test;
public class MoMoTest
extends MappingObject<test.MoTest>
{
public MoMoTest(test.MoTest MoTest) {
super(MoTest);
}
#Override
public test.MoTest getDataObject() {
return dataObject;
}
#Override
public String getStandardFormat() {
return dataObject.toString();
}
}
So why is Motest.class not imported in MomoTest.class ?
After modifying the parametername of the constructor of the generated class (MoMoTest.class) to first-letter-lower-case it somehow works:
MappingObjectCreator.class:
JMethod constructor = definedClass.constructor(JMod.PUBLIC);
char[] refName = ref.name().toCharArray();
refName[0] = Character.toLowerCase(refName[0]);
JVar param = constructor.param(ref, new String(refName));
constructor.body().invoke("super").arg(param);
Result:
package test;
public class MoMoTest
extends MappingObject<MoTest>
{
public MoMoTest(MoTest moTest) {
super(moTest);
}
#Override
public MoTest getDataObject() {
return dataObject;
}
#Override
public String getStandardFormat() {
return dataObject.toString();
}
}
Related
I like to have a Drive class where all files and folders for a project are managed.
My first attempt was pretty easy like a lot of functions (most of them with arguments).
Now I try to make it more fancy because it became more and more annoying to have a lot of functions, in which the desired one can be found. To not have an XY-problem here, I start with my dream.
I like to construct the Drive class in a way, so that it is super easy to find a certain file or folder.
If you look in the main function, I can find every needed file by writing a point and look which subclasses/methods are proposed to continue, till I find it and add .str to it. At every point, only the subclasses/methods will be proposed which makes sense at this point.
It almost works! It is more complicated to write and maintain as the first approach, but If I use it very often, it could be worth it.
I can:
go into subfolders
go into subfolders with name inside the argument
But there is an error if I define a fixed-name-subfolder of a fluid-name-folder like in the code below.
Now my questions:
how can I change the code so the main Function doesn't show this error?
would you recommend a completely different approach to the "make it easy to find strings inside a huge list of strings via making collections inside collections... of strings"-problem?
package utilities;
public class Drive_draft {
private static final String fs = System.getProperty("file.separator");
public static final String str = System.getProperty("user.home").concat(fs);
public static class IeCreation {
public static final String str = Drive_draft.str.concat(".meetings").concat(fs);
public static class Abstract {
public static final String str = IeCreation.str.concat("Abstracts").concat(fs);
}
public static class Meeting {
public static final String str = IeCreation.str.concat("Ueberordnungen").concat(fs);
}
}
public static class MetsSIPs {
public static final String str = Drive_draft.str.concat("workspace").concat(fs).concat("metsSIPs").concat(fs);
public static class preSIPs {
public static final String str = MetsSIPs.str.concat("preSIPs").concat(fs);
}
public static class RosettaInstance {
private static class MaterialflowId {
public static String str;
private static class ProducerId {
public static String str;
private static class Abstract {
public static String str;
public static class Mets {
public static final String str = Abstract.str.concat("content").concat(fs).concat("ie1.xml");
}
}
private static class Meeting {
public static String str;
}
public static Abstract Abstract (String value) {
Abstract ret = new Abstract();
ProducerId.Abstract.str = str.concat(value).concat(fs);
return ret;
}
public static Meeting Meeting (String value) {
Meeting ret = new Meeting();
ProducerId.Meeting.str = str.concat(value).concat(fs);
return ret;
}
}
public static ProducerId ProducerId (String value) {
ProducerId ret = new ProducerId();
MaterialflowId.ProducerId.str = str.concat(value).concat(fs);
return ret;
}
}
public static MaterialflowId MaterialflowId (String value) {
MaterialflowId ret = new MaterialflowId();
MaterialflowId.str = str.concat(value).concat(fs);
return ret;
}
}
public static class Dev extends RosettaInstance {
public static final String str = MetsSIPs.str.concat("dev").concat(fs);
}
public static class Test extends RosettaInstance {
public static final String str = MetsSIPs.str.concat("test").concat(fs);
}
public static class Prod extends RosettaInstance{
public static final String str = MetsSIPs.str.concat("prod").concat(fs);
}
}
#SuppressWarnings("static-access")
public static void main(String[] args) {
System.out.println(Drive_draft.MetsSIPs.Dev.str);
System.out.println(Drive_draft.MetsSIPs.Dev.MaterialflowId("1").str);
System.out.println(Drive_draft.MetsSIPs.Dev.MaterialflowId("2").str);
System.out.println(Drive_draft.MetsSIPs.Dev.MaterialflowId("1").ProducerId("t").str);
System.out.println(Drive_draft.MetsSIPs.Dev.MaterialflowId("1").ProducerId("t").Abstract("est").str);
System.out.println(Drive_draft.MetsSIPs.Dev.MaterialflowId("1").ProducerId("t").Meeting("oast").str);
System.out.println(Drive_draft.MetsSIPs.Dev.MaterialflowId("1").ProducerId("t").Abstract("est").Mets.str); //Error: Mets cannot be resolved or is not a field
}
}
You can encode your "directory" structure with interfaces, with each interface declaring what the user can do next. Then the implementation can use a StringBuilder to just append the appropriate snippets and keep returning this.
// PathBuilderInterfaces.java
public class PathBuilderInterfaces {
public interface Buildable {
String build();
}
public interface Drive extends Buildable {
IeCreation ieCreation();
MetsSIPs metsSIPs();
}
public interface IeCreation extends Buildable {
String ieCreationAbstract();
String meeting();
}
public interface MetsSIPs extends Buildable {
RosettaInstance dev();
RosettaInstance test();
RosettaInstance prod();
}
public interface RosettaInstance extends Buildable {
MaterialFlowId materialFlowId(String value);
}
public interface MaterialFlowId extends Buildable {
ProducerId producerId(String value);
}
public interface ProducerId extends Buildable {
Abstract producerIdAbstract(String value);
String meeting(String value);
}
public interface Abstract extends Buildable {
String mets();
}
}
// PathBuilder.java
import static com.example.somepackage.PathBuilderInterfaces.*;
public class PathBuilder implements Drive, IeCreation, MetsSIPs, RosettaInstance, MaterialFlowId, ProducerId, Abstract{
private StringBuilder builder = new StringBuilder(str);
private static final String fs = System.getProperty("file.separator");
public static final String str = System.getProperty("user.home").concat(fs);
public static Drive drive() {
return new PathBuilder();
}
#Override
public String build() {
return builder.toString();
}
#Override
public IeCreation ieCreation() {
builder.append(".meetings").append(fs);
return this;
}
#Override
public MetsSIPs metsSIPs() {
builder.append("workspace").append(fs).append("metsSIPs").append(fs);
return this;
}
#Override
public RosettaInstance dev() {
builder.append("dev").append(fs);
return this;
}
#Override
public RosettaInstance test() {
builder.append("test").append(fs);
return this;
}
#Override
public RosettaInstance prod() {
builder.append("prod").append(fs);
return this;
}
#Override
public MaterialFlowId materialFlowId(String value) {
builder.append(value).append(fs);
return this;
}
#Override
public ProducerId producerId(String value) {
builder.append(value).append(fs);
return this;
}
#Override
public Abstract producerIdAbstract(String value) {
builder.append(value).append(fs);
return this;
}
#Override
public String meeting(String value) {
builder.append(value).append(fs);
return build();
}
#Override
public String mets() {
builder.append("content").append(fs).append("ie1.xml");
return build();
}
#Override
public String ieCreationAbstract() {
builder.append("Abstracts").append(fs);
return build();
}
#Override
public String meeting() {
builder.append("Ueberordnungen").append(fs);
return build();
}
}
Usage:
// in a main method somewhere
System.out.println(
PathBuilder.drive()
.metsSIPs()
.dev()
.materialFlowId("1")
.producerId("t")
.producerIdAbstract("est")
.mets());
I am trying to create a Helper. My application can have many libraries, once instanced, I want to create a factory to be able to share the instance across classes, ie:
public ArrayList<Helper> helper = new ArrayList<>(asList(
new Helper(SomeLib.class, new SomeLib()),
new Helper(SomeOtherLib.class, new SomeOtherLib())
));
My Library class is standard as of the moment to test this working.
class Library {
public Library() { System.out.println("Instance is working"); }
}
An example Library I am trying to get to work looks like this:
public class ExampleLib extends Library {
public void test() { System.out.println("Test OK"); }
}
My current Helper class looks like this, however, I cannot cast the Library back to the original inherited class, I have tried multiple things:
import dreambot.libs.Library;
public class Helper {
private Library lib;
private Class<? extends Library> name;
public Helper(Class<? extends Library> name, Library lib) {
this.name = name;
this.lib = lib;
}
public Class<? extends Library> getName() { return name; }
public <Library> Library getLib() {
// All the things I've tried to do
return (this.name) lib;
return name.cast(lib);
return lib.getClass().cast(lib);
}
}
In turn, what I want is:
public ArrayList<Helper> helper = new ArrayList<>(asList(
new Helper(ExampleLib.class, new ExampleLib()),
));
public void test() {
Arrays.stream(helper.toArray()).filter(c -> c.getName(ExampleLib.class)).getFirst().ifPresent(h -> {
h.getLib().test(); // Should output "Test OK"
});
The errors I am receiving in my IDE in the Helper::getLib method are:
Not a statement for return (this.name) lib;
Incompatible Types Required: Library, Found: dreambot.libs.Library for
return lib.getClass().cast(lib); and return name.cast(lib);
Any help would be appreciated.
Try:
import dreambot.libs.Library;
public class Helper<T extends Library> {
private T lib;
private Class<T> name;
public Helper(Class<T> name, Library lib) {
this.name = name;
this.lib = lib;
}
public Class<T> getName() { return name; }
public T getLib() {
return lib;
}
}
or even simipler:
import dreambot.libs.Library;
public class Helper<T extends Library> {
private T lib;
public Helper(Library lib) {
this.lib = lib;
}
public Class<T> getName() { return lib.getClass(); }
public T getLib() {
return lib;
}
}
and don't forget about diamond operator during calling constructor:
public ArrayList<Helper> helper = new ArrayList<>(asList(
new Helper<>(SomeLib.class, new SomeLib()),
new Helper<>(SomeOtherLib.class, new SomeOtherLib())
));
which may be simplified to:
public ArrayList<Helper> helper = new ArrayList<>(asList(
new Helper<>(new SomeLib()),
new Helper<>(new SomeOtherLib())
));
The problem I am trying to solve is described in OO: dependent class needs information from grandparent class
import java.util.function.Supplier;
public class SupplyMe {
private Supplier<String> supplied;
public SupplyMe(Supplier<String> supplied) {
this.supplied = supplied;
}
public void printSupplied() {
System.out.println(supplied.get());
}
public static void main(String[] args) {
String changingStr = "hi";
Supplier<String> helloStrSupplier = () -> changingStr;
SupplyMe mySupplyMe = new SupplyMe(helloStrSupplier);
mySupplyMe.printSupplied();
changingStr = "hi2";
mySupplyMe.printSupplied();
}
}
the following code is throwing
SupplyMe.java:18: error: local variables referenced from a lambda expression must be final or effectively final
Supplier<String> helloStrSupplier = () -> changingStr;
What can I do to make Supplier be able to supple the value at runtime?
i was able to get it work by doing this
import java.util.function.Supplier;
public class SupplyMe {
public static String changingStr = "";
private Supplier<String> supplied;
public SupplyMe(Supplier<String> supplied) {
this.supplied = supplied;
}
public void printSupplied() {
System.out.println(supplied.get());
}
public static void main(String[] args) {
changingStr = "hi";
//Supplier<String> helloStrSupplier = changingStr;
Supplier<String> helloStrSupplier = new DynamicSupplier();
SupplyMe mySupplyMe = new SupplyMe(helloStrSupplier);
mySupplyMe.printSupplied();
changingStr = "hi2";
mySupplyMe.printSupplied();
}
static class DynamicSupplier implements Supplier<String> {
#Override
public String get() {
return changingStr;
}
}
}
I have a problem while deserializing an XML file.
My file is like:
<mission>
<branch>
<alternative uid="0" type="ALT_MONITOR"/>
<alternative uid="1" type="ALT_IF" condition="i==10"/>
</branch>
</mission>
I have a class called Alternative:
public abtract class Alternative {
#XStreamAsAttribute
public int uid;
#XStreamAsAttribute
public String type;
}
This class is extended by two other class:
#XStreamAlias("alternative")
public class AlternativeA extends Alternative {
}
#XStreamAlias("alternative")
public class AlternativeB extends Alternative {
#XStreamAsAttribute
public String condition;
}
And then i have an xStream converter :
public class AlternativeConverter extends ReflectionConverter {
public AlternativesConverter(Mapper mapper, ReflectionProvider reflectionProvider) {
super(mapper, reflectionProvider);
}
#Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
if (reader.getAttribute("condition") != null) {
AlternativeA alternativeA = new AlternativeA();
alternativeA.setUid(Integer.parseInt(reader.getAttribute("uid")));
alternativeA.setCondition(reader.getAttribute("condition"));
return super.doUnmarshal(alternativeA, reader, context);
}else {
AlternativeB alternativeB = new AlternativeB();
alternativeB.setUid(Integer.parseInt(reader.getAttribute("uid")));
return super.doUnmarshal(alternativeB, reader, context);
}
}
#SuppressWarnings("unchecked")
#Override
public boolean canConvert(Class clazz) {
return Alternative.class.isAssignableFrom(clazz);
}
}
But when i try to convert the xml to an object. When it reaches the alternative with a condition it throws an exception :
Cannot convert type AlternativeB to type AlternativeA
Do any of you have an idea or an int on what could cause that error ?
Thank you in advance.
Java:
package de.mosst.spielwiese;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.reflection.ReflectionConverter;
import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.mapper.Mapper;
import lombok.Data;
public class XStreamMultiClassesTest {
#Test
public void smokeTest() {
InputStream file = XStreamMultiClassesTest.class.getResourceAsStream("XStreamMultiClassesTest.xml");
XStream xStream = new XStream();
xStream.ignoreUnknownElements();
xStream.processAnnotations(Mission.class);
xStream.processAnnotations(Alternative.class);
Converter converter = new AlternativeConverter(xStream.getMapper(), xStream.getReflectionProvider());
xStream.registerConverter(converter);
Mission mission = (Mission) xStream.fromXML(file);
System.out.println(mission);
mission.branch.forEach(a -> {
System.out.println(a.getClass());
if (a instanceof AlternativeA) {
System.out.println("- condition: " + ((AlternativeA) a).condition);
}
});
}
public class AlternativeConverter extends ReflectionConverter {
public AlternativeConverter(Mapper mapper, ReflectionProvider reflectionProvider) {
super(mapper, reflectionProvider);
}
#Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
Alternative alternative = null;
if (reader.getAttribute("condition") != null) {
alternative = new AlternativeA();
((AlternativeA) alternative).condition = reader.getAttribute("condition");
} else {
alternative = new AlternativeB();
}
alternative.uid = Integer.parseInt(reader.getAttribute("uid"));
return super.doUnmarshal(alternative, reader, context);
}
#Override
public boolean canConvert(#SuppressWarnings("rawtypes") Class clazz) {
return Alternative.class.isAssignableFrom(clazz);
}
}
#XStreamAlias("mission")
#Data
class Mission {
public List<Alternative> branch = new ArrayList<>();
}
#XStreamAlias("alternative")
#Data
abstract class Alternative {
#XStreamAsAttribute
public int uid;
#XStreamAsAttribute
public String type;
}
class AlternativeA extends Alternative {
public String condition;
}
class AlternativeB extends Alternative {
}
}
XML:
<?xml version="1.0" encoding="UTF-8"?>
<mission>
<branch>
<alternative uid="0" type="ALT_MONITOR" />
<alternative uid="1" type="ALT_IF" condition="i==10" />
</branch>
</mission>
I am getting the error:
Prints the ASM code to generate the given class.
Usage: ASMifier [-debug]
From what I gather this is because I do not have the main method declared but this is clearly defined in one of my classes: Could this be a build error?
Main Class
package com.exacttarget.client;
public class Main {
public static void main(String[] args) {
System.out.println("Test");
}
}
PropertiesUtil Class
package com.exacttarget.client;
import java.util.Properties;
public class PropertiesUtil {
private static PropertiesUtil _instance = new PropertiesUtil();
Properties properties;
public void init() {
try {
if (properties == null || properties.isEmpty()) {
java.io.InputStream inConfig = PropertiesUtil.class.getClassLoader().getResourceAsStream("props.xml");
properties.loadFromXML(inConfig);
//properties.load(inConfig);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
private PropertiesUtil() {
super();
properties = new Properties();
init();
}
public static PropertiesUtil getInstance() {
return _instance;
}
public String getProperty(String key) {
return properties.getProperty(key);
}
}
PWCBHandler Class
package com.exacttarget.client;
import java.io.IOException;
import javax.security.auth.callback.*;
import org.apache.ws.security.WSPasswordCallback;
public class PWCBHandler implements CallbackHandler {
public PWCBHandler() {
}
#Override
public void handle(Callback callbacks[]) throws IOException, UnsupportedCallbackException {
for(int i = 0; i < callbacks.length; i++)
if(callbacks[i] instanceof WSPasswordCallback) {
WSPasswordCallback pc = (WSPasswordCallback)callbacks[i];
pc.setIdentifier(PropertiesUtil.getInstance().getProperty("username"));
pc.setPassword(PropertiesUtil.getInstance().getProperty("password"));
//pc.setPassword("welcome#1");
} else {
throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
}
}
}