Java Code Examples for com.sun.source.tree.ClassTree#getSimpleName()

The following examples show how to use com.sun.source.tree.ClassTree#getSimpleName() . 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: RenameConstructor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public List<Fix> run(CompilationInfo compilationInfo, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
    if (treePath.getLeaf().getKind() == Kind.METHOD) {
        MethodTree mt = (MethodTree) treePath.getLeaf();
        TreePath parentPath = treePath.getParentPath();
        ClassTree ct = (ClassTree) parentPath.getLeaf();
        Trees trees = compilationInfo.getTrees();
        Types types = compilationInfo.getTypes();
        TreeUtilities tu = compilationInfo.getTreeUtilities();
        TypeMirror type = types.erasure(trees.getTypeMirror(treePath));
        if (!Utilities.isValidType(type)) {
            return null;
        }
        for (Tree member : ct.getMembers()) {
            TreePath memberPath = new TreePath(parentPath, member);
            if (member.getKind() == Kind.METHOD && "<init>".contentEquals(((MethodTree)member).getName()) //NOI18N
                    && !tu.isSynthetic(memberPath) && types.isSameType(types.erasure(trees.getTypeMirror(memberPath)), type)) {
                return null;
            }
        }
        RenameConstructorFix fix = new RenameConstructorFix(compilationInfo.getSnapshot().getSource(), TreePathHandle.create(treePath, compilationInfo), offset, mt.getName(), ct.getSimpleName());
        return Collections.<Fix>singletonList(fix);
    }
    return null;
}
 
Example 2
Source File: ElementOverlay.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void enterClass(ClassTree ct) {
    if (ct.getSimpleName() == null || ct.getSimpleName().length() == 0 || anonymousCounter > 0) {
        anonymousCounter++;
    } else {
        if (fqn.length() > 0) fqn.append('.');
        fqn.append(ct.getSimpleName());
    }
}
 
Example 3
Source File: GenerationUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public ClassTree ensureNoArgConstructor(ClassTree classTree) {
    TypeElement typeElement = SourceUtils.classTree2TypeElement(copy, classTree);
    if (typeElement == null) {
        throw new IllegalArgumentException("No TypeElement for ClassTree " + classTree.getSimpleName());
    }
    ExecutableElement constructor = SourceUtils.getNoArgConstructor(copy, typeElement);
    MethodTree constructorTree = constructor != null ? copy.getTrees().getTree(constructor) : null;
    MethodTree newConstructorTree = null;
    TreeMaker make = getTreeMaker();
    if (constructor != null) {
        if (!constructor.getModifiers().contains(Modifier.PUBLIC)) {
            ModifiersTree oldModifiersTree = constructorTree.getModifiers();
            Set newModifiers = EnumSet.of(Modifier.PUBLIC);
       //     for (Modifier modifier : oldModifiersTree.getFlags()) {
         //       if (!Modifier.PROTECTED.equals(modifier) && !Modifier.PRIVATE.equals(modifier)) {
           //         newModifiers.add(modifier);
             //   }
            //}
            newConstructorTree = make.Constructor(
                make.Modifiers(newModifiers),
                constructorTree.getTypeParameters(),
                constructorTree.getParameters(),
                constructorTree.getThrows(),
                constructorTree.getBody());
        }
    } else {
        newConstructorTree = make.Constructor(
                createModifiers(Modifier.PUBLIC),
                Collections.<TypeParameterTree>emptyList(),
                Collections.<VariableTree>emptyList(),
                Collections.<ExpressionTree>emptyList(),
                "{ }"); // NOI18N
    }
    ClassTree newClassTree = classTree;
    if (newConstructorTree != null) {
        if (constructorTree != null) {
            newClassTree = make.removeClassMember(newClassTree, constructorTree);
        }
        newClassTree = make.addClassMember(newClassTree, newConstructorTree);
    }
    return newClassTree;
}
 
Example 4
Source File: GenerationUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Ensures the given class has a public no-arg constructor.
 *
 * @param  classTree the class to ensure the constructor for; cannot be null.
 * @return a modified class if a no-arg constructor was added, the original
 *         class otherwise; never null.
 */
