当前位置: 首页>>代码示例>>Java>>正文


Java PsiElementFactory.createMethodFromText方法代码示例

本文整理汇总了Java中com.intellij.psi.PsiElementFactory.createMethodFromText方法的典型用法代码示例。如果您正苦于以下问题:Java PsiElementFactory.createMethodFromText方法的具体用法?Java PsiElementFactory.createMethodFromText怎么用?Java PsiElementFactory.createMethodFromText使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.intellij.psi.PsiElementFactory的用法示例。


在下文中一共展示了PsiElementFactory.createMethodFromText方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: makePsiMethod

import com.intellij.psi.PsiElementFactory; //导入方法依赖的package包/类
private PsiMethod makePsiMethod( AbstractSrcMethod method, PsiClass psiClass )
{
  PsiElementFactory elementFactory = JavaPsiFacade.getInstance( psiClass.getProject() ).getElementFactory();
  StringBuilder sb = new StringBuilder();
  method.render( sb, 0 );
  try
  {
    return elementFactory.createMethodFromText( sb.toString(), psiClass );
  }
  catch( IncorrectOperationException ioe )
  {
    // the text of the method does not conform to method grammar, probably being edited in an IJ editor,
    // ignore these since the editor provides error information
    return null;
  }
}
 
开发者ID:manifold-systems,项目名称:manifold-ij,代码行数:17,代码来源:ManAugmentProvider.java

示例2: testQuickFix_classWithNullaryConstructor

import com.intellij.psi.PsiElementFactory; //导入方法依赖的package包/类
public void testQuickFix_classWithNullaryConstructor() {
  ProblemDescriptorImpl problemDescriptorMock = mock(ProblemDescriptorImpl.class);
  MockitoAnnotations.initMocks(this);

  PsiElementFactory factory =
      JavaPsiFacade.getInstance(myFixture.getProject()).getElementFactory();
  PsiClass psiClass = factory.createClass(getName());
  PsiMethod nullaryConstructor =
      factory.createMethodFromText("public " + psiClass.getName() + "() { }", psiClass);
  psiClass.addAfter(nullaryConstructor, null);

  ConstructorInspection constructorInspection = new ConstructorInspection();
  ConstructorInspection.MyQuickFix myQuickFix = constructorInspection.new MyQuickFix(psiClass);
  myQuickFix.applyFix(myFixture.getProject(), problemDescriptorMock);
  Assert.assertEquals(1, psiClass.getConstructors().length);
  Assert.assertTrue(EndpointUtilities.isPublicNullaryConstructor(psiClass.getConstructors()[0]));
}
 
开发者ID:GoogleCloudPlatform,项目名称:google-cloud-intellij,代码行数:18,代码来源:ConstructorInspectionTest.java

示例3: testQuickFix_classWithNonNullaryConstructor

import com.intellij.psi.PsiElementFactory; //导入方法依赖的package包/类
public void testQuickFix_classWithNonNullaryConstructor() {
  ProblemDescriptorImpl problemDescriptorMock = mock(ProblemDescriptorImpl.class);
  MockitoAnnotations.initMocks(this);

  PsiElementFactory factory =
      JavaPsiFacade.getInstance(myFixture.getProject()).getElementFactory();
  PsiClass psiClass = factory.createClass(getName());
  PsiMethod nullaryConstructor =
      factory.createMethodFromText(
          "public " + psiClass.getName() + "(String param) { }", psiClass);
  psiClass.addAfter(nullaryConstructor, null);

  ConstructorInspection constructorInspection = new ConstructorInspection();
  ConstructorInspection.MyQuickFix myQuickFix = constructorInspection.new MyQuickFix(psiClass);
  myQuickFix.applyFix(myFixture.getProject(), problemDescriptorMock);
  Assert.assertEquals(2, psiClass.getConstructors().length);
  Assert.assertTrue(EndpointUtilities.isPublicNullaryConstructor(psiClass.getConstructors()[0]));
}
 
开发者ID:GoogleCloudPlatform,项目名称:google-cloud-intellij,代码行数:19,代码来源:ConstructorInspectionTest.java

示例4: rebuildMethodFromString

import com.intellij.psi.PsiElementFactory; //导入方法依赖的package包/类
private PsiMethod rebuildMethodFromString() {
  final StringBuilder methodTextDeclaration = new StringBuilder();
  methodTextDeclaration.append(getAllModifierProperties((LightModifierList) getModifierList()));
  PsiType returnType = getReturnType();
  if (null != returnType) {
    methodTextDeclaration.append(returnType.getCanonicalText()).append(' ');
  }
  methodTextDeclaration.append(getName());
  methodTextDeclaration.append('(');
  if (getParameterList().getParametersCount() > 0) {
    for (PsiParameter parameter : getParameterList().getParameters()) {
      methodTextDeclaration.append(parameter.getType().getCanonicalText()).append(' ').append(parameter.getName()).append(',');
    }
    methodTextDeclaration.deleteCharAt(methodTextDeclaration.length() - 1);
  }
  methodTextDeclaration.append(')');
  methodTextDeclaration.append('{').append("  ").append('}');

  final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(getManager().getProject());

  final PsiMethod methodFromText = elementFactory.createMethodFromText(methodTextDeclaration.toString(), getContainingClass());
  if (null != getBody()) {
    methodFromText.getBody().replace(getBody());
  }
  return methodFromText;
}
 
开发者ID:mplushnikov,项目名称:lombok-intellij-plugin,代码行数:27,代码来源:LombokLightMethodBuilder.java

示例5: generateCopyConstructor

