本文整理汇总了Java中org.eclipse.jdt.core.dom.CompilationUnit类的典型用法代码示例。如果您正苦于以下问题:Java CompilationUnit类的具体用法?Java CompilationUnit怎么用?Java CompilationUnit使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CompilationUnit类属于org.eclipse.jdt.core.dom包,在下文中一共展示了CompilationUnit类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getField
import org.eclipse.jdt.core.dom.CompilationUnit; //导入依赖的package包/类
/**
* @param methodname
* @return
*/
public FieldDeclaration getField( ) {
ASTParser parser = ASTParser.newParser(AST.JLS8);
String s = getStaticVariable ( );
if (s==null) return null;
parser.setSource(s.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
cu.accept(new ASTVisitor() {
public boolean visit(FieldDeclaration node) {
field = node;
return true;
}
});
return field;
}
示例2: getGraphWalkerClassAnnotation
import org.eclipse.jdt.core.dom.CompilationUnit; //导入依赖的package包/类
/**
* @return
*/
public NormalAnnotation getGraphWalkerClassAnnotation() {
String source = getSource ();
if(source==null) return null;
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setSource(source.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
cu.accept(new ASTVisitor() {
public boolean visit(NormalAnnotation node) {
annotation = node;
return true;
}
});
if (this.generateAnnotation) return annotation;
return null;
}
示例3: analyzeCompilationUnit
import org.eclipse.jdt.core.dom.CompilationUnit; //导入依赖的package包/类
private void analyzeCompilationUnit(CompilationUnit unit, ICompilationUnit compilationUnit) {
Set<String> filters = loadFilters();
ASTVisitor importsVisitor = new ImportsVisitor(filters);
unit.accept(importsVisitor);
List types = unit.types();
for (Iterator iter = types.iterator(); iter.hasNext();) {
Object next = iter.next();
if (next instanceof TypeDeclaration) {
// declaration: Contains one file content at a time.
TypeDeclaration declaration = (TypeDeclaration) next;
// traverseType(declaration,true);
}
}
}
示例4: decideRuleKind
import org.eclipse.jdt.core.dom.CompilationUnit; //导入依赖的package包/类
private static String decideRuleKind(ReferencedClassesParser parser, Set<String> dependencies) {
CompilationUnit cu = parser.compilationUnit;
if (cu.types().isEmpty()) {
return "java_library";
}
AbstractTypeDeclaration topLevelClass = (AbstractTypeDeclaration) cu.types().get(0);
if ((topLevelClass.getModifiers() & Modifier.ABSTRACT) != 0) {
// Class is abstract, can't be a test.
return "java_library";
}
// JUnit 4 tests
if (parser.className.endsWith("Test") && dependencies.contains("org.junit.Test")) {
return "java_test";
}
if (any(
topLevelClass.bodyDeclarations(),
d -> d instanceof MethodDeclaration && isMainMethod((MethodDeclaration) d))) {
return "java_binary";
}
return "java_library";
}
示例5: getGeneratedClassAnnotation
import org.eclipse.jdt.core.dom.CompilationUnit; //导入依赖的package包/类
public NormalAnnotation getGeneratedClassAnnotation() {
String source = getGeneratedAnnotationSource ();
if(source==null) return null;
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setSource(source.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
cu.accept(new ASTVisitor() {
public boolean visit(NormalAnnotation node) {
annotation = node;
return true;
}
});
return annotation;
}
示例6: findPathGeneratorInGraphWalkerAnnotation
import org.eclipse.jdt.core.dom.CompilationUnit; //导入依赖的package包/类
/**
* @param cu
* @return
*/
public static String findPathGeneratorInGraphWalkerAnnotation(ICompilationUnit cu) {
CompilationUnit ast = parse(cu);
Map<String, String> ret = new HashMap<String, String>();
ast.accept(new ASTVisitor() {
public boolean visit(MemberValuePair node) {
String name = node.getName().getFullyQualifiedName();
if ("value".equals(name) && node.getParent() != null && node.getParent() instanceof NormalAnnotation) {
IAnnotationBinding annoBinding = ((NormalAnnotation) node.getParent()).resolveAnnotationBinding();
String qname = annoBinding.getAnnotationType().getQualifiedName();
if (GraphWalker.class.getName().equals(qname)) {
StringLiteral sl = (StringLiteral) node.getValue();
ret.put("ret", sl.getLiteralValue());
}
}
return true;
}
});
return ret.get("ret");
}
示例7: save
import org.eclipse.jdt.core.dom.CompilationUnit; //导入依赖的package包/类
/**
* Save the AST int he Compilation Unit
*
* @param testInterface
* @param rewrite
* @throws CoreException
*/
public static void save(CompilationUnit unit, ASTRewrite rewrite) throws CoreException {
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
IPath path = unit.getJavaElement().getPath();
try {
bufferManager.connect(path, null);
ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path);
IDocument document = textFileBuffer.getDocument();
TextEdit edit = rewrite.rewriteAST(document, null);
edit.apply(document);
textFileBuffer.commit(null /* ProgressMonitor */, true /* Overwrite */);
} catch (Exception e) {
ResourceManager.logException(e);
} finally {
// disconnect the path
bufferManager.disconnect(path, null);
}
}
示例8: parse
import org.eclipse.jdt.core.dom.CompilationUnit; //导入依赖的package包/类
public void parse() throws ParseException {
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setSource(source.toCharArray());
Map<String, String> options = JavaCore.getOptions();
options.put("org.eclipse.jdt.core.compiler.source", "1.8");
parser.setCompilerOptions(options);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setUnitName("Program.java");
parser.setEnvironment(new String[] { classpath != null? classpath : "" },
new String[] { "" }, new String[] { "UTF-8" }, true);
parser.setResolveBindings(true);
cu = (CompilationUnit) parser.createAST(null);
List<IProblem> problems = Arrays.stream(cu.getProblems()).filter(p ->
p.isError() &&
p.getID() != IProblem.PublicClassMustMatchFileName && // we use "Program.java"
p.getID() != IProblem.ParameterMismatch // Evidence varargs
).collect(Collectors.toList());
if (problems.size() > 0)
throw new ParseException(problems);
}
示例9: toGroovyTypeModel
import org.eclipse.jdt.core.dom.CompilationUnit; //导入依赖的package包/类
public TypeModel toGroovyTypeModel(String source) {
source = source.replaceAll("//(?i)\\s*" + quote("given") + SEPARATOR, "givenBlockStart();");
source = source.replaceAll("//(?i)\\s*" + quote("when") + SEPARATOR, "whenBlockStart();");
source = source.replaceAll("//(?i)\\s*" + quote("then") + SEPARATOR, "thenBlockStart();");
ASTParser parser = ASTParser.newParser(JLS8);
parser.setSource(source.toCharArray());
parser.setKind(K_COMPILATION_UNIT);
parser.setCompilerOptions(compilerOptions());
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
astProxy.setTarget(cu.getAST());
TypeVisitor visitor = testClassVisitorSupplier.get();
cu.accept(visitor);
return visitor.typeModel();
}
示例10: serializePosition
import org.eclipse.jdt.core.dom.CompilationUnit; //导入依赖的package包/类
private void serializePosition(CompilationUnit cu, ASTNode node, JsonGenerator jG) throws IOException {
final int startPosition = node.getStartPosition();
jG.writeFieldName("startPosition");
jG.writeNumber(startPosition);
jG.writeFieldName("startLine");
jG.writeNumber(cu.getLineNumber(startPosition));
jG.writeFieldName("startColumn");
jG.writeNumber(cu.getColumnNumber(startPosition) + 1); // 1-based numbering
final int endPosition = startPosition + node.getLength();
jG.writeFieldName("endPosition");
jG.writeNumber(endPosition);
jG.writeFieldName("endLine");
jG.writeNumber(cu.getLineNumber(endPosition));
jG.writeFieldName("endColumn");
jG.writeNumber(cu.getColumnNumber(endPosition) + 1); // 1-based numbering
}
示例11: addEdits
import org.eclipse.jdt.core.dom.CompilationUnit; //导入依赖的package包/类
@Override
protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
super.addEdits(doc, root);
// build a full AST
CompilationUnit unit = SharedASTProvider.getInstance().getAST(getCompilationUnit(), null);
ASTNode name= NodeFinder.perform(unit, fOffset, fLength);
if (name instanceof SimpleName) {
SimpleName[] names= LinkedNodeFinder.findByProblems(unit, (SimpleName) name);
if (names != null) {
for (int i= 0; i < names.length; i++) {
SimpleName curr= names[i];
root.addChild(new ReplaceEdit(curr.getStartPosition(), curr.getLength(), fNewName));
}
return;
}
}
root.addChild(new ReplaceEdit(fOffset, fLength, fNewName));
}
示例12: getFullName
import org.eclipse.jdt.core.dom.CompilationUnit; //导入依赖的package包/类
/**
* Evaluates fully qualified name of the TypeDeclaration object.
*/
public static String getFullName(TypeDeclaration decl) {
String name = decl.getName().getIdentifier();
ASTNode parent = decl.getParent();
// resolve full name e.g.: A.B
while (parent != null && parent.getClass() == TypeDeclaration.class) {
name = ((TypeDeclaration) parent).getName().getIdentifier() + "." + name;
parent = parent.getParent();
}
// resolve fully qualified name e.g.: some.package.A.B
if (decl.getRoot().getClass() == CompilationUnit.class) {
CompilationUnit root = (CompilationUnit) decl.getRoot();
if (root.getPackage() != null) {
PackageDeclaration pack = root.getPackage();
name = pack.getName().getFullyQualifiedName() + "." + name;
}
}
return name;
}
示例13: evaluateArgumentExpressions
import org.eclipse.jdt.core.dom.CompilationUnit; //导入依赖的package包/类
private Expression evaluateArgumentExpressions(AST ast, ITypeBinding requiredType, String key) {
CompilationUnit root= (CompilationUnit) fCallerNode.getRoot();
int offset= fCallerNode.getStartPosition();
Expression best= null;
ITypeBinding bestType= null;
ScopeAnalyzer analyzer= new ScopeAnalyzer(root);
IBinding[] bindings= analyzer.getDeclarationsInScope(offset, ScopeAnalyzer.VARIABLES);
for (int i= 0; i < bindings.length; i++) {
IVariableBinding curr= (IVariableBinding) bindings[i];
ITypeBinding type= curr.getType();
if (type != null && canAssign(type, requiredType) && testModifier(curr)) {
if (best == null || isMoreSpecific(bestType, type)) {
best= ast.newSimpleName(curr.getName());
bestType= type;
}
}
}
Expression defaultExpression= ASTNodeFactory.newDefaultExpression(ast, requiredType);
if (best == null) {
best= defaultExpression;
}
return best;
}
示例14: getClassAttributes
import org.eclipse.jdt.core.dom.CompilationUnit; //导入依赖的package包/类
/**
* Method to get class attributes and add them in a list.
* @author Mariana Azevedo
* @since 13/07/2014
* @param node
*/
@SuppressWarnings("unchecked")
private void getClassAttributes(CompilationUnit node){
for (Object type : node.types()){
if (type instanceof TypeDeclaration){
FieldDeclaration [] attributes = ((TypeDeclaration) type).getFields();
for (FieldDeclaration attribute: attributes){
List<FieldDeclaration> fragments = attribute.fragments();
Object obj = fragments.get(0);
if (obj instanceof VariableDeclarationFragment){
String str = ((VariableDeclarationFragment) obj).getName().toString();
this.listOfAttributes.add(str);
}
}
}
}
}
示例15: getAmbiguousTypeReferenceProposals
import org.eclipse.jdt.core.dom.CompilationUnit; //导入依赖的package包/类
public static void getAmbiguousTypeReferenceProposals(IInvocationContext context, IProblemLocation problem,
Collection<CUCorrectionProposal> proposals) throws CoreException {
final ICompilationUnit cu= context.getCompilationUnit();
int offset= problem.getOffset();
int len= problem.getLength();
IJavaElement[] elements= cu.codeSelect(offset, len);
for (int i= 0; i < elements.length; i++) {
IJavaElement curr= elements[i];
if (curr instanceof IType) {
String qualifiedTypeName= ((IType) curr).getFullyQualifiedName('.');
CompilationUnit root= context.getASTRoot();
String label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_importexplicit_description, BasicElementLabels.getJavaElementName(qualifiedTypeName));
ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, ASTRewrite.create(root.getAST()), IProposalRelevance.IMPORT_EXPLICIT);
ImportRewrite imports= proposal.createImportRewrite(root);
imports.addImport(qualifiedTypeName);
proposals.add(proposal);
}
}
}