Java enums are great. So are generics. Of course we all know the limitations of the latter because of type erasure. But there is one thing I don’t understand, Why can’t I create an enum like this: This generic type parameter <T> in turn could then be useful in various places. Imagine a gener…
Java IOException “Too many open files”
I’m doing some file I/O with multiple files (writing to 19 files, it so happens). After writing to them a few hundred times I get the Java IOException: Too many open files. But I actually have only a few files opened at once. What is the problem here? I can verify that the writes were successful. Answer…
Can we make unsigned byte in Java
I am trying to convert a signed byte in unsigned. The problem is the data I am receiving is unsigned and Java does not support unsigned byte, so when it reads the data it treats it as signed. I tried …
IllegalAnnotationException: Two classes have the same XML type name
I am developing web service under JBoss 5 and Java 1.6. What could possibly cause this exception? Below is my simplified web service code. And below is the full stack trace. Answer I found the cause of my problem. This problem occurs because JAX-WS generates a class for each method and the class name is const…
Loop through array, each element a JUnit test
I have a JUnit 4 test that loops through an array of test data: Because it’s all in one test method, the whole test stops as soon as one element in the array fails. Is there a way around that, without making a method for each test item? Maybe something with reflection? Answer Use JUnit 4’s paramet…
Comparing arrays in JUnit assertions, concise built-in way?
Is there a concise, built-in way to do equals assertions on two like-typed arrays in JUnit? By default (at least in JUnit 4) it seems to do an instance compare on the array object itself. EG, doesn’t work: Of course, I can do it manually with: ..but is there a better way? Answer Use org.junit.AssertR…
Why doesn’t this generic cast fail?
I’d expect this code to throw a ClassCastException: But it doesn’t. Casting String to T doesn’t fail, until I use the returned object somehow, like: Background: I created a Class which uses JAXB to unmarshal an XML file. It looks like this: Depending on whether the root-Element is an anonymo…
How do I pass kilogram, gram to a kilogram converter and obtain the result in grams
How do I pass kilogram, gram to a kilogram converter and obtain the result in grams (e.g. 5 kg 500 grams = 5500 grams) How to do this using javax.measure.* Refer over here Note: I need the solution only based on the above packages and not some generic solution Answer It’s a rather cumbersome API, but so…
Determine target service/method from CXF Interceptor
I’d like to write an interceptor for the Apache CXF JAX-RS implementation that inspects the target service/method for a particular annotation and does some special processing for that annotation. I can’t seem to find anything in the interceptor documentation that describes how to do this. Does any…
How to scale a BufferedImage
Following the javadocs, I have tried to scale a BufferedImage without success here is my code: I can’t understand why it is not working, any help? Answer AffineTransformOp offers the additional flexibility of choosing the interpolation type. The fragment shown illustrates resampling, not cropping; this …