Java Code Examples for com.intellij.psi.JavaPsiFacade#getElementFactory()

The following examples show how to use com.intellij.psi.JavaPsiFacade#getElementFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MethodChainLookupElement.java    From litho with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static PsiExpression createMethodChain(
    Project project, String firstName, List<? extends String> methodNames) {
  PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
  StringBuilder expressionText =
      new StringBuilder(firstName + "(" + TEMPLATE_INSERT_PLACEHOLDER_C + ")");
  methodNames.forEach(
      methodName ->
          expressionText
              .append("\n.")
              .append(methodName)
              .append("(")
              .append(TEMPLATE_INSERT_PLACEHOLDER)
              .append(")"));
  return factory.createExpressionFromText(expressionText.toString(), null);
}
 
Example 2
Source File: OnEventGenerateUtils.java    From litho with Apache License 2.0 6 votes vote down vote up
/**
 * Adds comment to the given method "// An event handler ContextClassName.methodName(c,
 * parameterName)
 */
public static void addComment(PsiClass contextClass, PsiMethod method) {
  final Project project = contextClass.getProject();
  final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);

  final StringBuilder builder =
      new StringBuilder("// An event handler ")
          .append(LithoPluginUtils.getLithoComponentNameFromSpec(contextClass.getName()))
          .append(".")
          .append(method.getName())
          .append("(")
          .append(CONTEXT_PARAMETER_NAME);
  for (PsiParameter parameter : method.getParameterList().getParameters()) {
    if (LithoPluginUtils.isParam(parameter)) {
      builder.append(", ").append(parameter.getName());
    }
  }

  builder.append(")");
  final PsiComment comment = factory.createCommentFromText(builder.toString(), method);
  method.addBefore(comment, method.getModifierList());
}
 
Example 3
Source File: TemplateService.java    From litho with Apache License 2.0 5 votes vote down vote up
private PsiMethod readMethodTemplate(String targetName, Project project) {
  // TemplateSettings#loadDefaultLiveTemplates
  PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
  return Optional.ofNullable(
          DecodeDefaultsUtil.getDefaultsInputStream(this, "methodTemplates/methods"))
      .map(TemplateService::load)
      .filter(element -> TAG_ROOT.equals(element.getName()))
      .map(element -> element.getChild(targetName))
      .map(template -> template.getAttributeValue("method"))
      .map(method -> factory.createMethodFromText(method, null))
      .orElse(null);
}
 
Example 4
Source File: BeanUtils.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
public List<ReferenceableBeanId> findReferenceableBeanIdsByType(Module module,
                                                                Predicate<String> idCondition,
                                                                PsiType expectedBeanType) {
    PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(module.getProject());
    return findReferenceableBeanIds(module, idCondition).stream()
            .filter(ref -> {
                PsiClass psiClass = resolveToPsiClass(ref);
                if (psiClass != null) {
                    PsiClassType beanType = elementFactory.createType(psiClass);
                    return expectedBeanType.isAssignableFrom(beanType);
                }
                return false;
            })
            .collect(Collectors.toList());
}
 
Example 5
Source File: DataWriter.java    From GsonFormat with Apache License 2.0 5 votes vote down vote up
public DataWriter(PsiFile file, Project project, PsiClass cls) {
    super(project, file);
    factory = JavaPsiFacade.getElementFactory(project);
    this.file = file;
    this.project = project;
    this.cls = cls;
}
 
Example 6
Source File: CodeGenerator.java    From ParcelablePlease with Apache License 2.0 5 votes vote down vote up
/**
 * Generate and insert the Parcel and ParcelablePlease code
 */
public void generate() {

  PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(psiClass.getProject());
  JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(psiClass.getProject());

  // Clear any previous
  clearPrevious();

  // Implements parcelable
  makeClassImplementParcelable(elementFactory, styleManager);

  // @ParcelablePlease Annotation
  addAnnotation(elementFactory, styleManager);

  // Creator
  PsiField creatorField = elementFactory.createFieldFromText(generateCreator(), psiClass);

  // Describe contents method
  PsiMethod describeContentsMethod =
      elementFactory.createMethodFromText(generateDescribeContents(), psiClass);

  // Method for writing to the parcel
  PsiMethod writeToParcelMethod =
      elementFactory.createMethodFromText(generateWriteToParcel(), psiClass);

  styleManager.shortenClassReferences(
      psiClass.addBefore(describeContentsMethod, psiClass.getLastChild()));

  styleManager.shortenClassReferences(
      psiClass.addBefore(writeToParcelMethod, psiClass.getLastChild()));

  styleManager.shortenClassReferences(psiClass.addBefore(creatorField, psiClass.getLastChild()));
}
 
Example 7
Source File: LombokLightModifierList.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
@NotNull
public PsiAnnotation addAnnotation(@NotNull @NonNls String qualifiedName) {
  final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(getProject());
  final PsiAnnotation psiAnnotation = elementFactory.createAnnotationFromText('@' + qualifiedName, null);
  myAnnotations.put(qualifiedName, psiAnnotation);
  return psiAnnotation;
}
 
Example 8
Source File: LombokToStringHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void processClass(@NotNull PsiClass psiClass) {
  final PsiElementFactory factory = JavaPsiFacade.getElementFactory(psiClass.getProject());
  final PsiClassType stringClassType = factory.createTypeByFQClassName(CommonClassNames.JAVA_LANG_STRING, psiClass.getResolveScope());

  final PsiMethod toStringMethod = findPublicNonStaticMethod(psiClass, "toString", stringClassType);
  if (null != toStringMethod) {
    toStringMethod.delete();
  }
  addAnnotation(psiClass, ToString.class);
}
 
Example 9
Source File: PsiQuickFixFactory.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static LocalQuickFix createAddAnnotationQuickFix(@NotNull PsiClass psiClass, @NotNull String annotationFQN, @Nullable String annotationParam) {
  PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(psiClass.getProject());
  PsiAnnotation newAnnotation = elementFactory.createAnnotationFromText("@" + annotationFQN + "(" + StringUtil.notNullize(annotationParam) + ")", psiClass);
  final PsiNameValuePair[] attributes = newAnnotation.getParameterList().getAttributes();

  return new AddAnnotationFix(annotationFQN, psiClass, attributes);
}
 
Example 10
Source File: BeanReferenceTypeAnnotator.java    From camel-idea-plugin with Apache License 2.0 4 votes vote down vote up
private boolean isAssignableFrom(PsiType type, PsiClass value) {
    PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(value.getProject());
    PsiClassType valueType = elementFactory.createType(value);
    return type.isAssignableFrom(valueType);
}
 
Example 11
Source File: LombokEnumConstantBuilder.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@NotNull
@Override
public PsiEnumConstantInitializer getOrCreateInitializingClass() {
  final PsiElementFactory factory = JavaPsiFacade.getElementFactory(getProject());
  return factory.createEnumConstantFromText("foo{}", null).getInitializingClass();
}
 
Example 12
Source File: PsiMethodUtil.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@NotNull
public static PsiCodeBlock createCodeBlockFromText(@NotNull String blockText, @NotNull PsiElement psiElement) {
  final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(psiElement.getProject());
  return elementFactory.createCodeBlockFromText("{" + blockText + "}", psiElement);
}