update copyright
[fedora-idea.git] / xml / dom-impl / src / com / intellij / util / xml / impl / VisitorDescription.java
blobb716e23ed26947ca056be48e5495043dec777338
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.util.xml.impl;
18 import com.intellij.util.ReflectionCache;
19 import com.intellij.util.containers.ConcurrentClassMap;
20 import com.intellij.util.xml.DomElement;
21 import com.intellij.util.xml.DomElementVisitor;
22 import com.intellij.util.xml.DomReflectionUtil;
23 import org.jetbrains.annotations.NonNls;
25 import java.lang.reflect.Method;
27 /**
28 * @author peter
30 public class VisitorDescription {
31 private final Class<? extends DomElementVisitor> myVisitorClass;
32 private final ConcurrentClassMap<Method> myMethods = new ConcurrentClassMap<Method>();
33 @NonNls private static final String VISIT = "visit";
35 public VisitorDescription(final Class<? extends DomElementVisitor> visitorClass) {
36 myVisitorClass = visitorClass;
37 for (final Method method : ReflectionCache.getMethods(visitorClass)) {
38 final Class<?>[] parameterTypes = method.getParameterTypes();
39 if (parameterTypes.length != 1) {
40 continue;
42 final Class<?> domClass = parameterTypes[0];
43 if (!ReflectionCache.isAssignable(DomElement.class, domClass)) {
44 continue;
46 final String methodName = method.getName();
47 if (VISIT.equals(methodName) ||
48 methodName.startsWith(VISIT) && domClass.getSimpleName().equals(methodName.substring(VISIT.length()))) {
49 method.setAccessible(true);
50 myMethods.put(domClass, method);
55 public void acceptElement(DomElementVisitor visitor, DomElement element) {
56 final Method method = myMethods.get(element.getClass());
57 assert method != null : myVisitorClass + " can't accept element of type " + element.getClass();
58 DomReflectionUtil.invokeMethod(method, visitor, element);