I have classes called ctdl_User, ctdl_Device and ctdl_Options.
I have a function that saves ctdl_User objects using the binary formatter, and another that loads them. However the functions specifically expect to take and return User objects, and I want to use this function to load other objects of mine.
How do I go about changing what types the functions will take? Here is the save and load functions...
public ctdl_User Load()
{
ctdl_User loadedUsr = new ctdl_User();
string DataFileSave = Settings.Default.savePath + "\\testuserfile.dat";
FileStream dataStr = new FileStream(DataFileSave, FileMode.Open);
BinaryFormatter frmtr = new BinaryFormatter();
loadedUsr = (ctdl_User) frmtr.Deserialize(dataStr);
dataStr.Close();
return loadedUsr;
}
public static void Save(ctdl_User usr)
{
string DataFileSave = Settings.Default.savePath + "\\testuserfile.dat";
File.Delete(DataFileSave);
FileStream dataStr = new FileStream(DataFileSave, FileMode.Create);
BinaryFormatter frmtr = new BinaryFormatter();
frmtr.Serialize(dataStr, usr);
dataStr.Close();
}
The following demonstrates an approach using generics in C#:
public static T Load<T>() where T : new()
{
T loadedUsr = new T();
string DataFileSave = Settings.Default.savePath + "\\testuserfile.dat";
FileStream dataStr = new FileStream(DataFileSave, FileMode.Open);
BinaryFormatter frmtr = new BinaryFormatter();
loadedUsr = (T) frmtr.Deserialize(dataStr);
dataStr.Close();
return loadedUsr;
}
public static void Save<T>(T usr)
{
string DataFileSave = Settings.Default.savePath + "\\testuserfile.dat";
File.Delete(DataFileSave);
FileStream dataStr = new FileStream(DataFileSave, FileMode.Create);
BinaryFormatter frmtr = new BinaryFormatter();
frmtr.Serialize(dataStr, usr);
dataStr.Close();
}
Note the use of the new() constraint in the Load() method.
Here's an example of calling these methods:
public static void Main(string[] args)
{
ctdl_User user = new ctdl_User();
user.name = "Alice";
Save<ctdl_User>(user);
ctdl_User user2 = Load<ctdl_User>();
Console.WriteLine(user2.name);
ctdl_Device device = new ctdl_Device();
device.type = "printer";
Save<ctdl_Device>(device);
ctdl_Device device2 = Load<ctdl_Device>();
Console.WriteLine(device2.type);
}
For completeness, here are the stub classes I used to test this code:
[Serializable()]
class ctdl_User
{
public string name;
}
[Serializable()]
class ctdl_Device
{
public string type;
}
Edit: added code to Main() that saves and loads a device, as well as a user.
Assuming this is Java you can just overload the method following this scheme:
public static void Load(Type1 obj)
{
//do sth with object of type Type1
}
public static void Load(Type2 obj)
{
//do sth with object of type Type2
}
//...etc
Also if you have common code for it that can be used for objects of another types you can exrtact this code to one methode that treat overloaded methods as facade
private static void doLoad(Object obj)
{
//the common code
}
public static void Load(Type1 obj)
{
doLoad(obj); // or something else...
}
public static void Load(Type2 obj)
{
doLoad(obj); // or something else...
}
//...etc
You can read more about overloading methods in Java here
Related
I would like to use the MMDevice API from my Java app. What are my options?
I tried to use JNA. Looks like I can't use JNA Typelib parsing because there no types for this API (Is there a COM type library for Windows Core Audio). As suggested, I need to provide my own declarations of the API.
So I also tried both JNA examples with manual declarations but they give "Interface not supported HRESULT=80004002" error:
public class MMDeviceAPITest {
public static void test1() {
try {
Ole32.INSTANCE.CoInitializeEx(Pointer.NULL, Ole32.COINIT_MULTITHREADED);
var obj = new Test1.MMDeviceEnumerator(); // exception E_NOINTERFACE (HRESULT: 80004002)
// ...
} finally {
Ole32.INSTANCE.CoUninitialize();
}
}
public static void test2() {
try {
Ole32.INSTANCE.CoInitializeEx(Pointer.NULL, Ole32.COINIT_MULTITHREADED);
var factory = new Factory();
var obj = factory.createObject(Test2.MMDeviceEnumerator.class); // exception E_NOINTERFACE (HRESULT: 80004002)
var in = obj.queryInterface(Test2.IMMDeviceEnumerator.class);
// ...
} finally {
Ole32.INSTANCE.CoUninitialize();
}
}
}
interface Test1 {
class MMDeviceEnumerator extends COMLateBindingObject {
public MMDeviceEnumerator() {
super(new Guid.CLSID("bcde0395-e52f-467c-8e3d-c4579291692e"), true);
}
}
}
interface Test2 {
#ComObject(clsId = "bcde0395-e52f-467c-8e3d-c4579291692e")
interface MMDeviceEnumerator extends IUnknown {} // doesn't extend IUnknown in C sources, probably it's the problem...
#ComInterface(iid = "a95664d2-9614-4f35-a746-de8db63617e6")
interface IMMDeviceEnumerator extends IUnknown {}
}
Any ideas how I could access this API from Java? Can I somehow create working declarations for JNA? Or use another framework maybe?
My last idea is to create/find a micro native app/library that wraps the needed COM calls, so I could call this app/library easily (via subprocesses or simple JNA declarations). I'm new to COM world, but it sounds working for me...
The docs you linked show how to create using CoCreateInstance:
const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
hr = CoCreateInstance(
CLSID_MMDeviceEnumerator, NULL,
CLSCTX_ALL, IID_IMMDeviceEnumerator,
(void**)&pEnumerator);
This should get you somewhere close with JNA.
class MMDeviceEnumerator extends Unknown {
public static final CLSID CLSID_MMDeviceEnumerator = new CLSID("bcde0395-e52f-467c-8e3d-c4579291692e");
public static final GUID IID_IMMDeviceEnumerator = new GUID("a95664d2-9614-4f35-a746-de8db63617e6");
public MMDeviceEnumerator(Pointer p) {
super(p);
}
public static MMDeviceEnumerator create() {
PointerByReference pEnumerator = new PointerByReference();
HRESULT hres = Ole32.INSTANCE.CoCreateInstance(
CLSID_MMDeviceEnumerator, null,
WTypes.CLSCTX_ALL, IID_IMMDeviceEnumerator,
pEnumerator);
if (COMUtils.FAILED(hres)) {
return null;
}
return new MMDeviceEnumerator(pEnumerator.getValue());
}
// map functions as needed
}
I used the implementation of IWbemContext in JNA as a template above. You can consult that class for example COM function mappings.
For some reason I can't suggest edits to the answer of Daniel Widdis. The answer worked for me, many thanks! Just wanted to show how to map one method as an example:
class MMDeviceEnumerator extends Unknown {
public static final CLSID CLSID_MMDeviceEnumerator = new CLSID("bcde0395-e52f-467c-8e3d-c4579291692e");
public static final GUID IID_IMMDeviceEnumerator = new GUID("a95664d2-9614-4f35-a746-de8db63617e6");
public MMDeviceEnumerator(Pointer p) {
super(p);
}
public static MMDeviceEnumerator create() {
PointerByReference pEnumerator = new PointerByReference();
HRESULT hres = Ole32.INSTANCE.CoCreateInstance(
CLSID_MMDeviceEnumerator, null,
WTypes.CLSCTX_ALL, IID_IMMDeviceEnumerator, pEnumerator);
if (COMUtils.FAILED(hres)) {
return null;
}
return new MMDeviceEnumerator(pEnumerator.getValue());
}
public static final int EDataFlow_eRender = 0;
public static final int EDataFlow_eCapture = 1;
public static final int EDataFlow_eAll = 2;
public static final int EDataFlow_enum_count = 3;
public static final int DEVICE_STATE_ACTIVE = 0x1;
public static final int DEVICE_STATE_DISABLED = 0x2;
public static final int DEVICE_STATE_NOTPRESENT = 0x4;
public static final int DEVICE_STATE_UNPLUGGED = 0x8;
public static final int DEVICE_STATEMASK_ALL = 0xF;
public void EnumAudioEndpoints(int dataFlow, int dwStateMask, PointerByReference ppDevices) {
WinNT.HRESULT res = (WinNT.HRESULT) _invokeNativeObject(
3, // `EnumAudioEndpoints` is the 3rd method of `IMMDeviceEnumeratorVtbl` in `mmdeviceapi.h`
new Object[] { getPointer(), dataFlow, new WinDef.DWORD(dwStateMask), ppDevices},
WinNT.HRESULT.class
);
COMUtils.checkRC(res);
}
// map other functions as needed
}
Suppose I have classes Circle, Rectangle, and Triangle.
Based on input from a data file, I want to create the appropriate object. For instance, if the first line of shapes.dat is C.5 0 0, I will create a Circle object with radius 5. If the next line is R.5 3 0, I will create a Rectangle object with length 5 and width 3.
I know I could use basic if-else logic, but I was wondering whether there's a way to use the string as a means of instantiating a new object. Sort of like the exec() method in Python. Here's the code snippet describing what I want:
Scanner file = new Scanner (new File("shapes.dat"));
String s;
Map<Character,String> dict = new HashMap<Character,String>();
dict.put('C', "Circle");
dict.put('R', "Rectangle");
dict.put('T', "Triangle");
while (file.hasNextLine())
{
s = file.nextLine().trim();
String name = dict.get(s.toCharArray()[0]);
String data = s.split(".")[1];
String code = name + " x = new " + name + "(data);";
SYS.exec(code); //???
...
}
I'm not sure I understand correctly, seems weird no one else mentioned this yet:
Map<Character, ShapeFactory> dict = new HashMap<>();
dict.put('C', new CircleFactory());
dict.put('R', new RectangleFactory());
dict.put('T', new TriangleFactory());
...
ShapeFactory factory = dict.get(symbol);
Shape shape = factory.create(data);
You can make use of reflection to create instance dynamically.
String className = "com.shape.Triangle";
Class classDefinition = Class.forName(className);
Object obj = classDefinition.newInstance();
Or
Just use if-else to create instance of specific class.
Exec in Python executes code.
You can do the same thing with Java, for example with javassist.
You could read the data file, compile the statements and insert them in your own class.
But it seems overkill.
You could also use java reflection but it will produce a brittle and unclear code.
Instead if else if, I think that you should use abstraction and create a factory class by type of object.
It could look like :
Scanner file = new Scanner (new File("shapes.dat"));
String s;
Map<Character, ShapeBuilder> dict = new HashMap<Character,String>();
dict.put('C', new CircleBuilder());
dict.put('R', new RectangleBuilder());
dict.put('T', new TriangleBuilder());
while (file.hasNextLine()){
s = file.nextLine().trim();
char shapeSymbol = ...; // computed from s
ShapeBuilder builder = dict.get(shapeSymbol);
Shape shape = builder.build(s);
}
You can actually use polymorphism to avoid if-else statements. So, you can create objects that actually do those two jobs you want, match a line and create a shape. So you could use something like the following code.
public class Program {
public static void main() throws FileNotFoundException {
Scanner file = new Scanner(new File("shapes.dat"));
while (file.hasNextLine()) {
String line = file.nextLine().trim();
Shape shape = new Matches(
new RectangleMatch(),
new TriangleMatch(),
new SquareMatch(),
new CircleMatch()
).map(line);
}
}
public interface ShapeMatch {
boolean matches(String line);
Shape shape(String line);
}
public static final class RectangleMatch implements ShapeMatch {
#Override
public boolean matches(String line) {
return line.startsWith("R");
}
#Override
public Shape shape(String line) {
String[] dimensions = line.substring(2).split(" ");
return new Rectangle(
Integer.parseInt(dimensions[0]),
Integer.parseInt(dimensions[1]),
Integer.parseInt(dimensions[2])
);
}
}
public static final class CircleMatch implements ShapeMatch {
#Override
public boolean matches(String line) {
return line.startsWith("C");
}
#Override
public Shape shape(String line) {
return new Circle(Integer.parseInt(line.substring(2, line.indexOf(" "))));
}
}
public interface ShapeMapping {
Shape map(String line);
}
public static final class Matches implements ShapeMapping {
private final Iterable<ShapeMatch> matches;
public Matches(ShapeMatch... matches) {
this(Arrays.asList(matches));
}
public Matches(Iterable<ShapeMatch> matches) {
this.matches = matches;
}
#Override
public Shape map(String line) {
for (ShapeMatch match : matches) {
if (match.matches(line)) {
return match.shape(line);
}
}
throw new RuntimeException("Invalid shape entry line.");
}
}
}
My Goal: I need to keep the App state exactly in same sate after shutdown, lets say it's equivalent to "suspend" state.
My Problem : I do know that serialization mechanism doesn't save transient variables neither static variables. However I need to maintain the static variables in exactly same state after App suspension/shut down.
Approach-1 : I could save the state of static variable(s) into a different file, using my "file format", and serialize the objects into a different one.
a) Is this the "normal" approach?
Approach-2 : If I extend the ObjectInputStream/ObjectOutputStreamand override the methods readStreamHeader/writeStreamHeaderI can write whatever I want. So I can also write my static variables.
b) Am I doing something I should not?
Here's the code I've written testing approach-2, and seams to work fine. Please note, I'm not a Java programmer, so for it's very important to understand best practices, if there's any in this particular case.
#SuppressWarnings("serial")
class SequenceIdentifier implements Serializable
{
protected static long seqIdentifier_ = 1L; //This variable MUST NOT be reseted.
private long id_; //Object variable to be serialised.
private SequenceIdentifier(long id)
{ id_ = id;
}
#Override
public String toString()
{ return ("Id : " + id_ + " of " + seqIdentifier_);
}
public static SequenceIdentifier newInstance()
{ return new SequenceIdentifier(seqIdentifier_++);
}
}
final class OOStream extends ObjectOutputStream
{
public OOStream(OutputStream out) throws IOException
{ super(out);
}
#Override
protected void writeStreamHeader() throws IOException
{ super.writeLong(SequenceIdentifier.seqIdentifier_);
}
}
final class OIStream extends ObjectInputStream
{
public OIStream(InputStream in) throws IOException
{ super(in);
}
#Override
protected void readStreamHeader() throws IOException
{ SequenceIdentifier.seqIdentifier_ = super.readLong();
}
}
public class Main
{
public static void dump(ArrayList<SequenceIdentifier> ids)
{
for (SequenceIdentifier id : ids)
System.out.println(id);
}
public static void saveData()
{
ArrayList<SequenceIdentifier> ids = new ArrayList<>(Arrays.asList(SequenceIdentifier.newInstance(),
SequenceIdentifier.newInstance(),
SequenceIdentifier.newInstance(),
SequenceIdentifier.newInstance()));
try (OOStream oOut = new OOStream(new FileOutputStream("foo.bin")))
{ oOut.writeObject(ids);
} catch (Exception e)
{ System.err.println(e);
}
dump(ids);
}
#SuppressWarnings("unchecked")
public static void loadData()
{
ArrayList<SequenceIdentifier> ids = null;
try (OIStream oIn = new OIStream(new FileInputStream("foo.bin")))
{ ids = (ArrayList<SequenceIdentifier>)oIn.readObject();
} catch (Exception e)
{ System.err.println(e);
}
dump(ids);
}
public static void main(String[] args)
{
saveData();
System.out.println("Counter at this point " + SequenceIdentifier.seqIdentifier_);
SequenceIdentifier.seqIdentifier_ = 0;
loadData();
System.out.println("Counter at this point " + SequenceIdentifier.seqIdentifier_);
}
}
I would create a separate Memento-class containing all the relevant data as fields and de-/serialize that.
class MyClassWithStaticFields1 {
private static String field;
}
class MyClassWithStaticFields2 {
private static String field;
}
class StaticMemento {
String field1;
String field2;
}
// serialization
StaticMemento mem = new StaticMemento();
mem.field1 = MyClassWithStaticFields1.field;
mem.field2 = MyClassWithStaticFields2.field;
outputStream.writeObject(mem);
// deserialize
StaticMemento mem = outputStream.readObject();
MyClassWithStaticFields1.setField(mem.field1);
MyClassWithStaticFields2.setField(mem.field2);
So basically your Approach-1.
Several possibilities.
Make it non-static.
Write complementary readObect()/writeObject() methods that call defaultReadObject() and defaultWriteObject() respectively and then serialize/deserialize the field.
Write complementary writeReplace()/readResolve() methods that substitute a proxy object that does contain this member as a non-transient non-static member.
Make the object Externalizable and take complete control of the serialization process yourself in the associated methods.
Review your requirement.
When I try to execute this code, after I choose AMD, I got null in value. how it can be happen ?
below is the source code :
[for main]
public class processor{
public int hargapro;
public String nmbarangpro;
public static final Scanner input = new Scanner(System.in);
public String getpro()
{
return nmbarangpro;
}
public int getproharga()
{
return hargapro;
}
public void daftarpro() {
List<String> daftarpro = new ArrayList<>();
daftarpro.add("AMD");
daftarpro.add("Intel");
List<String> nomer = new ArrayList<>();
nomer.add("1. ");
nomer.add("2. ");
System.out.println("Processor yang tersedia :");
for (int i = 0; i < daftarpro.size(); i++) {
System.out.println(nomer.get(i)+daftarpro.get(i));
}
System.out.println("Pilihan anda : ");
int pilih = input.nextInt();
switch(pilih)
{
case 1:
{
System.out.println("Anda membeli Processor AMD");
System.out.println("Seharga Rp 1.200.000");
harga(1200000); //call harga method
namabarang("AMD"); //call namabarang method
System.out.println(getpro()); //[for testing]filled with AMD[ni problem here]
System.out.println(getproharga()); //[for testing][filled with 1200000[no problem here]
break;
}
case 2:
{
System.out.println("Anda membeli Processor AMD");
System.out.println("Seharga Rp 1.200.000");
harga(1500000);
namabarang("Intel");
break;
}
default:
System.out.println("Pilihan tidak tersedia");
daftarpro();
}
}
#Override
public int harga(int hargamasuk) {
return hargapro = hargamasuk;
}
#Override
public String namabarang(String barang) {
return nmbarangpro = barang;
}
public static void main(String[] args) {
processor a = new processor();
a.daftarpro();//get menu from daftarpro()
kasir x = new kasir();
x.semua();//get null in value
}
}
my second files :
public class kasir {
public void semua()
{
processor a = new processor();
System.out.println(a.getpro());
}}
When I try to read value through class kasir, i get x.semua filled with null value. how it can be happen ?
Your semua method creates a new instance of processor which it then reads from:
public void semua()
{
processor a = new processor();
System.out.println(a.getpro());
}
That's entirely unrelated to the processor instance you've created in your main method. If your kasir class should logically "know about" the other processor instance, you probably want a processor field in the class, which you might populate via the constructor - so your main method might become:
public static void main(String[] args) {
processor a = new processor();
a.daftarpro();
kasir x = new kasir(a);
x.semua();
}
As an aside, you should really try to follow the Java naming conventions, so classes of Processor and Kasir, and methods of getPro etc. (And if your code actually looks like that in your editor, I suggest you reformat it, too...)
I found some examples that show me the location of certain method calls using a MethodAdapter:
public void visitMethodInsn(int opcode, String owner, String name, String desc) {
if (owner.equals(targetClass)
&& name.equals(targetMethod.getName())
&& desc.equals(targetMethod.getDescriptor())) {
callsTarget = true;
}
}
I need the arguments, e.g., if I have object.getText("mykey") I would like to get the text "mykey".
Is this possible ?
I have a framework which you may find useful (specifically, procyon-compilertools). It would enable you to do what you ask in a more object-oriented manner than ASM. However, the project is still early in development and subject to change, so I would not recommend using it in a production project.
You could use this as a starting point:
public class CallInspectionSample {
static class Target {
public static void main(String[] args) {
new Target().getText("MyKey");
}
public String getText(final String key) {
return null;
}
}
public static void main(String[] args) {
final TypeReference targetType = MetadataSystem.instance().lookupType("CallInspectionSample$Target");
final TypeDefinition resolvedType = targetType.resolve();
final MethodDefinition mainMethod = resolvedType.getDeclaredMethods().get(1);
final MethodDefinition getTextMethod = resolvedType.getDeclaredMethods().get(2);
final MethodBody mainBody = mainMethod.getBody();
final Block methodAst = new Block();
final DecompilerContext context = new DecompilerContext();
context.setCurrentType(resolvedType);
context.setCurrentMethod(mainMethod);
methodAst.getBody().addAll(AstBuilder.build(mainBody, true, context));
AstOptimizer.optimize(context, methodAst);
for (final Expression e : methodAst.getChildrenAndSelfRecursive(Expression.class)) {
if (e.getCode() == AstCode.InvokeVirtual &&
((MethodReference) e.getOperand()).resolve() == getTextMethod) {
// Analyze arguments here (skip first for instance methods)...
System.out.println(e.getArguments());
}
}
}
}
(example outputs [initobject:Target(Target::<init>), ldc:String("MyKey")])