org.eclipse.jdt.core.dom.CompilationUnit#types ( )源码实例Demo

下面列出了org.eclipse.jdt.core.dom.CompilationUnit#types ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@SuppressWarnings("unchecked")
public Javadoc getJavaDoc() {
	if (context == null || context.eResource() == null || context.eResource().getResourceSet() == null)
		return null;
	Object classpathURIContext = ((XtextResourceSet) context.eResource().getResourceSet()).getClasspathURIContext();
	if (classpathURIContext instanceof IJavaProject) {
		IJavaProject javaProject = (IJavaProject) classpathURIContext;
		@SuppressWarnings("all")
		ASTParser parser = ASTParser.newParser(AST.JLS3);
		parser.setProject(javaProject);
		Map<String, String> options = javaProject.getOptions(true);
		options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=212207
		parser.setCompilerOptions(options);
		String source = rawJavaDoc + "class C{}"; //$NON-NLS-1$
		parser.setSource(source.toCharArray());
		CompilationUnit root = (CompilationUnit) parser.createAST(null);
		if (root == null)
			return null;
		List<AbstractTypeDeclaration> types = root.types();
		if (types.size() != 1)
			return null;
		AbstractTypeDeclaration type = types.get(0);
		return type.getJavadoc();
	}
	return null;
}
 
源代码2 项目: eclipse.jdt.ls   文件: JavadocContentAccess2.java
private static Javadoc getJavadocNode(IJavaElement element, String rawJavadoc) {
	//FIXME: take from SharedASTProvider if available
	//Caveat: Javadoc nodes are not available when Javadoc processing has been disabled!
	//https://bugs.eclipse.org/bugs/show_bug.cgi?id=212207

	String source = rawJavadoc + "class C{}"; //$NON-NLS-1$
	CompilationUnit root = createAST(element, source);
	if (root == null) {
		return null;
	}
	List<AbstractTypeDeclaration> types = root.types();
	if (types.size() != 1) {
		return null;
	}
	AbstractTypeDeclaration type = types.get(0);
	return type.getJavadoc();
}
 
源代码3 项目: RefactoringMiner   文件: UMLModelASTReader.java
protected void processCompilationUnit(String sourceFilePath, CompilationUnit compilationUnit) {
	PackageDeclaration packageDeclaration = compilationUnit.getPackage();
	String packageName = null;
	if(packageDeclaration != null)
		packageName = packageDeclaration.getName().getFullyQualifiedName();
	else
		packageName = "";
	
	List<ImportDeclaration> imports = compilationUnit.imports();
	List<String> importedTypes = new ArrayList<String>();
	for(ImportDeclaration importDeclaration : imports) {
		importedTypes.add(importDeclaration.getName().getFullyQualifiedName());
	}
	List<AbstractTypeDeclaration> topLevelTypeDeclarations = compilationUnit.types();
       for(AbstractTypeDeclaration abstractTypeDeclaration : topLevelTypeDeclarations) {
       	if(abstractTypeDeclaration instanceof TypeDeclaration) {
       		TypeDeclaration topLevelTypeDeclaration = (TypeDeclaration)abstractTypeDeclaration;
       		processTypeDeclaration(compilationUnit, topLevelTypeDeclaration, packageName, sourceFilePath, importedTypes);
       	}
       	else if(abstractTypeDeclaration instanceof EnumDeclaration) {
       		EnumDeclaration enumDeclaration = (EnumDeclaration)abstractTypeDeclaration;
       		processEnumDeclaration(compilationUnit, enumDeclaration, packageName, sourceFilePath, importedTypes);
       	}
       }
}
 
源代码4 项目: DesigniteJava   文件: SM_TypeTest.java
@Test
public void SM_Type_getName() {
	CompilationUnit unit = project.createCU(getTestingPath() + File.separator + "test_package" +File.separator + "TestClass.java");
	List<TypeDeclaration> typeList = unit.types();

	SM_Type type = null;
	for (TypeDeclaration typeDecl : typeList)
		type = new SM_Type(typeDecl, unit, new SM_Package("Test", project, null), null);
		
	assertEquals(type.getName(), "TestClass");		
}
 
