コード例 #1
0
  @NotNull
  @Override
  public PsiMethod createMethod(@NotNull final String name, final PsiType returnType)
      throws IncorrectOperationException {
    PsiUtil.checkIsIdentifier(myManager, name);
    if (PsiType.NULL.equals(returnType)) {
      throw new IncorrectOperationException("Cannot create method with type \"null\".");
    }

    final String canonicalText = returnType.getCanonicalText();
    final PsiJavaFile aFile =
        createDummyJavaFile("class _Dummy_ { public " + canonicalText + " " + name + "() {} }");
    final PsiClass[] classes = aFile.getClasses();
    if (classes.length < 1) {
      throw new IncorrectOperationException(
          "Class was not created. Method name: " + name + "; return type: " + canonicalText);
    }
    final PsiMethod[] methods = classes[0].getMethods();
    if (methods.length < 1) {
      throw new IncorrectOperationException(
          "Method was not created. Method name: " + name + "; return type: " + canonicalText);
    }
    PsiMethod method = methods[0];
    method =
        (PsiMethod)
            JavaCodeStyleManager.getInstance(myManager.getProject()).shortenClassReferences(method);
    return (PsiMethod) CodeStyleManager.getInstance(myManager.getProject()).reformat(method);
  }
コード例 #2
0
  @NotNull
  @Override
  public PsiField createField(@NotNull final String name, @NotNull final PsiType type)
      throws IncorrectOperationException {
    PsiUtil.checkIsIdentifier(myManager, name);
    if (PsiType.NULL.equals(type)) {
      throw new IncorrectOperationException("Cannot create field with type \"null\".");
    }

    @NonNls
    final String text = "class _Dummy_ { private " + type.getCanonicalText() + " " + name + "; }";
    final PsiJavaFile aFile = createDummyJavaFile(text);
    final PsiClass[] classes = aFile.getClasses();
    if (classes.length < 1) {
      throw new IncorrectOperationException("Class was not created " + text);
    }
    final PsiClass psiClass = classes[0];
    final PsiField[] fields = psiClass.getFields();
    if (fields.length < 1) {
      throw new IncorrectOperationException("Field was not created " + text);
    }
    PsiField field = fields[0];
    field =
        (PsiField)
            JavaCodeStyleManager.getInstance(myManager.getProject()).shortenClassReferences(field);
    return (PsiField) CodeStyleManager.getInstance(myManager.getProject()).reformat(field);
  }
コード例 #3
0
 @NotNull
 @Override
 public PsiClassInitializer createClassInitializer() throws IncorrectOperationException {
   final PsiJavaFile aFile = createDummyJavaFile("class _Dummy_ { {} }");
   final PsiClassInitializer classInitializer = aFile.getClasses()[0].getInitializers()[0];
   return (PsiClassInitializer)
       CodeStyleManager.getInstance(myManager.getProject()).reformat(classInitializer);
 }
コード例 #4
0
 @NotNull
 @Override
 public PsiMethod createConstructor(@NotNull @NonNls final String name) {
   final PsiJavaFile aFile =
       createDummyJavaFile("class " + name + " { public " + name + "() {} }");
   final PsiMethod method = aFile.getClasses()[0].getMethods()[0];
   return (PsiMethod) CodeStyleManager.getInstance(myManager.getProject()).reformat(method);
 }
コード例 #5
0
 private static PsiImportStatementBase extractImport(
     final PsiJavaFile aFile, final boolean isStatic) {
   final PsiImportList importList = aFile.getImportList();
   assert importList != null : aFile;
   final PsiImportStatementBase[] statements =
       isStatic ? importList.getImportStaticStatements() : importList.getImportStatements();
   assert statements.length == 1 : aFile.getText();
   return statements[0];
 }
コード例 #6
0
 private PsiClass createClassInner(@NonNls final String type, @NonNls String name) {
   PsiUtil.checkIsIdentifier(myManager, name);
   final PsiJavaFile aFile = createDummyJavaFile("public " + type + " " + name + " { }");
   final PsiClass[] classes = aFile.getClasses();
   if (classes.length != 1) {
     throw new IncorrectOperationException("Incorrect " + type + " name \"" + name + "\".");
   }
   return classes[0];
 }
コード例 #7
0
 @NotNull
 @Override
 public PsiPackageStatement createPackageStatement(@NotNull final String name)
     throws IncorrectOperationException {
   final PsiJavaFile aFile = createDummyJavaFile("package " + name + ";");
   final PsiPackageStatement stmt = aFile.getPackageStatement();
   if (stmt == null) {
     throw new IncorrectOperationException("Incorrect package name: " + name);
   }
   return stmt;
 }