Eclipse Object Info Box on Custom Objects - java

I couldn't find how to do this anywhere else online, though I'm sure it's really easy to do. I'm primarily self taught though, and I'd like to start learning to document my code properly. This "yellow box" that pops up in eclipse with information about the method - I want it to pop up on a custom object. For my example below I have a custom class called "System Properties" and a method called "getOs" but when I hover that option, no information comes up. How do I add information to my object?
This picture shows the yellow box
This picture shows the lack of a "yellow box" on my object
and then finally my custom objects code...
public class SystemProperties {
private String os;
public SystemProperties() {
this.os = setOs();
}
private String setOs() {
String osName = System.getProperty("os.name");
if(osName.toLowerCase().contains("window"))
return "Windows";
else if(osName.toLowerCase().contains("mac"))
return "Mac";
else
return "Linux";
}
/**
* Method to grab the OS the user is running from
* #return String - the os
*/
public String getOs() {
return this.os;
}
}
Thank you in advance for your time and knowledge. :)
EDIT:
When I import the project of the custom object, it works just fine. It only doesn't work when I export the project of the custom class to a jar file and then use that instead. Do I have to click an option on the export screen?

Eclipse take the info from the notes above the methods in the built in objects.
see this:
/**
* Returns <tt>true</tt> if this map contains a mapping for the specified
* key. More formally, returns <tt>true</tt> if and only if
* this map contains a mapping for a key <tt>k</tt> such that
* <tt>(key==null ? k==null : key.equals(k))</tt>. (There can be
* at most one such mapping.)
*
* #param key key whose presence in this map is to be tested
* #return <tt>true</tt> if this map contains a mapping for the specified
* key
* #throws ClassCastException if the key is of an inappropriate type for
* this map
* (optional)
* #throws NullPointerException if the specified key is null and this map
* does not permit null keys
* (optional)
*/
boolean containsKey(Object key);
You can do the same to the methods of your own objects.

Related

JNA with Libvirt c code. What did i do wrong?