源代码5 项目: DesigniteJava   文件: SM_TypeTest.java
@Test
public void SM_Type_checkDefaultAccess() {
	CompilationUnit unit = project.createCU(getTestingPath() + File.separator + "test_package" +File.separator + "DefaultClass.java");
	List<TypeDeclaration> typeList = unit.types();

	SM_Type type = null;
	for (TypeDeclaration typeDecl : typeList)
		type = new SM_Type(typeDecl, unit, new SM_Package("Test", project, null), null);
	assertEquals(type.getAccessModifier(), AccessStates.DEFAULT);		
}
 
源代码6 项目: DesigniteJava   文件: SM_TypeTest.java
@Test
public void SM_Type_check_isAbstract() {
	CompilationUnit unit = project.createCU(getTestingPath() + File.separator +"test_package" + File.separator + "AbstractClass.java");
	List<TypeDeclaration> typeList = unit.types();

	SM_Type type = null;
	for (TypeDeclaration typeDecl : typeList)
		type = new SM_Type(typeDecl, unit, new SM_Package("Test", project, null), null);
	assertTrue(type.isAbstract());		
}
 
源代码7 项目: DesigniteJava   文件: SM_TypeTest.java
@Test
public void SM_Type_check_isInterface() {
	CompilationUnit unit = project.createCU(getTestingPath() + File.separator +"test_package"+File.separator +"Interface.java");
	List<TypeDeclaration> typeList = unit.types();

	SM_Type type = null;
	for (TypeDeclaration typeDecl : typeList)
		type = new SM_Type(typeDecl, unit, new SM_Package("Test", project, null), null);
	assertTrue(type.isInterface());		
}
 
源代码8 项目: DesigniteJava   文件: SM_TypeTest.java
@Test(expected = NullPointerException.class)
public void SM_Type_nullCompilationUnit() {
	CompilationUnit unit = project.createCU(getTestingPath() + File.separator +"test_package"+File.separator +"TestClass.java");
	List<TypeDeclaration> listOfTypes = unit.types();
	
	type = new SM_Type(listOfTypes.get(0), null, null, null);	
}
 
源代码9 项目: DesigniteJava   文件: SM_MethodTest.java
@SuppressWarnings("unchecked")
@Before
public void setUp() {
	project = new SM_Project(new InputArgs(getTestingPath() + File.separator + "test_package", getTestingPath()));
	CompilationUnit unit = project.createCU(getTestingPath() + File.separator + "test_package" + File.separator + "TestMethods.java");
	List<TypeDeclaration> typeList = unit.types();
	
	for(TypeDeclaration typeDecl: typeList){
		type = new SM_Type(typeDecl, unit, new SM_Package("Test", project, null), null);
		type.parse();
	}

	methods = type.getMethodList();
}
 
源代码10 项目: txtUML   文件: WizardUtils.java
/**
 * @return the list of types in the given compilation unit.
 */
private synchronized static List<?> getTypes(ICompilationUnit compilationUnit) {
	ASTParser parser = ASTParser.newParser(AST.JLS8);
	parser.setResolveBindings(true);
	parser.setSource(compilationUnit);
	CompilationUnit cu = (CompilationUnit) parser.createAST(null);
	return cu.types();
}
 
源代码11 项目: txtUML   文件: Utils.java
@SuppressWarnings("unchecked")
public static List<AbstractTypeDeclaration> getSequenceDiagrams(CompilationUnit node) {
	List<AbstractTypeDeclaration> types = node.types();
	List<AbstractTypeDeclaration> sequenceDiagrams = types.stream().map(td -> ((AbstractTypeDeclaration) td))
			.filter(td -> implementsInteraction(td.resolveBinding())).collect(toList());
	return sequenceDiagrams;
}
 