public ClassTree ensureNoArgConstructor(ClassTree classTree) {
    TypeElement typeElement = SourceUtils.classTree2TypeElement(copy, classTree);
    if (typeElement == null) {
        throw new IllegalArgumentException("No TypeElement for ClassTree " + classTree.getSimpleName());
    }
    ExecutableElement constructor = SourceUtils.getNoArgConstructor(copy, typeElement);
    MethodTree constructorTree = constructor != null ? copy.getTrees().getTree(constructor) : null;
    MethodTree newConstructorTree = null;
    TreeMaker make = getTreeMaker();
    if (constructor != null) {
        if (!constructor.getModifiers().contains(Modifier.PUBLIC)) {
            ModifiersTree oldModifiersTree = constructorTree.getModifiers();
            Set<Modifier> newModifiers = EnumSet.of(Modifier.PUBLIC);
            for (Modifier modifier : oldModifiersTree.getFlags()) {
                if (!Modifier.PROTECTED.equals(modifier) && !Modifier.PRIVATE.equals(modifier)) {
                    newModifiers.add(modifier);
                }
            }
            newConstructorTree = make.Constructor(
                make.Modifiers(newModifiers),
                constructorTree.getTypeParameters(),
                constructorTree.getParameters(),
                constructorTree.getThrows(),
                constructorTree.getBody());
        }
    } else {
        newConstructorTree = make.Constructor(
                createModifiers(Modifier.PUBLIC),
                Collections.<TypeParameterTree>emptyList(),
                Collections.<VariableTree>emptyList(),
                Collections.<ExpressionTree>emptyList(),
                "{ }"); // NOI18N
    }
    ClassTree newClassTree = classTree;
    if (newConstructorTree != null) {
        if (constructorTree != null) {
            newClassTree = make.removeClassMember(newClassTree, constructorTree);
        }
        newClassTree = make.addClassMember(newClassTree, newConstructorTree);
    }
    return newClassTree;
}
 
Example 5
Source File: GenerationUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Ensures the given class has a public no-arg constructor.
 *
 * @param  classTree the class to ensure the constructor for; cannot be null.
 * @return a modified class if a no-arg constructor was added, the original
 *         class otherwise; never null.
 */
public ClassTree ensureNoArgConstructor(ClassTree classTree) {
    TypeElement typeElement = SourceUtils.classTree2TypeElement(copy, classTree);
    if (typeElement == null) {
        throw new IllegalArgumentException("No TypeElement for ClassTree " + classTree.getSimpleName());
    }
    ExecutableElement constructor = SourceUtils.getNoArgConstructor(copy, typeElement);
    MethodTree constructorTree = constructor != null ? copy.getTrees().getTree(constructor) : null;
    MethodTree newConstructorTree = null;
    TreeMaker make = getTreeMaker();
    if (constructor != null) {
        if (!constructor.getModifiers().contains(Modifier.PUBLIC)) {
            ModifiersTree oldModifiersTree = constructorTree.getModifiers();
            Set<Modifier> newModifiers = EnumSet.of(Modifier.PUBLIC);
            for (Modifier modifier : oldModifiersTree.getFlags()) {
                if (!Modifier.PROTECTED.equals(modifier) && !Modifier.PRIVATE.equals(modifier)) {
                    newModifiers.add(modifier);
                }
            }
            newConstructorTree = make.Constructor(
                make.Modifiers(newModifiers),
                constructorTree.getTypeParameters(),
                constructorTree.getParameters(),
                constructorTree.getThrows(),
                constructorTree.getBody());
        }
    } else {
        newConstructorTree = make.Constructor(
                createModifiers(Modifier.PUBLIC),
                Collections.<TypeParameterTree>emptyList(),
                Collections.<VariableTree>emptyList(),
                Collections.<ExpressionTree>emptyList(),
                "{ }"); // NOI18N
    }
    ClassTree newClassTree = classTree;
    if (newConstructorTree != null) {
        if (constructorTree != null) {
            newClassTree = make.removeClassMember(newClassTree, constructorTree);
        }
        newClassTree = make.addClassMember(newClassTree, newConstructorTree);
    }
    return newClassTree;
}