I'm new to JNA and am trying to get a storage info with libvirt c and JNA.
There are two libvirt c functions, virConnectListAllStoragePools() and virStoragePoolGetInfo(), as shown below.
/**
* virConnectListAllStoragePools:
* #conn: Pointer to the hypervisor connection.
* #pools: Pointer to a variable to store the array containing storage pool
* objects or NULL if the list is not required (just returns number
* of pools).
* #flags: bitwise-OR of virConnectListAllStoragePoolsFlags.
*
* Collect the list of storage pools, and allocate an array to store those
* objects. This API solves the race inherent between
* virConnectListStoragePools and virConnectListDefinedStoragePools.
*
* Normally, all storage pools are returned; however, #flags can be used to
* filter the results for a smaller list of targeted pools. The valid
* flags are divided into groups, where each group contains bits that
* describe mutually exclusive attributes of a pool, and where all bits
* within a group describe all possible pools.
*
* The first group of #flags is VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE (online)
* and VIR_CONNECT_LIST_STORAGE_POOLS_INACTIVE (offline) to filter the pools
* by state.
*
* The second group of #flags is VIR_CONNECT_LIST_STORAGE_POOLS_PERSITENT
* (defined) and VIR_CONNECT_LIST_STORAGE_POOLS_TRANSIENT (running but not
* defined), to filter the pools by whether they have persistent config or not.
*
* The third group of #flags is VIR_CONNECT_LIST_STORAGE_POOLS_AUTOSTART
* and VIR_CONNECT_LIST_STORAGE_POOLS_NO_AUTOSTART, to filter the pools by
* whether they are marked as autostart or not.
*
* The last group of #flags is provided to filter the pools by the types,
* the flags include:
* VIR_CONNECT_LIST_STORAGE_POOLS_DIR
* VIR_CONNECT_LIST_STORAGE_POOLS_FS
* VIR_CONNECT_LIST_STORAGE_POOLS_NETFS
* VIR_CONNECT_LIST_STORAGE_POOLS_LOGICAL
* VIR_CONNECT_LIST_STORAGE_POOLS_DISK
* VIR_CONNECT_LIST_STORAGE_POOLS_ISCSI
* VIR_CONNECT_LIST_STORAGE_POOLS_SCSI
* VIR_CONNECT_LIST_STORAGE_POOLS_MPATH
* VIR_CONNECT_LIST_STORAGE_POOLS_RBD
* VIR_CONNECT_LIST_STORAGE_POOLS_SHEEPDOG
* VIR_CONNECT_LIST_STORAGE_POOLS_GLUSTER
* VIR_CONNECT_LIST_STORAGE_POOLS_ZFS
* VIR_CONNECT_LIST_STORAGE_POOLS_VSTORAGE
* VIR_CONNECT_LIST_STORAGE_POOLS_ISCSI_DIRECT
*
* Returns the number of storage pools found or -1 and sets #pools to
* NULL in case of error. On success, the array stored into #pools is
* guaranteed to have an extra allocated element set to NULL but not included
* in the return count, to make iteration easier. The caller is responsible
* for calling virStoragePoolFree() on each array element, then calling
* free() on #pools.
*/
int
virConnectListAllStoragePools(virConnectPtr conn,
virStoragePoolPtr **pools,
unsigned int flags)
{
//logic about set pool pointer to parameter '**pools' and return number of pools.
}
/**
* virStoragePoolGetInfo:
* #pool: pointer to storage pool
* #info: pointer at which to store info
*
* Get volatile information about the storage pool
* such as free space / usage summary
*
* Returns 0 on success, or -1 on failure.
*/
int
virStoragePoolGetInfo(virStoragePoolPtr pool,
virStoragePoolInfoPtr info)
{
// logic about set pool info to parameter 'info'.
}
My java code with JNA is:.
1) StoragePoolPointer.java
import com.sun.jna.PointerType;
public class StoragePoolPointer extends PointerType {}
2) virStoragePoolInfo
public class virStoragePoolInfo extends Struture {
public int state;
public long capacity;
public long allocation;
public long available;
private static final List<String> filedls = Arrays.asList("state", "capacity", "allocation", "available");
protected List<String> getFieldOrder() {
return fields;
}
}
3) Libvirt.java interface
public interface Libvirt extends Library {
int virConnectListAllStoragePools(ConnectionPoinet VCP, StoragePoolPointer[] pPointers, int flag);
int virStoragePoolGetInfo(StoragePoolPointer poolPointer, virStoragePoolInfo info);
}
4) jna test code
public void listAllStoragePools(ConnectionPointer VCP) {
Libvirt libvirt = (Libvirt) Native.loadLibrary("virt", Libvirt.class);
StoragePoolPointer[] pPointerArr = new StoragePoolPointer[10];
int result = livirt.virConnectListAllStoragePools(VCP, pPointerArr, 64);
// result is fine. i get storage pool count. and also i get StoragePoolPointer object inside array.
virStoragePoolInfo poolInfo = new virStoragePoolInfo();
libvirt.virStoragePoolGetInfo(pPointerArr[0], poolInfo);
// when i call libvirt.virStoragePoolGetInfo(). i get a error msg like this. 'libvirt: Storage Driver error : invalid storage pool pointer in virStoragePoolGetInfo.
//error msg : org.libvirt.LibvirtException: invalid storage pool pointer in virStoragePoolGetInfo.
}
I think i made wrong interface method virConnectListAllStoragePools() and I am passing the wrong parameter for 'virStoragePoolPtr **pools'.
Native arrays are laid out in contiguous memory. In this case, virStoragePoolPtr **pools is a single pointer to the beginning of the array; and C uses its knowledge of pointer size to use offsets to find the remaining elements.
JNA does not translate (most) Java arrays to this native layout. When it does so, it uses clearly defined sizes, such as for primitive arrays like int[] or for arrays of Structure that JNA can calculate the size for, and allocate with its toArray method.
You have defined a Java-side array here:
StoragePoolPointer[] pPointerArr = new StoragePoolPointer[10];
But you have not allocated any Java objects to fill the array with, so it's simply an array of nulls. Further, even if you did fill the array by iterating and setting pPointerArr[i] = new StoragePoolPointer() you would have non-contiguous native memory attached to those pointers.
There are (at least) two approaches to solving this. One is directly allocating the native memory using JNA's Memory class, like this:
pPointerArr = new Memory(10 * Native.POINTER_SIZE);
After the method call to virConnectListAllStoragePools(), you'd have the pointers in this buffer and could iterate, e.g., Pointer p = pPointerArr.share(i * Native.POINTER_SIZE) would be each pointer as you incremented i.
Alternately, you could define a Structure containing a pointer, like this:
#FieldOrder({ "storagePool" })
public class StoragePoolPointer extends Structure {
public Pointer storagePool;
}
Then you could get both the Java array convenience and contiguous native memory with:
StoragePoolPointer[] pPointerArr =
(StoragePoolPointer[]) new StoragePoolPointer().toArray(10);
After populating the arrays, pPointerArr[i].storagePool would be the ith pointer.
Assuming the 10 you are using is just a test placeholder, the standard JNA idiom in this situation is to call virConnectListAllStoragePools() once with a null pointer to get the number of elements to use, then allocate the memory as I've described above and call it a second time with the properly sized buffer (or array of Structures backed by a buffer).