public static TypeParameter newTypeParameter(AST ast, String content) {
	StringBuffer buffer= new StringBuffer(TYPEPARAM_HEADER);
	buffer.append(content);
	buffer.append(TYPEPARAM_FOOTER);
	ASTParser p= ASTParser.newParser(ast.apiLevel());
	p.setSource(buffer.toString().toCharArray());
	CompilationUnit root= (CompilationUnit) p.createAST(null);
	List<AbstractTypeDeclaration> list= root.types();
	TypeDeclaration typeDecl= (TypeDeclaration) list.get(0);
	MethodDeclaration methodDecl= typeDecl.getMethods()[0];
	TypeParameter tp= (TypeParameter) methodDecl.typeParameters().get(0);
	ASTNode result= ASTNode.copySubtree(ast, tp);
	result.accept(new PositionClearer());
	return (TypeParameter) result;
}
 
public static Type newType(AST ast, String content) {
	StringBuffer buffer= new StringBuffer(TYPE_HEADER);
	buffer.append(content);
	buffer.append(TYPE_FOOTER);
	ASTParser p= ASTParser.newParser(ast.apiLevel());
	p.setSource(buffer.toString().toCharArray());
	CompilationUnit root= (CompilationUnit) p.createAST(null);
	List<AbstractTypeDeclaration> list= root.types();
	TypeDeclaration typeDecl= (TypeDeclaration) list.get(0);
	MethodDeclaration methodDecl= typeDecl.getMethods()[0];
	ASTNode type= methodDecl.getReturnType2();
	ASTNode result= ASTNode.copySubtree(ast, type);
	result.accept(new PositionClearer());
	return (Type)result;
}
 
/**
 * Returns a string for the given AST used for debugging.
 *
 * @param ast the compilation unit AST
 * @return a string used for debugging
 */
private String toString(CompilationUnit ast) {
	if (ast == null)
		return "null"; //$NON-NLS-1$

	List<AbstractTypeDeclaration> types= ast.types();
	if (types != null && types.size() > 0)
		return types.get(0).getName().getIdentifier() + "(" + ast.hashCode() + ")"; //$NON-NLS-1$//$NON-NLS-2$
	else
		return "AST without any type"; //$NON-NLS-1$
}
 
private static Javadoc getJavadocNode(IJavaElement element, String rawJavadoc) {
	//FIXME: take from SharedASTProvider if available
	//Caveat: Javadoc nodes are not available when Javadoc processing has been disabled!
	//https://bugs.eclipse.org/bugs/show_bug.cgi?id=212207

	String source= rawJavadoc + "class C{}"; //$NON-NLS-1$
	CompilationUnit root= createAST(element, source);
	if (root == null)
		return null;
	List<AbstractTypeDeclaration> types= root.types();
	if (types.size() != 1)
		return null;
	AbstractTypeDeclaration type= types.get(0);
	return type.getJavadoc();
}
 
private static int getFirstTypeBeginPos(CompilationUnit root) {
	List types= root.types();
	if (!types.isEmpty()) {
		return root.getExtendedStartPosition(((ASTNode) types.get(0)));
	}
	return -1;
}
 
