<commit_before>/*
 * Copyright (c) 2005 Your Corporation. All Rights Reserved.
 */
package com.intellij.util.xml.ui;

import com.intellij.psi.PsiClass;
import com.intellij.util.xml.GenericValue;
import com.intellij.util.xml.DomElement;
import com.intellij.util.xml.DomMethodsInfo;
import com.intellij.util.xml.impl.ui.BooleanControl;
import com.intellij.util.xml.impl.ui.PsiClassControl;
import com.intellij.util.xml.impl.ui.StringControl;
import com.intellij.util.xml.impl.ui.CollectionControl;
import com.intellij.openapi.diagnostic.Logger;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.InvocationTargetException;

/**
 * @author peter
 */
public class DomUIFactory {
  private static final Logger LOG = Logger.getInstance("#com.intellij.util.xml.ui.DomUIFactory");

  public static <T> DomUIControl createControl(GenericValue<T> element, Class<T> aClass) {
    try {
      final Method getValueMethod = aClass.getMethod("getValue");
      final Method setValueMethod = aClass.getMethod("setValue", aClass);
      final Method getStringMethod = aClass.getMethod("getStringValue");
      final Method setStringMethod = aClass.getMethod("setStringValue", aClass);
      if (aClass.equals(boolean.class) || aClass.equals(Boolean.class)) {
        return new BooleanControl(element, getValueMethod, setValueMethod);
      } else if (aClass.equals(String.class)) {
        return new StringControl(element, getValueMethod, setValueMethod);
      } else if (aClass.equals(PsiClass.class)) {
        return new PsiClassControl(element, getStringMethod, setStringMethod);
      }
      throw new IllegalArgumentException("Not supported: " + aClass);
    }
    catch (NoSuchMethodException e) {
      throw new RuntimeException(e);
    }
  }

  public static DomUIControl createControl(DomElement parent, Method genericValueGetterMethod) {
    final Type genericReturnType = genericValueGetterMethod.getGenericReturnType();
    assert genericReturnType instanceof ParameterizedType;
    assert ((ParameterizedType) genericReturnType).getRawType().equals(GenericValue.class);

    try {
      return createControl((GenericValue)genericValueGetterMethod.invoke(parent), (Class)((ParameterizedType) genericReturnType).getActualTypeArguments()[0]);
    }
    catch (IllegalAccessException e) {
      LOG.error(e);
    }
    catch (InvocationTargetException e) {
      LOG.error(e);
    }
  }

  public static <T extends DomElement> CollectionControl<T> createCollectionControl(DomElement element, Class<T> aClass, String tagName) {
    final DomMethodsInfo methodsInfo = element.getMethodsInfo();
    final Method addMethod = methodsInfo.getCollectionAddMethod(tagName);
    assert addMethod != null;
    final Method getMethod = methodsInfo.getCollectionGetMethod(tagName);
    return new CollectionControl<T>(element, getMethod, addMethod);
  }

}<commit_msg>compile fix<commit_after>/*
 * Copyright (c) 2005 Your Corporation. All Rights Reserved.
 */
package com.intellij.util.xml.ui;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.PsiClass;
import com.intellij.util.xml.DomElement;
import com.intellij.util.xml.DomMethodsInfo;
import com.intellij.util.xml.GenericValue;
import com.intellij.util.xml.impl.ui.BooleanControl;
import com.intellij.util.xml.impl.ui.CollectionControl;
import com.intellij.util.xml.impl.ui.PsiClassControl;
import com.intellij.util.xml.impl.ui.StringControl;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author peter
 */
public class DomUIFactory {
  private static final Logger LOG = Logger.getInstance("#com.intellij.util.xml.ui.DomUIFactory");

  public static <T> DomUIControl createControl(GenericValue<T> element, Class<T> aClass) {
    try {
      final Method getValueMethod = aClass.getMethod("getValue");
      final Method setValueMethod = aClass.getMethod("setValue", aClass);
      final Method getStringMethod = aClass.getMethod("getStringValue");
      final Method setStringMethod = aClass.getMethod("setStringValue", aClass);
      if (aClass.equals(boolean.class) || aClass.equals(Boolean.class)) {
        return new BooleanControl(element, getValueMethod, setValueMethod);
      } else if (aClass.equals(String.class)) {
        return new StringControl(element, getValueMethod, setValueMethod);
      } else if (aClass.equals(PsiClass.class)) {
        return new PsiClassControl(element, getStringMethod, setStringMethod);
      }
      throw new IllegalArgumentException("Not supported: " + aClass);
    }
    catch (NoSuchMethodException e) {
      throw new RuntimeException(e);
    }
  }

  public static DomUIControl createControl(DomElement parent, Method genericValueGetterMethod) {
    final Type genericReturnType = genericValueGetterMethod.getGenericReturnType();
    assert genericReturnType instanceof ParameterizedType;
    assert ((ParameterizedType) genericReturnType).getRawType().equals(GenericValue.class);

    try {
      return createControl((GenericValue)genericValueGetterMethod.invoke(parent), (Class)((ParameterizedType) genericReturnType).getActualTypeArguments()[0]);
    }
    catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  public static <T extends DomElement> CollectionControl<T> createCollectionControl(DomElement element, Class<T> aClass, String tagName) {
    final DomMethodsInfo methodsInfo = element.getMethodsInfo();
    final Method addMethod = methodsInfo.getCollectionAddMethod(tagName);
    final Method getMethod = methodsInfo.getCollectionGetMethod(tagName);
    return new CollectionControl<T>(element, getMethod, addMethod);
  }

}
{'fraction_non_alphanumeric': 0.10439944134078212, 'fraction_numerical': 0.0017458100558659217, 'mean_word_length': 4.816243654822335, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5210559', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1623, 'n_words': 340}