Java 3.0 get distinct values from a column mongodb

I am really struggling here and have looked at other questions but just cant seem to get the answer I need.
What I am trying to do is pull through all the unique values of a column and then iterate through them and add them to an array. Ending up with the one column being stored in my array, but one of each value that exists not the multiple like there currently is.
Every time I try and do .distinct it asks me for the return class I have tried many different class but it just doesn't seem to work... Code is below any help would be appreciated.
public static void MediaInteraction() {
//Storing data from MediaInteraction in MediaArray
//BasicDBObject Query = new BasicDBObject();
//Query.put("consumerid", "");
MongoCursor<Document> cursormedia = collectionmedia.distinct("consumerid", (What do I put here?)).iterator();
while (cursormedia.hasNext()) {
System.out.println(cursormedia.next());
MediasessionID.add(cursormedia.next());
}
System.out.println("Media Array Complete");
System.out.println(MediasessionID.size());
}
The change that you probably want to introduce shall be somewhat like -
MongoCursor<Document> cursormedia = collectionmedia.distinct("consumerid",
<ConsumerId-DataType>.class).iterator(); //please replace the consumerId field's datatype here
Also from the docs -
/**
* Gets the distinct values of the specified field name.
*
* #param fieldName the field name
* #param resultClass the class to cast any distinct items into.
* #param <TResult> the target type of the iterable.
* #return an iterable of distinct values
* #mongodb.driver.manual reference/command/distinct/ Distinct
*/
<TResult> DistinctIterable<TResult> distinct(String fieldName, Class<TResult> resultClass);
So in your example, if you are trying to attain cursor for Document you probably want to use Document.class in the above suggested code.
Edit - Also the fact that you are calling cursormedia.next() twice the count of your MediasessionID would be halved. Suggest you do that(.next) once improving it further to obtain results.

how to copy javadoc from variables to constructor