public static void getWrongTypeNameProposals(IInvocationContext context, IProblemLocationCore problem,
		Collection<ChangeCorrectionProposal> proposals) {
	ICompilationUnit cu= context.getCompilationUnit();
	boolean isLinked = cu.getResource().isLinked();

	IJavaProject javaProject= cu.getJavaProject();
	String sourceLevel= javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
	String compliance= javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);

	CompilationUnit root= context.getASTRoot();

	ASTNode coveredNode= problem.getCoveredNode(root);
	if (!(coveredNode instanceof SimpleName)) {
		return;
	}

	ASTNode parentType= coveredNode.getParent();
	if (!(parentType instanceof AbstractTypeDeclaration)) {
		return;
	}

	String currTypeName= ((SimpleName) coveredNode).getIdentifier();
	String newTypeName= JavaCore.removeJavaLikeExtension(cu.getElementName());


	boolean hasOtherPublicTypeBefore = false;

	boolean found = false;
	List<AbstractTypeDeclaration> types= root.types();
	for (int i= 0; i < types.size(); i++) {
		AbstractTypeDeclaration curr= types.get(i);
		if (parentType != curr) {
			if (newTypeName.equals(curr.getName().getIdentifier())) {
				return;
			}
			if (!found && Modifier.isPublic(curr.getModifiers())) {
				hasOtherPublicTypeBefore = true;
			}
		} else {
			found = true;
		}
	}

	if (!JavaConventions.validateJavaTypeName(newTypeName, sourceLevel, compliance).matches(IStatus.ERROR)) {
		proposals.add(new CorrectMainTypeNameProposal(cu, context, currTypeName, newTypeName, IProposalRelevance.RENAME_TYPE));
	}

	if (!hasOtherPublicTypeBefore && JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isResourceOperationSupported()) {
		String newCUName = JavaModelUtil.getRenamedCUName(cu, currTypeName);
		ICompilationUnit newCU = ((IPackageFragment) (cu.getParent())).getCompilationUnit(newCUName);
		if (!newCU.exists() && !isLinked && !JavaConventions.validateCompilationUnitName(newCUName, sourceLevel, compliance).matches(IStatus.ERROR)) {
			RenameCompilationUnitChange change = new RenameCompilationUnitChange(cu, newCUName);

			// rename CU
			String label = Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_renamecu_description, BasicElementLabels.getResourceName(newCUName));
			proposals.add(new ChangeCorrectionProposal(label, CodeActionKind.QuickFix, change, IProposalRelevance.RENAME_CU));
		}
	}

}
 
源代码18 项目: txtUML   文件: PlantUmlExporter.java
@SuppressWarnings("unchecked")
private TypeDeclaration getSequenceDiagramTypeDeclaration(String sequenceDiagramName, CompilationUnit cu) {
	List<TypeDeclaration> declarations = cu.types();
	return declarations.stream()
			.filter(decl -> decl.resolveBinding().getQualifiedName().equals(sequenceDiagramName)).findFirst().get();
}
 
public static void getWrongTypeNameProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ICompilationUnit cu= context.getCompilationUnit();
	boolean isLinked= cu.getResource().isLinked();

	IJavaProject javaProject= cu.getJavaProject();
	String sourceLevel= javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
	String compliance= javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);

	CompilationUnit root= context.getASTRoot();

	ASTNode coveredNode= problem.getCoveredNode(root);
	if (!(coveredNode instanceof SimpleName))
		return;

	ASTNode parentType= coveredNode.getParent();
	if (!(parentType instanceof AbstractTypeDeclaration))
		return;

	String currTypeName= ((SimpleName) coveredNode).getIdentifier();
	String newTypeName= JavaCore.removeJavaLikeExtension(cu.getElementName());

	boolean hasOtherPublicTypeBefore= false;

	boolean found= false;
	List<AbstractTypeDeclaration> types= root.types();
	for (int i= 0; i < types.size(); i++) {
		AbstractTypeDeclaration curr= types.get(i);
		if (parentType != curr) {
			if (newTypeName.equals(curr.getName().getIdentifier())) {
				return;
			}
			if (!found && Modifier.isPublic(curr.getModifiers())) {
				hasOtherPublicTypeBefore= true;
			}
		} else {
			found= true;
		}
	}
	if (!JavaConventions.validateJavaTypeName(newTypeName, sourceLevel, compliance).matches(IStatus.ERROR)) {
		proposals.add(new CorrectMainTypeNameProposal(cu, context, currTypeName, newTypeName, IProposalRelevance.RENAME_TYPE));
	}

	if (!hasOtherPublicTypeBefore) {
		String newCUName= JavaModelUtil.getRenamedCUName(cu, currTypeName);
		ICompilationUnit newCU= ((IPackageFragment) (cu.getParent())).getCompilationUnit(newCUName);
		if (!newCU.exists() && !isLinked && !JavaConventions.validateCompilationUnitName(newCUName, sourceLevel, compliance).matches(IStatus.ERROR)) {
			RenameCompilationUnitChange change= new RenameCompilationUnitChange(cu, newCUName);

			// rename CU
			String label= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_renamecu_description, BasicElementLabels.getResourceName(newCUName));
			proposals.add(new ChangeCorrectionProposal(label, change, IProposalRelevance.RENAME_CU, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_RENAME)));
		}
	}
}