Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There should be a possibility to specify "post-construct" code. #251

Open
Tomas-Kraus opened this issue Oct 9, 2009 · 15 comments
Open

There should be a possibility to specify "post-construct" code. #251

Tomas-Kraus opened this issue Oct 9, 2009 · 15 comments
Assignees
Labels
enhancement New feature or request Major Priority

Comments

@Tomas-Kraus
Copy link
Contributor

There should be a possibility to specify code, which JAXB would call after an
object was unmarshalled (after all fields were set, so that method could use
them). It is not possible to achieve this using the ctor.

This functionality could be made available either by a new annotation
(@XmlPostConstruct or something).

Please see this resource for a possible workaround:
http://stackoverflow.com/questions/1545478/is-there-something-like-postconstruct-for-jaxb-annnotated-classes/1545867#1545867

Environment

Operating System: All
Platform: All

Affected Versions

[2.0 FCS]

@Tomas-Kraus
Copy link
Contributor Author

@glassfishrobot Commented
Reported by converginglight

@Tomas-Kraus
Copy link
Contributor Author

@glassfishrobot Commented
Was assigned to snajper

@Tomas-Kraus
Copy link
Contributor Author

@glassfishrobot Commented
converginglight said:
Sorry for the typo in description:

There should be a possibility to specify a "post-construct" method, which JAXB
would call after an object was unmarshalled
(after all fields were set, so that the "post-construct" method could use them).
It is not possible to achieve this using the ctor.

@Tomas-Kraus
Copy link
Contributor Author

@glassfishrobot Commented
skaffman said:
This is a great idea, and is exactly what the standard @PostConstruct annotation
is intended for.

@Tomas-Kraus
Copy link
Contributor Author

@glassfishrobot Commented
@pavelbucek said:
isn't this exactly what does afterUnmarshal method? Try adding

public void afterUnmarshal(Unmarshaller u, Object parent)

{ // do something }

to your java class..

@Tomas-Kraus
Copy link
Contributor Author

@glassfishrobot Commented
converginglight said:
In my opinion, a special annotation would be much more handy:

  1. Much less code to write

  2. More straightforward, more obvious code

@Tomas-Kraus
Copy link
Contributor Author

@glassfishrobot Commented
@pavelbucek said:
ad 1) much less code? annotation + method is less than method?

ad 2) maybe, but JAXB provides four "callbacks": beforeMarshal, afterMarshal,
beforeUnmarshal and afterUnmarshal. There are no annotations for others and I
think providing two ways to call only one callback could be misleading.

And additionally -
http://java.sun.com/javaee/5/docs/api/javax/annotation/PostConstruct.html says
that PostConstruct is meant for classes which use dependency injection and this
is not the case AFAIK (Unmarshalling is not considered as injection).

@Tomas-Kraus
Copy link
Contributor Author

@glassfishrobot Commented
converginglight said:
No, I mean: you don't have to write the listener class,
which is a lot of "saving":
(The 'real' implementation might be even bigger.)

public class CustomListener
extends Unmarshaller.Listener {

@OverRide
public void afterUnmarshal(Object object, Object arg1) {
System.out.println("unmarshalling finished on: " + object);

Class<?> type = object.getClass();
Method postConstructMethod = null;

for (Method m : ReflectionUtils.getAllMethods(type)) {
if (m.getAnnotation(PostConstruct.class) != null) {
if (postConstructMethod != null)

{ throw new IllegalStateException( "@PostConstruct used multiple times"); }

postConstructMethod = m;
}
}

if (postConstructMethod != null) {
System.out.println("invoking post construct: "

  • postConstructMethod.getName() + "()");

if (!Modifier.isFinal(postConstructMethod.getModifiers()))

{ throw new IllegalArgumentException("post construct method [" + postConstructMethod.getName() + "] must be final"); }

try

{ postConstructMethod.setAccessible(true); postConstructMethod.invoke(object); }

catch (IllegalAccessException ex)

{ throw new RuntimeException(ex); } catch (InvocationTargetException ex) { throw new RuntimeException(ex); }

}
}
}

@Tomas-Kraus
Copy link
Contributor Author

@glassfishrobot Commented
@pavelbucek said:
You don't need to write Listener class when you use (JAXB) life cycle methods,
they are discovered automatically..

@Tomas-Kraus
Copy link
Contributor Author

@glassfishrobot Commented
converginglight said:
I couldn't find usable information about JAXB's "life cycle methods". Could you
tell more specific, what you mean?

@Tomas-Kraus
Copy link
Contributor Author

@glassfishrobot Commented
@pavelbucek said:
It's defined in jsr222:

http://jcp.org/en/jsr/detail?id=222

@Tomas-Kraus
Copy link
Contributor Author

@glassfishrobot Commented
converginglight said:
I read the PDF of that JSR.
Could not find a word about life cycle methods.
(Perhaps some synonym is used?)

Please, explain what you mean.

@Tomas-Kraus
Copy link
Contributor Author

@glassfishrobot Commented
flb_at_ll said:
If I understand things correctly, afterUnmarshal will be called after the
document has been fully unmarshalled.

What I think is needed is the ability to operate on objects as they are created.
More important to me would be able to transform an object into a child wrapper
class before the bind/resolve call. Unfortunately, bind is called before the
object members have been fully populated, so all re-wrapping must occur after
unmarshal which means having to walk the entire document tree and update all
references to a re-wrapped object.

@Tomas-Kraus
Copy link
Contributor Author

@glassfishrobot Commented
This issue was imported from java.net JIRA JAXB-698

@Tomas-Kraus
Copy link
Contributor Author

@lukasj lukasj transferred this issue from eclipse-ee4j/jaxb-ri May 19, 2022
@lukasj lukasj added enhancement New feature or request and removed Component: spec labels May 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Major Priority
Projects
None yet
Development

No branches or pull requests

3 participants