I am having some problems with Javadoc. I have written documentation for variables of a class. And then I want to use that same javaDoc in the constructor. I didn't seem to be able to use #link or #see for this purpose (Well, Netbeans didn't show the result I liked).
It seems like a hassle to copy-paste everything, so is there a tag/parameter to copy javaDoc?
Here is the example:
/**
* The id for identifying this specific detectionloop. It is assumed the
* Detectionloops are numbered in order, so Detectionloop '2' is always next to
* Detectionloop '1'.
*/
private int id;
/**
* Constructor for a detectionloop. Detectionloops are real-world sensors
* that register and identify a kart when it passes by. Please note that
* this class is still under heavy development and the parameters of the
* constructor may change along the way!
*
* #param id The id for identifying this specific detectionloop. It is assumed
* the Detectionloops are numbered in order, so Detectionloop '2' is always
* next to Detectionloop '1'.
* #param nextID The id of the next detectionloop is sequense.
* #param distanceToNext The distance in meters to the next detectionloop.
*/
DetectionLoop(int id, int nextID, int distanceToNext) {
this.distanceToNext = distanceToNext;
this.id = id;
if (Detectionloops.containsKey(id)) {
throw new IllegalArgumentException("Detectionloop " + this.id
+ " already exist, please use a unused identification!");
} else {
Detectionloops.put(this.id, this);
}
}
This is unfortunately impossible using standard Javadoc. As a workaround, you could use the #link tag to reference the field, and then people could click the link to get at its documentation. This would require a click, but at least you don't have to maintain redundant documentation:
/**
* ...
* #param id the value for {#link #id}
The only other way of solving this that I know of is to write a custom doclet, which would allow you to define your own tag for your purpose.

USB Settings in android not getting stored

I am new to android framework. I am working with USB Settings. I want to know where are USB settings stored in android.
I am having following problem :
when i connect my phone to Computer and select one of the option from mass storage, MTP,PTP and now if I switch off and then switch on the device my selection get cleared. I want the previous selection to be retained.
What should I do..??
Thanks in advance...
Edit : I discovered today that usb_device_manager.xml file should be created in /data/system folder but in my case it is not getting generated.
I am unable to find what is the reason behind this.
The file that creates this file is located at frameworks/base/services/java/com/android/server/usb/
Waiting for positive reply...
According to http://developer.android.com/reference/android/hardware/usb/UsbManager.html
You cannot find an open API for getting this information.
However, from the source code we can see this two functions:
/**
* Name of the MTP USB function.
* Used in extras for the {#link #ACTION_USB_STATE} broadcast
*
* {#hide}
*/
public static final String USB_FUNCTION_MTP = "mtp";
/**
* Name of the PTP USB function.
* Used in extras for the {#link #ACTION_USB_STATE} broadcast
*
* {#hide}
*/
public static final String USB_FUNCTION_PTP = "ptp";
/**
* Returns the current default USB function.
*
* #return name of the default function.
*
* {#hide}
*/
public String getDefaultFunction() {
String functions = SystemProperties.get("persist.sys.usb.config", "");
int commaIndex = functions.indexOf(',');
if (commaIndex > 0) {
return functions.substring(0, commaIndex);
} else {
return functions;
}
}
/**
* Sets the current USB function.
* If function is null, then the current function is set to the default function.
*
* #param function name of the USB function, or null to restore the default function
* #param makeDefault true if the function should be set as the new default function
*
* {#hide}
*/
public void setCurrentFunction(String function, boolean makeDefault) {
try {
mService.setCurrentFunction(function, makeDefault);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in setCurrentFunction", e);
}
}
It is not recommended to use hidden APIs, but this might solve your problem.
If you wants to know how to use the hidden APIs, refer to this link:
http://devmaze.wordpress.com/2011/01/18/using-com-android-internal-part-1-introduction/

Checkstyle working differently for same condition

On applying check style i am getting " hides a field" if the name of formal and actual parameters are same.
private String limitedDimensionId;
/**
* Sets the limited dimension id.
*
* #param limitedDimensionId
* the new limited dimension id
*/
public void setLimitedDimensionId(final String limitedDimensionId) {
this.limitedDimensionId = limitedDimensionId;
}
However i am not getting the same issue in the following case:
private boolean fallBack;
/**
* #param isFallBack
* the isFallBack to set
*/
public void setFallBack(final boolean isFallBack) {
this.fallBack = isFallBack;
}
Both the conditions appear same to me. Still the discrepancy. Usually i change the name of the parameter variable to resolve this check style issue. But looking at the other case i am getting a hint that a more elegant solution is available. Any insights?
The variable names are different:
fallBack vs isFallBack
Usually i change the name of the parameter variable to resolve this check style issue
That's correct solution.
I would agree that giving them different names is more appropriate, however the "this" keyword in the "this.limitedDimensionid" should avoid the "hides a field" error. That's what it's for...

Categories