2 * Copyright (c) 2000-2006 JetBrains s.r.o. All Rights Reserved.
4 package com
.intellij
.util
.xml
.impl
;
6 import com
.intellij
.openapi
.util
.Factory
;
7 import com
.intellij
.util
.xml
.DomElement
;
8 import com
.intellij
.util
.xml
.StableElement
;
9 import net
.sf
.cglib
.proxy
.InvocationHandler
;
11 import java
.lang
.reflect
.InvocationTargetException
;
12 import java
.lang
.reflect
.Method
;
18 class StableInvocationHandler
<T
extends DomElement
> implements InvocationHandler
, StableElement
{
20 private T myCachedValue
;
21 private Set
<Class
> myClasses
;
22 private final Factory
<T
> myProvider
;
24 public StableInvocationHandler(final T initial
, final Factory
<T
> provider
) {
25 myProvider
= provider
;
26 myCachedValue
= initial
;
30 public void setClasses(final Set
<Class
> classes
) {
34 public final Object
invoke(Object proxy
, final Method method
, final Object
[] args
) throws Throwable
{
35 if (StableElement
.class.equals(method
.getDeclaringClass())) {
37 return method
.invoke(this, args
);
39 catch (InvocationTargetException e
) {
44 if (AdvancedProxy
.FINALIZE_METHOD
.equals(method
)) {
48 if (isNotValid(myCachedValue
)) {
49 if (myCachedValue
!= null) {
50 myOldValue
= myCachedValue
;
52 myCachedValue
= myProvider
.create();
53 if (isNotValid(myCachedValue
)) {
54 if (myOldValue
!= null && Object
.class.equals(method
.getDeclaringClass())) {
55 return method
.invoke(myOldValue
, args
);
58 if ("isValid".equals(method
.getName())) {
61 throw new AssertionError("Calling methods on invalid value");
66 return method
.invoke(myCachedValue
, args
);
68 catch (InvocationTargetException e
) {
73 public final void revalidate() {
74 final T t
= myProvider
.create();
75 if (!isNotValid(t
) && !t
.equals(myCachedValue
)) {
81 private void doInvalidate() {
82 final DomInvocationHandler handler
= DomManagerImpl
.getDomInvocationHandler(myCachedValue
);
83 if (handler
!= null) {
88 public final void invalidate() {
89 if (!isNotValid(myCachedValue
)) {
94 public final DomElement
getWrappedElement() {
95 if (isNotValid(myCachedValue
)) {
96 myCachedValue
= myProvider
.create();
101 public T
getOldValue() {
105 private boolean isNotValid(final T t
) {
106 if (t
== null || !t
.isValid()) return true;
107 for (final Class aClass
: myClasses
) {
108 if (!aClass
.isInstance(t
)) return true;