faces highlighting
[fedora-idea.git] / source / com / intellij / util / xml / impl / InvocationCache.java
blob487c01f2d9ea901f95d178a6c68f02c3e380afe6
1 /*
2 * Copyright (c) 2005 Your Corporation. All Rights Reserved.
3 */
4 package com.intellij.util.xml.impl;
6 import com.intellij.psi.xml.XmlTag;
7 import com.intellij.util.xml.DomElement;
8 import com.intellij.util.xml.GenericAttributeValue;
9 import com.intellij.util.xml.JavaMethodSignature;
10 import com.intellij.util.xml.StableElement;
11 import com.intellij.pom.Navigatable;
13 import java.lang.reflect.Method;
14 import java.util.HashMap;
15 import java.util.Map;
17 /**
18 * @author peter
20 public class InvocationCache {
21 private static final Map<JavaMethodSignature, Invocation> ourCoreInvocations = new HashMap<JavaMethodSignature, Invocation>();
22 private final Map<JavaMethodSignature, Invocation> myInvocations = new HashMap<JavaMethodSignature, Invocation>();
24 static {
25 addCoreInvocations(DomElement.class);
26 addCoreInvocations(Navigatable.class);
27 addCoreInvocations(Object.class);
28 try {
29 ourCoreInvocations.put(JavaMethodSignature.getSignature(GenericAttributeValue.class.getMethod("getXmlAttribute")), new Invocation() {
30 public final Object invoke(final DomInvocationHandler handler, final Object[] args) throws Throwable {
31 final XmlTag tag = handler.getXmlTag();
32 return tag != null ? tag.getAttribute(handler.getXmlElementName(), null) : null;
34 });
36 catch (NoSuchMethodException e) {
37 throw new AssertionError();
41 private static void addCoreInvocations(final Class<?> aClass) {
42 for (final Method method : aClass.getDeclaredMethods()) {
43 if ("equals".equals(method.getName())) {
44 ourCoreInvocations.put(JavaMethodSignature.getSignature(method), new Invocation() {
45 public Object invoke(DomInvocationHandler handler, Object[] args) throws Throwable {
46 return _equals(handler.getProxy(), args[0]);
48 private boolean _equals(final DomElement proxy, final Object o) {
49 return proxy == o || o instanceof StableElement && _equals(proxy, ((StableElement)o).getWrappedElement());
52 });
54 else {
55 ourCoreInvocations.put(JavaMethodSignature.getSignature(method), new Invocation() {
56 public Object invoke(DomInvocationHandler handler, Object[] args) throws Throwable {
57 return method.invoke(handler, args);
59 });
65 public Invocation getInvocation(JavaMethodSignature method) {
66 Invocation invocation = ourCoreInvocations.get(method);
67 return invocation != null ? invocation : myInvocations.get(method);
70 public void putInvocation(JavaMethodSignature method, Invocation invocation) {
71 myInvocations.put(method, invocation);