2

This line of code https://code.google.com/p/google-oauth-java-client/source/browse/google-oauth-client/src/main/java/com/google/api/client/auth/oauth2/BearerToken.java#44 contains a bug.

The INVALID_TOKEN_ERROR regex is wrong and there are no chances that the mantainers will fix it soon.

Beside forking the library, is there a way I can redefine this safely in my own code?

Incriminated code follows for who doesn't want to open the link:

public class BearerToken {
  [...]
  static final Pattern INVALID_TOKEN_ERROR = Pattern.compile("\\s*error\\s*=\\s*invalid_token");
John Smith
  • 1,726
  • 2
  • 18
  • 30
  • Using [reflection](http://stackoverflow.com/questions/3301635/change-private-static-final-field-using-java-reflection). The example linked is a `private` field, so you can ignore that much of it. – GriffeyDog Jan 28 '14 at 17:15

3 Answers3

0

No, I don't think so. Reflection won't save you here (see http://docs.oracle.com/javase/tutorial/reflect/member/fieldTrouble.html), a final field cannot be changed. If the field was a string for example, the value would have been inlined in the compiled code so changing it could spell disaster... So your options are to put pressure on the maintainers or fork the code.

JP Moresmau
  • 7,388
  • 17
  • 31
0

Although you can't replace the Pattern object since it is final, you can manipulate the member fields of the object INVALID_TOKEN_ERROR using reflection.

Update the private field String pattern and then call the private method compile() on INVALID_TOKEN_ERROR

How to update a private variable: Changing private final fields via reflection

How to call a private method: How to call a private method from outside a java class

Community
  • 1
  • 1
JustinKSU
  • 4,875
  • 2
  • 29
  • 51
  • This is interesting, do you mind expanding a bit the reply with a sample code idea? Java noob here. Thanks. – John Smith Jan 28 '14 at 17:30
0

Create your own com.google.api.client.auth.oauth2.BearerToken class and ensure the JAR/WAR/folder where it is located is in the ClassPath before the original library. Thus you may slip it under. I believe even the original library might use it. (Please check it out.)

Note: you may create a class in the same package but in another physical folder/directory. Thus you may e.g. access package private (default access) fields in someone else's packages. When JVM searches during runtime for the classes to be loaded, it searches the class path in the given order. The first occurrence of the class is used.

Honza Zidek
  • 9,204
  • 4
  • 72
  • 118