Generic CLASS parameters
Hi,
I have a function module with a TABLE parameter with generic type TABLE.
I have to pass these table parameter to a method in the class, then what should be the type of the corresponding parameter inside my class.
Regards,
Anoop
Ah, you can't create a generic class attribute. But you <b>can</b> create a data reference to a generic object.
Instead of creating an attribute "mytab", create an attribute that is a reference to data,e.g. <b>ref_mytab</b>, of type REF TO DATA.
In your method, where you've received the table through the parameters (e.g. it_mytab TYPE STANDARD TABLE ), do this:
GET REFERENCE OF it_mytab INTO ref_mytab.
Whenever you need to do something with the table, do this:
FIELD-SYMBOLS: <mytab> TYPE STANDARD TABLE.
ASSIGN ref_mytab->* TO <mytab>.
matt
Similar Messages
-
Generic classes with parameterized super class and interface
Hello.
I'm trying to write a generic class that looks like the following pseudo-code.
public interface Interface {
public Interface getSibling(...);
public class Klass {...}
public class MyKlass<T extends Klass, T2 extends Interface>
extends T implements T2 {
public T2 getSibling(...) {
return this;
}where Interface and Klass each have various extensions.
I came across this problem, or challenge, while writing classes for testing my EJBs. I tried and failed various attempts.
Is it possible to write generic classes of this nature in Java?
If so, please tell me and others who are like me.
Thanks in advance.No. That would not work.
Beside being forbidden by the compiler, to my understanding, it cannot be done in theory either, as the parameterized types get bound at instantiation time and are not available for static reference, which both extends and implements require. -
What is the use of Generic class in java
hi everyone,
i want to know that
what is the use of Generic class in java ?
regards,
dhruvangSimplistically...
A method is a block of code that makes some Objects in the block of code abstract (those abstract Objects are the parameters of the method). This allows us to reuse the method passing in different Objects (arguments) each time.
In a similar way, Generics allows us to take a Class and make some of the types in the class abstract. (These types are the type parameters of the class). This allows us to reuse the class, passing in different types each time we use it.
We write type parameters (when we declare) and type arguments (when we use) inside < >.
For example the List class has a Type Parameter which makes the type of the things in the list become abstract.
A List<String> is a list of Strings, it has a method "void add(String)" and a method "String get(int)".
A List<File> is a list of Files, it has a method "void add(File)" and a method "File get(int)".
List is just one class (interface actually but don't worry about that), but we can specify different type arguments which means the methods use this abstract type rather than a fixed concrete type in their declarations.
Why?
You spend a little more effort describing your types (List<String> instead of just List), and as a benefit, you, and anyone else who reads your code, and the compiler (which also reads your code) know more accurately the types of things. Because more detail is known, the compiler is able to tell you when you screw up (as opposed to finding out at runtime). And people understand your code better.
Once you get used to them, its a bit like the difference between black and white TV, and colour TV. When you see code that doesn't specify the type parameters, you just get the feeling that you are missing out on something. When I see an API with List as a return type or argument type, I think "List of what?". When I see List<String>, I know much more about that parameter or return type.
Bruce -
I thought it would be useful if the class literal could return a generic class. For
e.g. something like this
Class<HashMap<Long,Long>> c = HashMap.class;
Obviously the above will not work by typecasting or templating the RHS. A
work around could be something like
HashMap<Long,Long> h = new HashMap<Long,Long> ();
Class<HashMap<Long,Long>> c = (HashMap<Long,Long>)h.getClass();
Are there any other better work arounds?Hmmm, I was thinking of passing a generic Class to
the method so that I could use reflection to
instantiate the class (instead of new HashMap<K,
V>()). That way, the method would work not just for
HashMap but for anything implementing Map. For e.g. I
also use EnumMap simillarly.Okay, now I see where you're going with this. I don't see any reason the following wouldn't work if the client uses it correctly. You would need to document clearly the class must either be generic or implement the parameterized types specified and it must have a no-argument constructor.
<K, V> Map<K, V> putIfMissing(final String key,
final Map<String, Map<K, V>> h,
final Class<? extends Map> clazz)
throws IllegalAccessException, InstantiationException {
Map<K, V> value = h.get(key);
if (value == null) {
// Unchecked assignment. Will be fine if the class passed is a
// generic Map or if it implements Map<K, V> with the same types.
value = clazz.newInstance();
h.put(key, value);
return value;
}You'll have to be very careful to document everything that could go wrong with this. Even then I'm not sure there's not a gotcha in there somewhere. A perhaps more object-oriented and safer solution, though more verbose, would be for the client to pass the mechanism for creating the map.
interface MapFactory<K, V> {
Map<K, V> createMap();
<K, V> Map<K, V> putIfMissing(final String key,
final Map<String, Map<K, V>> h,
final MapFactory<K, V> factory) {
Map<K, V> value = h.get(key);
if (value == null) {
value = factory.createMap();
h.put(key, value);
return value;
} -
Using static .values() method of Enum in Generic Class
Hi *,
I tried to do the following:
public class AClass<E extends Enum<E> > {
public AClass() {
E[] values = E.values(); // this DOESN'T work
for (E e : values) { /* do something */ }
}This is not possible. But how can I access all Enum constants if I use
an Enum type parameter in a Generic class?
Thanks for your help ;-) StephanHere's a possible workaround. The generic class isn't adding much in this case; I originally wrote it as a static method that you simply passed the class to:
public class Test21
public static enum TestEnum { A, B, C };
public static class AClass<E extends Enum<E>>
private Class<E> clazz;
public AClass(Class<E> _clazz)
{ clazz = _clazz; }
public Class<E> getClazz()
{ return clazz; }
public void printConstants()
for (E e : clazz.getEnumConstants())
System.out.println(e.toString());
public static void main(String[] argv)
AClass<TestEnum> a = new AClass<TestEnum>(TestEnum.class);
a.printConstants();
} -
Hp LaserJet 1200 Generic Class: Waiting for device
I have 2 computers that I want to print to my hp Laserjet 1200 printer. We are using an iogear 4-port usb 2.0 sharing switch. One computer prints fine but the other always displays a message "Generic Class: Waiting for device." Help!
Both computers have the printer driver installed. The usb device does not have a driver to install. You just plug in the usb cords to each computer and the printer. You only install a driver if using a pc operating system.
-
Best way to call a function in a generic class from a base class
Hi,
I have a generic class that works with subclasses of some other baseclass. When I construct the subclass, I call the constructor of the baseclass which I want to call a function in the generic class. e.g. suppose I have the following classes
public class List<T extends BaseClass>
newTCreated(T t)
}Sorry, I pressed Tab and Enter last time when typing the code so I posted without meaning to
Hi,
I have a generic class that works with subclasses of some other baseclass. When I construct the subclass, I call the constructor of the baseclass which I want to call a function in the generic class. e.g. suppose I have the following classes
public class List<T extends BaseClass>
public void newTCreated(T t)
// add the t to some internal list
public T getT(int index)
// get the object from the internal list
public class BaseClass
public BaseClass(List<?> list)
list.newTCreated(this);
public class SubClass extends BaseClass
public SubClass(List<SubCass> list)
super(list);
}This doesn't compile because of the call to newTCreated in the BaseClass constructor because BaseClass is not necessarily of type T. Is there any way of checking when I call the newTCreated function that the BaseClass is actually of type SubClass? I could either add the call explicitly in each SubClass's constructor or have a function addToList in BaseClass that is called from the BaseClass constructor but overloaded in each subclass but both of those rely on future subclasses doing the same. Or I could change the newTCreated function to take an argument of type BaseClass and then cast it to type T but this doesn't give a compilation error, only a runtime exception.
It seems like there should be solution but having only recently started writing Generic classes I can't find it. Thanks in advance for any help,
Tom -
Getting the name of the generic class?
I have a generic class declared as such:
public class MyClass<T>
and I want to access the name of the <T> class a member function in that class? I just can't figure out which syntax to use.
Thank you!
JoshuaSorry, I'm a little bit confused at your answer.
I cannot seem to instantiate an object of class T, like: T item = new T();does not work. If I can't create an object of type T, then I can't get it's type. I need to get the class even if I don't have any instantiated objects (or if I can create a blank one), so that I can use the name to fetch the objects from the persistence engine. Here is the class, so you can see what I'm talking about. I have in there now what seems like it should be right, but doesn't compile.
A small example of solution please?
public class PersistentSelectorCellEditor<T> extends AbstractCellEditor
implements TableCellEditor {
JComboBox control;
List<T> choices;
Session session;
public PersistentSelectorCellEditor() {
initializeComponents();
public PersistentSelectorCellEditor(Object value) {
initializeComponents();
this.control.setSelectedItem(value);
private void initializePersistence() {
session = LabApp.getSession();
session.beginTransaction();
private void initializeComponents() {
initializePersistence();
populateList(); //query for selections to fill combo box
control = new JComboBox((ComboBoxModel)choices);
private void populateList() {
//query to populate drop down lists
Query query = session.createQuery("from " + T.class.getName());
choices = query.list();
public void setValue(T value) {
control.setSelectedItem(value);
public T getValue() {
return (T)control.getSelectedItem();
//interface members
public Object getCellEditorValue() {
return control.getSelectedItem();
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
return control;
}Thank you!
Joshua -
Hi.
i have to create an array of generic class
ex: GenericClass<E> [ ] x = new GenericClass<E>[ y ]
is it possible?
i can create GenericClass<E> [ ] x; but i can't initiate it in this way..
Someone know how i can do it?crosspost
http://forum.java.sun.com/thread.jspa?threadID=746524&messageID=4272614#4272614 -
Changing generic selection parameters for Payment advice notes
Hi,
I want to add few more generic selection parameters for Automatic Payment advice notes generation on the seletion screen(f110 - rffoavis_fpaym). Please let me know the config settings for doing this.Hi Pradeep,
you can change the sender, when you change the system-field SY-UNAME before calling FM SO_NEW_DOCUMENT_ATT_SEND_API1.
REPORT Z_TEST_SEND_MAIL.
h_uname must be a SAP-UserId.
sy-uname = h_uname.
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
DOCUMENT_DATA = DOC_CHNG
PUT_IN_OUTBOX = 'X'
COMMIT_WORK = 'X'
TABLES
PACKING_LIST = OBJPACK
OBJECT_HEADER = OBJHEAD
CONTENTS_BIN = OBJBIN
CONTENTS_TXT = OBJTXT
RECEIVERS = RECLIST
EXCEPTIONS
TOO_MANY_RECEIVERS = 1
DOCUMENT_NOT_SENT = 2
OPERATION_NO_AUTHORIZATION = 4
OTHERS = 99.
I tried it and it was successfull. -
How to get the parameter name of a generic class?
Is it possible to get the name of generic class parameter type without having an instance of that type?
e.g.
class MyClass<T> {
public MyClass() {
// get the name here
}I need to get a string containing "Object" (or "java.lang.Object") if the class would be used like this:
MyClass<Object> mc = new MyClass<Object>() -
"Master Data Read Class Parameters" field is disabled
Hi.
While creating infoobject I switch master data access from Default to Own implementation,
after this screen is redisplayed and only Name of Master Data Read Class field become enabled.
I need Master Data Read Class Parameters field, but it is disabled and button near this field has the same status.
Why? How to make them active?Hi,
I think it is not possible to add own Master Data Class if the reference is 0DATE or if the type is DATS.
So may be you can create your infoobject as Type CHAR, Length 8.
May be you can add a Conversion Routine for the required format of date to be displayed. -
Hello All,
I want to create a generic class which is used to perorm SAP data lookup.
I don't want to use Jco or RFC channel..
Is there any other way to do this?
waiting for Reply
thank in advance.
- AKSHAY.Hi,
use RFC channel
you can wrap it up like this:
/people/morten.wittrock/blog/2006/03/30/wrapping-your-mapping-lookup-api-code-in-easy-to-use-java-classes
why do you want to create something diffucult to maintin and non standard if
you can use the RFC API ?
Regards,
michal
<a href="/people/michal.krawczyk2/blog/2005/06/28/xipi-faq-frequently-asked-questions"><b>XI / PI FAQ - Frequently Asked Questions</b></a> -
Question about typecast to more generic Class
I have controls refnum and pages refnum and I want to know which way is better:
1) let LabVIEW doing the job
or 2) use "typecast to more generic Class".
In both case the VI works properly.
Thanks
Jean-Marc
Jean-Marc
LV2009 and LV2013
Free PDF Report with iTextSharp
Solved!
Go to Solution.
Attachments:
Test LV86.zip 153 KBThanks for your reply Ben.
I need the controls refnum in the visible part of the pane (for the translation english to french and french to english ).
Jean-Marc
Jean-Marc
LV2009 and LV2013
Free PDF Report with iTextSharp
Attachments:
Translation LV86.zip 235 KB -
hi,
i m using creator 2 update 1, it does not support generic classes.
it says use source 1.5 for generic classes.
please tell me how to solve this problem
thank youWell, not necessarily true... Most of the latest graphics cards work off of a common core, which is named depending on its maximum tested performance and on how many pipelines passed quality checks.
Thus a 7300 is fundementally identical to a 7800, except that it uses fewer fragment/vertex pipelines and is clocked lower. This is done either artificially by disabling parts of the GPU, or if when they fail quality control.
Either way, the drivers are the same, and have been unified for a long time for the entire product line. In fact if you look at the drivers for the card in yoru computer you will see they are more then likely for a different chipset. My MBP claims the drivers are for a X1000, even though the card is a X1600.
Maybe you are looking for
-
How do I control the formatting of the presenters notes? When I enter notes now, the formatting is erratic: sometimes in one font, sometimes in another; sometimes one size, sometime another. Most of the time the text is too large & all of the notes c
-
I have problems installing update for PS Elements 9 under Windows 8.1. Are any problems known?
-
On the initial install, everything worked. However since then Vibe Desktop refuses to launch. It does not show up in the tray either. I have uninstalled, rebooted, installed. I have re-installed java, updated. I have deleted all folder structures, un
-
Strange error when trying to validate a business rule
I get this strange error when I try to validate a business rule: Detail:Cannot calculate. Essbase Error(1200369): Error optimizing formula for [69972] (line 38): can not cross members from the same dimension The rule is: FIX (@RELATIVE("SG&A", 0)) "6
-
Hi, i am created search page using query region ( resultbasesearch) and i want check box for each row in result set and i want to export to excel sheet pls help me what procedure i can follow reply asap