import com.intellij.psi.PsiElementFactory; //导入方法依赖的package包/类
private PsiMethod generateCopyConstructor(PsiClass psiClass, ClassMember[] copyableFields) {
	String parameterName = "other";

	StringBuilder code = new StringBuilder();
	code.append(String.format("public %s(%s %s) {", psiClass.getName(), psiClass.getName(), parameterName));

	boolean superclassHasCopyConstructor = ConstructorUtil.hasCopyConstructor(psiClass.getSuperClass());
	if (superclassHasCopyConstructor) {
		code.append(String.format("super(%s);", parameterName));
	}

	for (ClassMember fieldMember : copyableFields) {
		PsiField field = ((PsiFieldMember) fieldMember).getElement();
		String name = field.getName();
		code.append(String.format("this.%s = %s.%s;", name, parameterName, name));
	}
	code.append("}");

	PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(psiClass.getProject());
	PsiMethod constructor = elementFactory.createMethodFromText(code.toString(), psiClass);
	return constructor;
}
 
开发者ID:jkovacs,项目名称:CopyConstructorPlugin,代码行数:23,代码来源:GenerateCopyConstructorHandler.java

示例6: rebuildMethodFromString

import com.intellij.psi.PsiElementFactory; //导入方法依赖的package包/类
private PsiMethod rebuildMethodFromString()
{
  final StringBuilder builder = StringBuilderSpinAllocator.alloc();
  try
  {
    builder.append( getAllModifierProperties( (LightModifierList)getModifierList() ) );
    PsiType returnType = getReturnType();
    if( null != returnType )
    {
      builder.append( returnType.getCanonicalText() ).append( ' ' );
    }
    builder.append( getName() );
    builder.append( '(' );
    if( getParameterList().getParametersCount() > 0 )
    {
      for( PsiParameter parameter : getParameterList().getParameters() )
      {
        builder.append( parameter.getType().getCanonicalText() ).append( ' ' ).append( parameter.getName() ).append( ',' );
      }
      builder.deleteCharAt( builder.length() - 1 );
    }
    builder.append( ')' );
    builder.append( '{' ).append( "  " ).append( '}' );

    PsiElementFactory elementFactory = JavaPsiFacade.getInstance( getManager().getProject() ).getElementFactory();
    return elementFactory.createMethodFromText( builder.toString(), getContainingClass() );
  }
  finally
  {
    StringBuilderSpinAllocator.dispose( builder );
  }
}
 
开发者ID:manifold-systems,项目名称:manifold-ij,代码行数:33,代码来源:ManLightMethodBuilderImpl.java

示例7: applyFix

import com.intellij.psi.PsiElementFactory; //导入方法依赖的package包/类
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
  for (PsiMethod constructor : psiClass.getConstructors()) {
    if (EndpointUtilities.isPublicNullaryConstructor(constructor)) {
      return;
    }
  }

  PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
  PsiMethod newConstructor =
      factory.createMethodFromText("public " + psiClass.getName() + "() { }", psiClass);
  final PsiMethod[] psiMethods = psiClass.getMethods();
  PsiMethod firstMethod = (psiMethods.length == 0) ? null : psiMethods[0];
  psiClass.addBefore(newConstructor, firstMethod);
}
 
开发者ID:GoogleCloudPlatform,项目名称:google-cloud-intellij,代码行数:16,代码来源:ConstructorInspection.java

示例8: generate

import com.intellij.psi.PsiElementFactory; //导入方法依赖的package包/类
/**
 * 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()));
}
 
开发者ID:sockeqwe,项目名称:ParcelablePlease,代码行数:37,代码来源:CodeGenerator.java

示例9: createTypeParameterList

import com.intellij.psi.PsiElementFactory; //导入方法依赖的package包/类
@Nullable
public static PsiTypeParameterList createTypeParameterList(@NotNull PsiTypeParameterList psiTypeParameterList) {
  PsiTypeParameter[] psiTypeParameters = psiTypeParameterList.getTypeParameters();
  if (psiTypeParameters.length > 0) {

    final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(psiTypeParameterList.getProject());

    final StringBuilder builder = new StringBuilder("public <");

    for (PsiTypeParameter psiTypeParameter : psiTypeParameters) {
      builder.append(psiTypeParameter.getName());

      PsiClassType[] superTypes = psiTypeParameter.getExtendsListTypes();
      if (superTypes.length > 1 || superTypes.length == 1 && !superTypes[0].equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) {
        builder.append(" extends ");
        for (PsiClassType type : superTypes) {
          if (type.equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) {
            continue;
          }
          builder.append(type.getCanonicalText()).append('&');
        }
        builder.deleteCharAt(builder.length() - 1);
      }
      builder.append(',');
    }
    builder.deleteCharAt(builder.length() - 1);

    builder.append("> void foo(){}");

    PsiMethod methodFromText = elementFactory.createMethodFromText(builder.toString(), null);
    return methodFromText.getTypeParameterList();
  }
  return null;
}
 
开发者ID:mplushnikov,项目名称:lombok-intellij-plugin,代码行数:35,代码来源:PsiMethodUtil.java

示例10: findCreateComponentsMethod

import com.intellij.psi.PsiElementFactory; //导入方法依赖的package包/类
public static PsiMethod findCreateComponentsMethod(final PsiClass aClass)
{
	PsiElementFactory factory = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory();
	PsiMethod method;
	try
	{
		method = factory.createMethodFromText("void " + AsmCodeGenerator.CREATE_COMPONENTS_METHOD_NAME + "() {}", aClass);
	}
	catch(IncorrectOperationException e)
	{
		throw new RuntimeException(e);
	}
	return aClass.findMethodBySignature(method, true);
}
 
开发者ID:consulo,项目名称:consulo-ui-designer,代码行数:15,代码来源:FormEditingUtil.java


注:本文中的com.intellij.psi.PsiElementFactory.createMethodFromText方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。