@SuppressWarnings("unchecked") protected Annotation getAnnotation(TypeDeclaration td, String annotation) { return getAnnotation(td.modifiers(), annotation); }
/** * Set the abstract flag for this class. */ public void setAbstract() { if (m_class instanceof TypeDeclaration) { Modifier modifier = m_source.getAST().newModifier(Modifier.ModifierKeyword.ABSTRACT_KEYWORD); ((TypeDeclaration)m_class).modifiers().add(modifier); } else { // should not be possible, but just in case of added types in future throw new IllegalStateException("Internal error - abstract not supported for class type"); } }
@SuppressWarnings("unchecked") public static Consumer<TypeDeclaration> make(final ModifierKeyword... keywords) { return td -> { final AST ast = td.getAST(); for (final ModifierKeyword keyword : keywords) { td.modifiers().add(ast.newModifier(keyword)); } }; }
@SuppressWarnings(UNCHECKED) private static List<Object> getClassAnnotations(final Path path) { final CompilationUnit unit = getCompilationUnit(path); List<Object> modifiers = null; if (unit.types().size() > 0) { final TypeDeclaration classType = (TypeDeclaration) unit.types().get(0); modifiers = classType.modifiers(); } if (modifiers == null) { return Collections.emptyList(); } return modifiers; }
@SuppressWarnings(UNCHECKED) private static List<Object> getClassAnnotations(final Path path) { final CompilationUnit unit = getCompilationUnit(path); List<Object> modifiers = null; if (unit.types().size() > 0) { final TypeDeclaration classType = (TypeDeclaration) unit.types().get(0); modifiers = classType.modifiers(); } if (modifiers == null) { return Collections.emptyList(); } return modifiers; }
public boolean isNotSupportedInnerType(final TypeDeclaration it) { return (((!it.isInterface()) && ((it.getParent() instanceof TypeDeclaration) || (it.getParent() instanceof Block))) && (!IterableExtensions.<Modifier>exists(Iterables.<Modifier>filter(it.modifiers(), Modifier.class), ((Function1<Modifier, Boolean>) (Modifier it_1) -> { return Boolean.valueOf(it_1.isStatic()); })))); }
private void createParameterClass(MethodDeclaration methodDeclaration, CompilationUnitRewrite cuRewrite) throws CoreException { if (fCreateAsTopLevel) { IPackageFragmentRoot root= (IPackageFragmentRoot) cuRewrite.getCu().getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); fOtherChanges.addAll(fParameterObjectFactory.createTopLevelParameterObject(root)); } else { ASTRewrite rewriter= cuRewrite.getASTRewrite(); TypeDeclaration enclosingType= (TypeDeclaration) methodDeclaration.getParent(); ListRewrite bodyRewrite= rewriter.getListRewrite(enclosingType, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); String fqn= enclosingType.getName().getFullyQualifiedName(); TypeDeclaration classDeclaration= fParameterObjectFactory.createClassDeclaration(fqn, cuRewrite, null); classDeclaration.modifiers().add(rewriter.getAST().newModifier(ModifierKeyword.PUBLIC_KEYWORD)); classDeclaration.modifiers().add(rewriter.getAST().newModifier(ModifierKeyword.STATIC_KEYWORD)); bodyRewrite.insertBefore(classDeclaration, methodDeclaration, null); } }
private void createParameterClass(MethodDeclaration methodDeclaration, CompilationUnitRewrite cuRewrite) throws CoreException { if (fCreateAsTopLevel) { IPackageFragmentRoot root= (IPackageFragmentRoot) cuRewrite.getCu().getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); fOtherChanges.addAll(fParameterObjectFactory.createTopLevelParameterObject(root)); } else { ASTRewrite rewriter= cuRewrite.getASTRewrite(); TypeDeclaration enclosingType= (TypeDeclaration) methodDeclaration.getParent(); ContextSensitiveImportRewriteContext context=new ContextSensitiveImportRewriteContext(enclosingType, cuRewrite.getImportRewrite()); ListRewrite bodyRewrite= rewriter.getListRewrite(enclosingType, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); String fqn= enclosingType.getName().getFullyQualifiedName(); TypeDeclaration classDeclaration= fParameterObjectFactory.createClassDeclaration(fqn, cuRewrite, null, context); classDeclaration.modifiers().add(rewriter.getAST().newModifier(ModifierKeyword.PUBLIC_KEYWORD)); classDeclaration.modifiers().add(rewriter.getAST().newModifier(ModifierKeyword.STATIC_KEYWORD)); bodyRewrite.insertBefore(classDeclaration, methodDeclaration, null); } }
final List internalGetChildListProperty(ChildListPropertyDescriptor property) { if (property == MODIFIERS2_PROPERTY) { return modifiers(); } if (property == TYPE_PARAMETERS_PROPERTY) { return typeParameters(); } if (property == SUPER_INTERFACES_PROPERTY) { return superInterfaces(); } if (property == SUPER_INTERFACE_TYPES_PROPERTY) { return superInterfaceTypes(); } if (property == BODY_DECLARATIONS_PROPERTY) { return bodyDeclarations(); } // allow default implementation to flag the error return super.internalGetChildListProperty(property); }
final List internalGetChildListProperty(ChildListPropertyDescriptor property) { if (property == MODIFIERS2_PROPERTY) { return modifiers(); } if (property == TYPE_PARAMETERS_PROPERTY) { return typeParameters(); } if (property == SUPER_INTERFACES_PROPERTY) { return superInterfaces(); } if (property == SUPER_INTERFACE_TYPES_PROPERTY) { return superInterfaceTypes(); } if (property == BODY_DECLARATIONS_PROPERTY) { return bodyDeclarations(); } // allow default implementation to flag the error return super.internalGetChildListProperty(property); }
final List internalGetChildListProperty(ChildListPropertyDescriptor property) { if (property == MODIFIERS2_PROPERTY) { return modifiers(); } if (property == TYPE_PARAMETERS_PROPERTY) { return typeParameters(); } if (property == SUPER_INTERFACES_PROPERTY) { return superInterfaces(); } if (property == SUPER_INTERFACE_TYPES_PROPERTY) { return superInterfaceTypes(); } if (property == BODY_DECLARATIONS_PROPERTY) { return bodyDeclarations(); } // allow default implementation to flag the error return super.internalGetChildListProperty(property); }
@Override final List internalGetChildListProperty(ChildListPropertyDescriptor property) { if (property == MODIFIERS2_PROPERTY) { return modifiers(); } if (property == TYPE_PARAMETERS_PROPERTY) { return typeParameters(); } if (property == SUPER_INTERFACES_PROPERTY) { return superInterfaces(); } if (property == SUPER_INTERFACE_TYPES_PROPERTY) { return superInterfaceTypes(); } if (property == BODY_DECLARATIONS_PROPERTY) { return bodyDeclarations(); } // allow default implementation to flag the error return super.internalGetChildListProperty(property); }
final List internalGetChildListProperty(ChildListPropertyDescriptor property) { if (property == MODIFIERS2_PROPERTY) { return modifiers(); } if (property == TYPE_PARAMETERS_PROPERTY) { return typeParameters(); } if (property == SUPER_INTERFACES_PROPERTY) { return superInterfaces(); } if (property == SUPER_INTERFACE_TYPES_PROPERTY) { return superInterfaceTypes(); } if (property == BODY_DECLARATIONS_PROPERTY) { return bodyDeclarations(); } // allow default implementation to flag the error return super.internalGetChildListProperty(property); }
final List internalGetChildListProperty(ChildListPropertyDescriptor property) { if (property == MODIFIERS2_PROPERTY) { return modifiers(); } if (property == TYPE_PARAMETERS_PROPERTY) { return typeParameters(); } if (property == SUPER_INTERFACES_PROPERTY) { return superInterfaces(); } if (property == SUPER_INTERFACE_TYPES_PROPERTY) { return superInterfaceTypes(); } if (property == BODY_DECLARATIONS_PROPERTY) { return bodyDeclarations(); } // allow default implementation to flag the error return super.internalGetChildListProperty(property); }
private List<ResourceChange> createParameterObject(ParameterObjectFactory pof, IPackageFragmentRoot packageRoot) throws CoreException { FieldUpdate fieldUpdate= new FieldUpdate(); if (fDescriptor.isCreateTopLevel()) return pof.createTopLevelParameterObject(packageRoot, fieldUpdate); else { CompilationUnit root= fBaseCURewrite.getRoot(); TypeDeclaration typeDecl= ASTNodeSearchUtil.getTypeDeclarationNode(fDescriptor.getType(), root); ASTRewrite rewrite= fBaseCURewrite.getASTRewrite(); ListRewrite listRewrite= rewrite.getListRewrite(typeDecl, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); TypeDeclaration paramClass= pof.createClassDeclaration(typeDecl.getName().getFullyQualifiedName(), fBaseCURewrite, fieldUpdate); paramClass.modifiers().add(rewrite.getAST().newModifier(ModifierKeyword.PUBLIC_KEYWORD)); if (shouldParamClassBeStatic(typeDecl)) { paramClass.modifiers().add(rewrite.getAST().newModifier(ModifierKeyword.STATIC_KEYWORD)); } listRewrite.insertFirst(paramClass, fBaseCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractClassRefactoring_group_insert_parameter)); return new ArrayList<>(); //Change will be generated later for fBaseCURewrite } }
private List<ResourceChange> createParameterObject(ParameterObjectFactory pof, IPackageFragmentRoot packageRoot) throws CoreException { FieldUpdate fieldUpdate= new FieldUpdate(); if (fDescriptor.isCreateTopLevel()) return pof.createTopLevelParameterObject(packageRoot, fieldUpdate); else { CompilationUnit root= fBaseCURewrite.getRoot(); TypeDeclaration typeDecl= ASTNodeSearchUtil.getTypeDeclarationNode(fDescriptor.getType(), root); ASTRewrite rewrite= fBaseCURewrite.getASTRewrite(); ListRewrite listRewrite= rewrite.getListRewrite(typeDecl, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); ContextSensitiveImportRewriteContext context= new ContextSensitiveImportRewriteContext(typeDecl, fBaseCURewrite.getImportRewrite()); TypeDeclaration paramClass= pof.createClassDeclaration(typeDecl.getName().getFullyQualifiedName(), fBaseCURewrite, fieldUpdate, context); paramClass.modifiers().add(rewrite.getAST().newModifier(ModifierKeyword.PUBLIC_KEYWORD)); if (shouldParamClassBeStatic(typeDecl)) { paramClass.modifiers().add(rewrite.getAST().newModifier(ModifierKeyword.STATIC_KEYWORD)); } listRewrite.insertFirst(paramClass, fBaseCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractClassRefactoring_group_insert_parameter)); return new ArrayList<>(); //Change will be generated later for fBaseCURewrite } }
private void createParameterClass(MethodDeclaration methodDeclaration, CompilationUnitRewrite cuRewrite) throws CoreException { if (fCreateAsTopLevel) { IJavaProject javaProject= fCompilationUnit.getJavaProject(); IPackageFragment packageFragment= getPackageFragmentRoot().getPackageFragment(fParameterObjectFactory.getPackage()); if (!packageFragment.exists()) { fOtherChanges.add(new CreatePackageChange(packageFragment)); } ICompilationUnit unit= packageFragment.getCompilationUnit(fParameterObjectFactory.getClassName() + ".java"); //$NON-NLS-1$ Assert.isTrue(!unit.exists()); createTopLevelParameterObject(javaProject, unit); } else { ASTRewrite rewriter= cuRewrite.getASTRewrite(); TypeDeclaration enclosingType= (TypeDeclaration) methodDeclaration.getParent(); ListRewrite bodyRewrite= rewriter.getListRewrite(enclosingType, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); TypeDeclaration classDeclaration= fParameterObjectFactory.createClassDeclaration(fCompilationUnit, enclosingType.getName() .getFullyQualifiedName(), cuRewrite); classDeclaration.modifiers().add(rewriter.getAST().newModifier(ModifierKeyword.PUBLIC_KEYWORD)); classDeclaration.modifiers().add(rewriter.getAST().newModifier(ModifierKeyword.STATIC_KEYWORD)); bodyRewrite.insertBefore(classDeclaration, methodDeclaration, null); } }
@SuppressWarnings("unchecked") @Test public void testGenerateClassWithTemplate() { TemplateSignature templateSignature = mock(TemplateSignature.class); EList<TemplateParameter> templateParameters = mock(EList.class, Answers.RETURNS_DEEP_STUBS.get()); when(clazz.getOwnedTemplateSignature()).thenReturn(templateSignature); when(templateSignature.getParameters()).thenReturn(templateParameters); AST ast = AST.newAST(AST.JLS3); CompilationUnit cu = ast.newCompilationUnit(); TypeDeclaration td = ast.newTypeDeclaration(); td.setInterface(true); Modifier modifier = ast .newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD); td.modifiers().add(modifier); td.setName(ast.newSimpleName("Company")); TypeDeclaration typeDeclaration = interfaceGenerator.generateClass( clazz, ast, cu); assertEquals(typeDeclaration.toString(), td.toString()); }
@SuppressWarnings("unchecked") @Test public void testGenerateClass() { AST ast = AST.newAST(AST.JLS3); CompilationUnit cu = ast.newCompilationUnit(); TypeDeclaration td = ast.newTypeDeclaration(); td.setInterface(true); Modifier modifier = ast .newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD); td.modifiers().add(modifier); td.setName(ast.newSimpleName("Company")); TypeDeclaration typeDeclaration = interfaceGenerator.generateClass( clazz, ast, cu); assertEquals(typeDeclaration.toString(), td.toString()); }
AST ast = AST.newAST(AST.JLS3); CompilationUnit unit = ast.newCompilationUnit(); PackageDeclaration packageDeclaration = ast.newPackageDeclaration(); packageDeclaration.setName(ast.newSimpleName("example")); unit.setPackage(packageDeclaration); ImportDeclaration importDeclaration = ast.newImportDeclaration(); QualifiedName name = ast.newQualifiedName( ast.newSimpleName("java"), ast.newSimpleName("util")); importDeclaration.setName(name); importDeclaration.setOnDemand(true); unit.imports().add(importDeclaration); TypeDeclaration type = ast.newTypeDeclaration(); type.setInterface(false); type.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); type.setName(ast.newSimpleName("HelloWorld")); // ....