2

I would like to mark usage of certain methods provide by the JRE as deprecated. How do I do this?

4
  • Why would you want to do that? Jun 24, 2013 at 17:57
  • Making sure people don't use java's implementation and use my implementation from now on. Jun 24, 2013 at 17:58
  • 1
    Is this an X-Y problem?
    – erickson
    Jun 24, 2013 at 17:59
  • I don't want people to use java's implementation. That's all. Jun 24, 2013 at 18:01

7 Answers 7

10

You can't. Only code within your control can have the @Deprecated annotation added. Any attempt to reverse engineer the bytecode will result in a non-portable JRE. This is contrary to Java's write once, run anywhere methodology.

5

you can't deprecate JRE methods, but you can add warnings or even compile errors to your build system i.e. using AspectJ or forbid the use of given methods in the IDE.

For example in Eclipse: Go to Project properties -->Java Compiler --> Errors Warnings, Then enable project specific settings, Expand Deprecated and restrited APIs category "Forbidden reference (acess rule)"

Obviously you could instrument or override the class adding @Deprecated annotation, but it's not a clean solution.

2

Add such restrictions to your coding guidelines, and enforce as part of your code review process.

1

You only can do it, if and only if you are building your own JRE! In that case just add @Deprecated above the corresponding code block! But if you are using Oracle's JRE, you are no where to do so!

1
  • If you deprecate API yourself, you don't have a JRE. That is, you can't call it Java.
    – erickson
    Jun 24, 2013 at 17:56
1

In what context? Do you mean you want to be able to easily configure your IDE to inhibit use of certain API? Or are you trying to dictate to the world what APIs you prohibit? Or are you trying to do something at runtime?

If the first case, Eclipse, and I assume other IDEs, allow you to mark any API as forbidden, discouraged, or accessible at the package or class level.

If you mean the second, you can't, of course. That would be silly.

If you are trying to prohibit certain methods from being called at runtime, you can configure a security policy to prevent code loaded from specified locations from being able to call specific methods that check with the SecurityManager, if one is installed.

1

You can compile your own version of the class and add it to the boot class path or lib/ext directory. http://docs.oracle.com/javase/tutorial/ext/basics/install.html This will change the JDK and the JRE.

In fact you can remove it for compiling and your program won't compile if it is used.

1

Snihalani: Just so that I get this straight ...

You want to 'deprecate methods in the JRE' in order to 'Making sure people don't use java's implementation and use my implementation from now on.' ?

First of all: you can't change anything in the JRE, neither are you allowed to, it's property of Oracle. Uou might be able to change something locally if you want to go through the trouble, but that 'll just be in your local JRE, not in the ones that can be downloaded from the Oracle webpage.

Next to that, nobody has your implementation, so how would we be able to use it anyway? The implementations provided by Oracle do exactly what they should do, and when a flaw/bug/... is found it'll be corrected or replaced by a new method (at which point the original method becomes deprecated).

But, what mostly worries me, is that you would go and change implementations with something you came up with. Reminds me quite lot of phishing and such techniques, having us run your code, without knowing what it does, without even knowing we are running your code. After all, if you would have access to the original code and "build" the JRE, what's to stop you from altering the code in the original method?

Deprecated is a way for the author to say: "Yup ... I did this in the past, but it seems that there are problems with the method. just in order not to change the behaviour of existing applications using this method, I will not change this method, rather mark it as deprecated, and add a method that solves this problem".

You are not the author, so it isn't up to you to decide whether or not the methods work the way they should anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.