本文整理汇总了Java中org.eclipse.jdt.core.dom.AST类的典型用法代码示例。如果您正苦于以下问题:Java AST类的具体用法?Java AST怎么用?Java AST使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AST类属于org.eclipse.jdt.core.dom包,在下文中一共展示了AST类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getField
import org.eclipse.jdt.core.dom.AST; //导入依赖的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.AST; //导入依赖的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: rewriteAST
import org.eclipse.jdt.core.dom.AST; //导入依赖的package包/类
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel positionGroups) throws CoreException {
final ASTRewrite rewrite = cuRewrite.getASTRewrite();
VariableDeclarationFragment fragment = null;
for (int i = 0; i < fNodes.length; i++) {
final ASTNode node = fNodes[i];
final AST ast = node.getAST();
fragment = ast.newVariableDeclarationFragment();
fragment.setName(ast.newSimpleName(NAME_FIELD));
final FieldDeclaration declaration = ast.newFieldDeclaration(fragment);
declaration.setType(ast.newPrimitiveType(PrimitiveType.LONG));
declaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL));
if (!addInitializer(fragment, node)) {
continue;
}
if (fragment.getInitializer() != null) {
final TextEditGroup editGroup = createTextEditGroup(FixMessages.SerialVersion_group_description, cuRewrite);
if (node instanceof AbstractTypeDeclaration) {
rewrite.getListRewrite(node, ((AbstractTypeDeclaration) node).getBodyDeclarationsProperty()).insertAt(declaration, 0, editGroup);
} else if (node instanceof AnonymousClassDeclaration) {
rewrite.getListRewrite(node, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup);
} else if (node instanceof ParameterizedType) {
final ParameterizedType type = (ParameterizedType) node;
final ASTNode parent = type.getParent();
if (parent instanceof ClassInstanceCreation) {
final ClassInstanceCreation creation = (ClassInstanceCreation) parent;
final AnonymousClassDeclaration anonymous = creation.getAnonymousClassDeclaration();
if (anonymous != null) {
rewrite.getListRewrite(anonymous, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup);
}
}
} else {
Assert.isTrue(false);
}
addLinkedPositions(rewrite, fragment, positionGroups);
}
final String comment = CodeGeneration.getFieldComment(fUnit, declaration.getType().toString(), NAME_FIELD, "\n" /* StubUtility.getLineDelimiterUsed(fUnit) */);
if (comment != null && comment.length() > 0 && !"/**\n *\n */\n".equals(comment)) {
final Javadoc doc = (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC);
declaration.setJavadoc(doc);
}
}
if (fragment == null) {
return;
}
positionGroups.setEndPosition(rewrite.track(fragment));
}
示例4: getGeneratedClassAnnotation
import org.eclipse.jdt.core.dom.AST; //导入依赖的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;
}
示例5: parse
import org.eclipse.jdt.core.dom.AST; //导入依赖的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);
}
示例6: structuralPropertyNamesOf
import org.eclipse.jdt.core.dom.AST; //导入依赖的package包/类
private static List<String> structuralPropertyNamesOf(final List<Class<? extends ASTNode>> nodes) {
final List<String> names = new ArrayList<>();
for (final Class<? extends ASTNode> node : nodes) {
try {
final Method m = node.getDeclaredMethod("propertyDescriptors", int.class);
final List l = (List) m.invoke(null, AST.JLS8);
for (final Object o : l) {
final StructuralPropertyDescriptor d = (StructuralPropertyDescriptor) o;
names.add(d.getId());
}
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
throw new RuntimeException("unexpected exception", ex);
}
}
return names;
}
示例7: EclipseParser
import org.eclipse.jdt.core.dom.AST; //导入依赖的package包/类
/**
* Creates a new EclipseParser
*
* @param sourceFile String of source file to read
* @param outJ JSON parsed out
* @throws IOException when file can't be opened or errors in reading/writing
*/
public EclipseParser(String sourceFile, PrintStream outJ, boolean prettyprint) throws IOException {
File file = new File(sourceFile);
final BufferedReader reader = new BufferedReader(new FileReader(file));
char[] source = IOUtils.toCharArray(reader);
reader.close();
this.parser = ASTParser.newParser(AST.JLS8);
parser.setSource(source);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final JsonFactory jsonF = new JsonFactory();
jG = jsonF.createGenerator(outJ);
if (prettyprint) {
jG.setPrettyPrinter(new DefaultPrettyPrinter());
mapper.enable(SerializationFeature.INDENT_OUTPUT);
}
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
SimpleModule module = new SimpleModule();
module.addSerializer(ASTNode.class, new NodeSerializer());
mapper.registerModule(module);
}
示例8: evaluateVariableType
import org.eclipse.jdt.core.dom.AST; //导入依赖的package包/类
private Type evaluateVariableType(AST ast, ImportRewrite imports, ImportRewriteContext importRewriteContext, IBinding targetContext, TypeLocation location) {
if (fOriginalNode.getParent() instanceof MethodInvocation) {
MethodInvocation parent= (MethodInvocation) fOriginalNode.getParent();
if (parent.getExpression() == fOriginalNode) {
// _x_.foo() -> guess qualifier type by looking for a type with method 'foo'
ITypeBinding[] bindings= ASTResolving.getQualifierGuess(fOriginalNode.getRoot(), parent.getName().getIdentifier(), parent.arguments(), targetContext);
if (bindings.length > 0) {
return imports.addImport(bindings[0], ast, importRewriteContext, location);
}
}
}
ITypeBinding binding= ASTResolving.guessBindingForReference(fOriginalNode);
if (binding != null) {
if (binding.isWildcardType()) {
binding= ASTResolving.normalizeWildcardType(binding, isVariableAssigned(), ast);
if (binding == null) {
// only null binding applies
binding= ast.resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
}
}
return imports.addImport(binding, ast, importRewriteContext, location);
}
// no binding, find type AST node instead -> ABC a= x-> use 'ABC' as is
Type type = ASTResolving.guessTypeForReference(ast, fOriginalNode);
if (type != null) {
return type;
}
if (fVariableKind == CONST_FIELD) {
return ast.newSimpleType(ast.newSimpleName("String")); //$NON-NLS-1$
}
return ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$
}
示例9: createBuilderClass
import org.eclipse.jdt.core.dom.AST; //导入依赖的package包/类
public TypeDeclaration createBuilderClass(AST ast, TypeDeclaration originalType) {
TypeDeclaration builderType = ast.newTypeDeclaration();
builderType.setName(ast.newSimpleName(getBuilderName(originalType)));
if (preferencesManager.getPreferenceValue(ADD_GENERATED_ANNOTATION)) {
generatedAnnotationPopulator.addGeneratedAnnotation(ast, builderType);
}
builderType.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
builderType.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
builderType.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
if (preferencesManager.getPreferenceValue(GENERATE_JAVADOC_ON_BUILDER_CLASS)) {
Javadoc javadoc = javadocGenerator.generateJavadoc(ast, String.format(Locale.ENGLISH, "Builder to build {@link %s}.", originalType.getName().toString()),
Collections.emptyMap());
builderType.setJavadoc(javadoc);
}
return builderType;
}
示例10: createWithMethodBody
import org.eclipse.jdt.core.dom.AST; //导入依赖的package包/类
private Block createWithMethodBody(AST ast, BuilderField builderField) {
String originalFieldName = builderField.getOriginalFieldName();
String builderFieldName = builderField.getBuilderFieldName();
Block newBlock = ast.newBlock();
ReturnStatement builderReturnStatement = ast.newReturnStatement();
builderReturnStatement.setExpression(ast.newThisExpression());
Assignment newAssignment = ast.newAssignment();
FieldAccess fieldAccess = ast.newFieldAccess();
fieldAccess.setExpression(ast.newThisExpression());
fieldAccess.setName(ast.newSimpleName(originalFieldName));
newAssignment.setLeftHandSide(fieldAccess);
newAssignment.setRightHandSide(ast.newSimpleName(builderFieldName));
newBlock.statements().add(ast.newExpressionStatement(newAssignment));
newBlock.statements().add(builderReturnStatement);
return newBlock;
}
示例11: createWithMethodBody
import org.eclipse.jdt.core.dom.AST; //导入依赖的package包/类
private Block createWithMethodBody(AST ast, String originalFieldName, String builderFieldName) {
Block newBlock = ast.newBlock();
ReturnStatement builderReturnStatement = ast.newReturnStatement();
builderReturnStatement.setExpression(ast.newThisExpression());
Assignment newAssignment = ast.newAssignment();
FieldAccess fieldAccess = ast.newFieldAccess();
fieldAccess.setExpression(ast.newThisExpression());
fieldAccess.setName(ast.newSimpleName(originalFieldName));
newAssignment.setLeftHandSide(fieldAccess);
newAssignment.setRightHandSide(ast.newSimpleName(builderFieldName));
newBlock.statements().add(ast.newExpressionStatement(newAssignment));
newBlock.statements().add(builderReturnStatement);
return newBlock;
}
示例12: createNewWithMethod
import org.eclipse.jdt.core.dom.AST; //导入依赖的package包/类
private MethodDeclaration createNewWithMethod(AST ast, String fieldName, Block newBlock,
SingleVariableDeclaration methodParameterDeclaration, TypeDeclaration builderType,
BuilderField builderField) {
MethodDeclaration builderMethod = ast.newMethodDeclaration();
builderMethod.setName(ast.newSimpleName(builderClassMethodNameGeneratorService.build(fieldName)));
builderMethod.setReturnType2(ast.newSimpleType(
ast.newName(builderType.getName().getIdentifier())));
builderMethod.setBody(newBlock);
builderMethod.parameters().add(methodParameterDeclaration);
javadocAdder.addJavadocForWithMethod(ast, fieldName, builderMethod);
if (preferencesManager.getPreferenceValue(ADD_NONNULL_ON_RETURN)) {
markerAnnotationAttacher.attachNonNull(ast, builderMethod);
}
builderMethod.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
return builderMethod;
}
示例13: createNewWithMethod
import org.eclipse.jdt.core.dom.AST; //导入依赖的package包/类
public MethodDeclaration createNewWithMethod(AST ast, BuilderField builderField,
StagedBuilderProperties nextStage) {
String fieldName = builderField.getBuilderFieldName();
MethodDeclaration builderMethod = ast.newMethodDeclaration();
builderMethod.setName(ast.newSimpleName(builderClassMethodNameGeneratorService.build(fieldName)));
builderMethod.setReturnType2(ast.newSimpleType(ast.newName(nextStage.getInterfaceName())));
builderMethod.parameters()
.add(withMethodParameterCreatorFragment.createWithMethodParameter(ast, builderField.getFieldType(), fieldName));
if (preferencesManager.getPreferenceValue(ADD_NONNULL_ON_RETURN)) {
markerAnnotationAttacher.attachNonNull(ast, builderMethod);
}
builderMethod.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
return builderMethod;
}
开发者ID:helospark,项目名称:SparkBuilderGenerator,代码行数:17,代码来源:StagedBuilderWithMethodDefiniationCreatorFragment.java
示例14: createStaticMethodInvocation
import org.eclipse.jdt.core.dom.AST; //导入依赖的package包/类
public MethodInvocation createStaticMethodInvocation(AST ast, String className, String methodName) {
SimpleName typeName = ast.newSimpleName(className);
MethodInvocation methodInvocation = ast.newMethodInvocation();
methodInvocation.setName(ast.newSimpleName(methodName));
methodInvocation.setExpression(typeName);
return methodInvocation;
}
示例15: createReturnBlock
import org.eclipse.jdt.core.dom.AST; //导入依赖的package包/类
public Block createReturnBlock(AST ast, TypeDeclaration builderType, String withName, String parameterName) {
Block builderMethodBlock = ast.newBlock();
ReturnStatement returnStatement = ast.newReturnStatement();
ClassInstanceCreation newClassInstanceCreation = ast.newClassInstanceCreation();
newClassInstanceCreation.setType(ast.newSimpleType(ast.newName(builderType.getName().toString())));
MethodInvocation withMethodInvocation = ast.newMethodInvocation();
withMethodInvocation.setExpression(newClassInstanceCreation);
withMethodInvocation.setName(ast.newSimpleName(withName));
withMethodInvocation.arguments().add(ast.newSimpleName(parameterName));
returnStatement.setExpression(withMethodInvocation);
builderMethodBlock.statements().add(returnStatement);
return builderMethodBlock;
}
开发者ID:helospark,项目名称:SparkBuilderGenerator,代码行数:16,代码来源:NewBuilderAndWithMethodCallCreationFragment.java