IDEA-172425 Add ability to enable reflection replacement with any JDK
[idea/community.git] / java / java-impl / src / com / intellij / refactoring / extractMethodObject / reflect / CompositeReflectionAccessor.java
1 // Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.refactoring.extractMethodObject.reflect;
3
4 import com.intellij.psi.PsiClass;
5 import com.intellij.psi.PsiElement;
6 import com.intellij.psi.PsiElementFactory;
7 import org.jetbrains.annotations.NotNull;
8
9 import java.util.ArrayList;
10 import java.util.List;
11
12 /**
13  * @author Vitaliy.Bibaev
14  */
15 public class CompositeReflectionAccessor implements ReflectionAccessor {
16   private final List<ReflectionAccessor> myAccessors = new ArrayList<>();
17
18   private CompositeReflectionAccessor() {
19   }
20
21   private void registerAccessor(@NotNull ReflectionAccessor accessor) {
22     myAccessors.add(accessor);
23   }
24
25   public static ReflectionAccessor createAccessorToEverything(@NotNull PsiClass psiClass, @NotNull PsiElementFactory elementFactory) {
26     CompositeReflectionAccessor compositeAccessor = new CompositeReflectionAccessor();
27     compositeAccessor.registerAccessor(new FieldReflectionAccessor(psiClass, elementFactory));
28     compositeAccessor.registerAccessor(new MethodReflectionAccessor(psiClass, elementFactory));
29     compositeAccessor.registerAccessor(new ConstructorReflectionAccessor(psiClass, elementFactory));
30     compositeAccessor.registerAccessor(new MethodReferenceReflectionAccessor(psiClass, elementFactory));
31
32     return compositeAccessor;
33   }
34
35   @Override
36   public void accessThroughReflection(@NotNull PsiElement element) {
37     for (ReflectionAccessor accessor : myAccessors) {
38       accessor.accessThroughReflection(element);
39     }
40   }
41 }