java


Specify that a Class argument must implement a particular interface [on hold]


I am writing a method which is passed a Class as an argument, like this:
public void doStuff(Class<?> clazz) {
/* do stuff */
}
I can then call the method like this:
doStuff(FooBar.class);
How do I enforce at the compiler level that the argument can only be a class which implements a particular interface?
I’ve tried the following—
public <T extends FooInterface> void doStuff(Class<T> clazz) {
/* do stuff */
}
—but get a compiler error for the following statement:
doStuff(FooBar.class); // FooBar implements FooInterface
How do I do this correctly?
I tried your statement but it seems to work. Please have a look at the code below. I don't know the exact class definitions, but here in this case
t.doStuff(Foo.class)
works but
t.doStuff(Faa.class)
does not
public class Test{
public <T extends FooInterface> void doStuff(Class<T> clazz) {
/* do stuff */
}
public static void main(String[] args) {
Test t = new Test();
t.doStuff(Foo.class); //This one compiles fine
//g.doStuff(Faa.class); <-- This line gives error
}
}
interface FooInterface{
}
class Foo implements FooInterface{
}
class Faa{
}
Turns out the code I tried was in fact correct, and the compiler error (that is, Eclipse underlining the call in red) was a result of a missing try/catch block around the call.
There are two possible ways to achieve the desired result:
public <T extends FooInterface> void doStuff(Class<T> clazz) {
/* do stuff */
}
Or, less elaborate:
public void doStuff(Class<? extends FooInterface> clazz) {
/* do stuff */
}

Related Links

Do servlet filters have to be specified in the web.xml file?
How does a scroll pane do scrolling
Why do the Java bytecodes for invoking methods implicitly acquire and release monitors?
What does Object<String> signify in Java?
Android-java vs PC-Java
How do I handle a callback in java
Jython: Error in subprocess module, AttributeError: 'module' object has no attribute 'python'
Does Spring #Transactional attribute work on a private method?
Can't connect to a SQLite database
Working in a BufferedImage's int[] pixels array
How do I create the first line in a new Google Spreadsheet using the API?
How do I stop EL expressions from being evaluated in non-rendered JSF components?
OutOfMemoryError trying to run Cactus unit tests
Liskov's Substitution Principle - How to model Square and Rectangle [duplicate]
Get Date as of 4 hours ago
detecting vulnerabilities in web applications and desktop applications

Categories

HOME
xamarin
winforms
keycloak
pycharm
relative-path
framework7
kalman-filter
yarn
ebean
pheatmap
synchronization
node-pdfkit
row
export-to-csv
circular-dependency
serverless-framework
highlight.js
pc
oxyplot
kryo
sylius
autosys
trading
facet
semantic-analysis
greendao
linkerd
maquette
unboundid
r-raster
ioio
calibre
hybridauth
openoffice.org
stacked
service-discovery
glew
node-sass
long-polling
worksheet
ionicons
elgg
carrot
kbuild
nsarray
r-forge
namecoin
nbconvert
hittest
or-tools
setuptools
asp.net-4.5
nsviewcontroller
qcustomplot
design-by-contract
ios4
goose
cloudbees
pervasive-sql
endeca-workbench
python-3.2
django-unittest
angular-leaflet-directive
android-listview
twirl
remobjects
inmobi
clicktag
dd
camanjs
tablelayout
android-imagebutton
relocation
cctv
wordpress-theme-customize
bitcoinj
xceed-datagrid
oracle-warehouse-builder
dotnetnuke-5
flash-builder4.5
seed
orchardcms-1.7
interface-orientation
rdoc
gdata-api
pysimplesoap
objective-c-2.0
netbeans-6.9
buildr
bigcouch
fireworks
data-loss
blackberry-playbook
xdomainrequest
horizontal-accordion
winbugs14
html-input
paster
asp.net-profiles
photoshop-cs4
ntvdm.exe

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile