List of usage examples for org.eclipse.jdt.core.dom AbstractTypeDeclaration isPackageMemberTypeDeclaration
public boolean isPackageMemberTypeDeclaration()
From source file:com.google.currysrc.api.process.ast.TypeLocator.java
License:Apache License
/** Returns the enclosing type for nested/inner classes, {@code null} otherwise. */ public static AbstractTypeDeclaration findEnclosingTypeDeclaration(AbstractTypeDeclaration typeDeclaration) { if (typeDeclaration.isPackageMemberTypeDeclaration()) { return null; }// w w w . ja v a 2s . co m ASTNode enclosingNode = typeDeclaration.getParent(); return enclosingNode instanceof AbstractTypeDeclaration ? (AbstractTypeDeclaration) enclosingNode : null; }
From source file:com.halware.nakedide.eclipse.core.templates.propl.GenericTemplCompltnProcessor.java
License:Open Source License
/** * Creates a concrete template context for the given region in the document. This involves finding out which * context type is valid at the given location, and then creating a context of this type. The default implementation * returns a <code>DocumentTemplateContext</code> for the context type at the given location. * * @param viewer the viewer for which the context is created * @param region the region into <code>document</code> for which the context is created * @return a template context that can handle template insertion at the given location, or <code>null</code> */// w w w . j av a2 s. co m protected TemplateContext createContext(ITextViewer viewer, IRegion region) { IDocument document = viewer.getDocument(); // somewhat heavy handed. Oh well. ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(document.get().toCharArray()); CompilationUnit parsedCompilationUnit = (CompilationUnit) parser.createAST(null); List<AbstractTypeDeclaration> typeDeclarations = Generics.asT(parsedCompilationUnit.types()); TypeDeclaration containingTypeDeclaration = null; for (AbstractTypeDeclaration candidateTypeDeclaration : typeDeclarations) { if (!regionOverlaps(candidateTypeDeclaration, region)) { continue; } // ignore enums and annotations if (!(candidateTypeDeclaration instanceof TypeDeclaration)) { continue; } // ignore nested (not top-level) types. if (!candidateTypeDeclaration.isPackageMemberTypeDeclaration()) { continue; } containingTypeDeclaration = (TypeDeclaration) candidateTypeDeclaration; break; } if (containingTypeDeclaration != null) { MethodDeclaration[] methodDeclarations = containingTypeDeclaration.getMethods(); for (MethodDeclaration methodDeclaration : methodDeclarations) { if (regionOverlaps(methodDeclaration, region)) { // no templates within methods. return null; } } } TemplateContextType contextType = getContextType(viewer, region); if (contextType != null) { return new DocTemplContext(contextType, document, region.getOffset(), region.getLength(), containingTypeDeclaration != null); } return null; }
From source file:java5totext.input.JDTVisitor.java
License:Open Source License
private void endVisitATD(org.eclipse.jdt.core.dom.AbstractTypeDeclaration node, AbstractTypeDeclaration element) { element.setName(node.getName().getIdentifier()); for (Iterator<?> i = node.bodyDeclarations().iterator(); i.hasNext();) { BodyDeclaration itElement = (BodyDeclaration) this.binding.get(i.next()); if (itElement != null) element.getBodyDeclarations().add(itElement); }/*ww w.j a va 2s .c o m*/ // process for package direct types if (node.isPackageMemberTypeDeclaration()) { String mainClassName = this.currentJavaFilePath .substring(this.currentJavaFilePath.lastIndexOf(java.io.File.separator) + 1); mainClassName = mainClassName.substring(0, mainClassName.length() - ".java".length()); // process for main type of the .java file if (mainClassName.equalsIgnoreCase(node.getName().getIdentifier())) { // memorize this AST node as the main setRootTypeOrEnum(element); for (Iterator<ImportDeclaration> i = this.imports.iterator(); i.hasNext();) { ImportDeclaration importDecl = i.next(); element.getImports().add(importDecl); } } if (this.currentPackage == null) { // Type without package this.currentPackage = (PackageDeclaration) this.globalBindings.getTarget(this.DEFAULT_PKG_ID); if (this.currentPackage == null) { this.currentPackage = this.factory.createPackageDeclaration(); this.currentPackage.setName(this.DEFAULT_PKG_ID); if (this.currentJavaFilePath != null) this.globalBindings.addTarget(this.DEFAULT_PKG_ID, this.currentPackage); } } this.currentPackage.getOwnedElements().add(element); // calculate qualified name String qualifiedName = this.currentPackage.getQualifiedName() + "." + element.getName(); element.setQualifiedName(qualifiedName); } }
From source file:org.eclipse.modisco.java.discoverer.internal.io.java.JDTVisitor.java
License:Open Source License
/** * Fill informations in the MoDisco {@code AbstractTypeDeclaration} node * from the JDT {@code AbstractTypeDeclaration} node. * //from w ww . ja v a 2 s . c o m * @param node * the JDT {@code AbstractTypeDeclaration} node * @param element * the MoDisco {@code AbstractTypeDeclaration} node */ private void endVisitATD(final org.eclipse.jdt.core.dom.AbstractTypeDeclaration node, final AbstractTypeDeclaration element) { element.setName(node.getName().getIdentifier()); for (Iterator<?> i = node.bodyDeclarations().iterator(); i.hasNext();) { BodyDeclaration itElement = (BodyDeclaration) this.binding.get(i.next()); if (itElement != null) { element.getBodyDeclarations().add(itElement); } } // process for package direct types if (node.isPackageMemberTypeDeclaration()) { String ext = null; if (this.cuNode instanceof ICompilationUnit) { ext = ".java"; //$NON-NLS-1$ } else if (this.cuNode instanceof IClassFile) { ext = ".class"; //$NON-NLS-1$ } if (ext == null) { // memorize this AST node as the main setRootTypeOrEnum(element); } else { String mainClassName = this.currentFilePath .substring(this.currentFilePath.lastIndexOf(java.io.File.separator) + 1); mainClassName = mainClassName.substring(0, mainClassName.length() - ext.length()); // process for main type of the .java file if (mainClassName.equalsIgnoreCase(node.getName().getIdentifier())) { // memorize this AST node as the main setRootTypeOrEnum(element); } } if (this.currentPackage == null) { // Type without package this.currentPackage = (Package) getGlobalBindings().getTarget(JDTVisitor.DEFAULT_PKG_ID); if (this.currentPackage == null) { this.currentPackage = this.factory.createPackage(); this.currentPackage.setName(JDTVisitor.DEFAULT_PKG_ID); if (this.currentFilePath != null) { this.currentPackage.setModel(this.jdtModel); } getGlobalBindings().addTarget(JDTVisitor.DEFAULT_PKG_ID, this.currentPackage); } } this.currentPackage.getOwnedElements().add(element); // calculate qualified name // String qualifiedName = this.currentPackage.getQualifiedName() + // "." + element.getName(); } if (this.isINCREMENTALDISCOVERING) { List<EObject> toBeRemoved = new ArrayList<EObject>(); Iterator<BodyDeclaration> bodyDeclarations = element.getBodyDeclarations().iterator(); while (bodyDeclarations.hasNext()) { BodyDeclaration bodyDeclaration = bodyDeclarations.next(); if (bodyDeclaration.isProxy()) { toBeRemoved.add(bodyDeclaration); } } if (element instanceof EnumDeclaration) { Iterator<EnumConstantDeclaration> enums = ((EnumDeclaration) element).getEnumConstants().iterator(); while (enums.hasNext()) { EnumConstantDeclaration enumDecl = enums.next(); if (enumDecl.isProxy()) { toBeRemoved.add(enumDecl); } } } // To remove proxy Field element that have been replaced by an other // one // This is useful in the case of the incremental algorithm Iterator<EObject> toBeRemovedIter = toBeRemoved.iterator(); while (toBeRemovedIter.hasNext()) { EObject eObject = toBeRemovedIter.next(); if (eObject instanceof FieldDeclaration) { FieldDeclaration fieldDeclaration = (FieldDeclaration) eObject; if (fieldDeclaration.getFragments().size() > 0) { String message = "Proxy field declaration should not contains any fragments (size=" //$NON-NLS-1$ + fieldDeclaration.getFragments().size() + "): " //$NON-NLS-1$ + JDTVisitorUtils.getQualifiedName(node) + "."; //$NON-NLS-1$ try { message += (fieldDeclaration.getFragments().get(0)).getName(); } catch (Exception e) { message += "???"; //$NON-NLS-1$ } RuntimeException exception = new RuntimeException(message); MoDiscoLogger.logError(exception, JavaActivator.getDefault()); throw exception; } deepRemove(eObject); element.getBodyDeclarations().remove(eObject); if (element instanceof EnumDeclaration) { ((EnumDeclaration) element).getEnumConstants().remove(eObject); } } } } }
From source file:org.eclipse.pde.api.tools.internal.builder.Validator.java
License:Open Source License
/** * Constructs the qualified name of the enclosing parent type * //w ww .j a v a2s . com * @param node the node to get the parent name for * @param buffer the buffer to write the name into * @return the fully qualified name of the parent */ protected String getTypeName(ASTNode node, StringBuffer buffer) { switch (node.getNodeType()) { case ASTNode.COMPILATION_UNIT: { CompilationUnit unit = (CompilationUnit) node; PackageDeclaration packageDeclaration = unit.getPackage(); if (packageDeclaration != null) { buffer.insert(0, '.'); buffer.insert(0, packageDeclaration.getName().getFullyQualifiedName()); } return String.valueOf(buffer); } default: { if (node instanceof AbstractTypeDeclaration) { AbstractTypeDeclaration typeDeclaration = (AbstractTypeDeclaration) node; if (typeDeclaration.isPackageMemberTypeDeclaration()) { buffer.insert(0, typeDeclaration.getName().getIdentifier()); } else { buffer.insert(0, typeDeclaration.getName().getFullyQualifiedName()); buffer.insert(0, '$'); } } } } return getTypeName(node.getParent(), buffer); }
From source file:org.eclipse.pde.api.tools.internal.util.Signatures.java
License:Open Source License
/** * Determines if the given {@link MethodDeclaration} is present in a top * level type//from ww w. j a v a 2 s .com * * @param method the given method * @return true if the given {@link MethodDeclaration} is present in a top * level type, false otherwise */ static boolean isInTopLevelType(final MethodDeclaration method) { AbstractTypeDeclaration type = (AbstractTypeDeclaration) method.getParent(); return type != null && type.isPackageMemberTypeDeclaration(); }
From source file:org.eclipse.pde.ds.internal.annotations.AnnotationProcessor.java
License:Open Source License
private boolean isNestedPublicStatic(AbstractTypeDeclaration type) { if (Modifier.isStatic(type.getModifiers())) { ASTNode parent = type.getParent(); if (parent != null && (parent.getNodeType() == ASTNode.TYPE_DECLARATION || parent.getNodeType() == ASTNode.ANNOTATION_TYPE_DECLARATION)) { AbstractTypeDeclaration parentType = (AbstractTypeDeclaration) parent; if (Modifier.isPublic(parentType.getModifiers())) return parentType.isPackageMemberTypeDeclaration() || isNestedPublicStatic(parentType); }//w w w . j a v a2 s. com } return false; }
From source file:org.jboss.forge.roaster.spi.impl.JavaParserImpl.java
License:Open Source License
@Override public JavaUnit parseUnit(final String data) { Document document = new Document(data); ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setSource(document.get().toCharArray()); parser.setCompilerOptions(JDTOptions.getJDTOptions()); parser.setResolveBindings(true);//from w w w . j a v a 2s . c om parser.setKind(ASTParser.K_COMPILATION_UNIT); CompilationUnit unit = (CompilationUnit) parser.createAST(null); unit.recordModifications(); TypeDeclarationFinderVisitor visitor = new TypeDeclarationFinderVisitor(); unit.accept(visitor); List<AbstractTypeDeclaration> declarations = visitor.getTypeDeclarations(); List<JavaType<?>> types = new ArrayList<>(); if (!declarations.isEmpty()) { for (AbstractTypeDeclaration declaration : declarations) { if (declaration.isPackageMemberTypeDeclaration()) { types.add(getJavaSource(null, document, unit, declaration)); } } return new JavaUnitImpl(types); } else if (visitor.getPackageDeclaration() != null) { types.add(getJavaSource(null, document, unit, visitor.getPackageDeclaration())); return new JavaUnitImpl(types); } else { throw new ParserException("Could not find type declaration in Java source - is this actually code?"); } }
From source file:sharpen.core.CSharpBuilder.java
License:Open Source License
private boolean isMainType(AbstractTypeDeclaration node) { return node.isPackageMemberTypeDeclaration() && Modifier.isPublic(node.getModifiers()); }