use of org.eclipse.jdt.core.dom.FieldDeclaration in project whole by wholeplatform.
the class EntityDescriptorEnumBuilder method addSimpleEntity.
// public void addSupertypes(String eType, String name, Set<String> supertypes) {
// if (supertypes.size()>1) {//!supertypes.isEmpty()) {
// ExpressionStatement expStm = etypeExpressionMap.get(eType);
// Expression edExp = expStm.getExpression();
//
// MethodInvocation nfd = newMethodInvocation("withSupertypes");
// expStm.setExpression(nfd);
// nfd.setExpression(edExp); // after unparenting edExp
//
// for (String sType : supertypes)
// if (!sType.equals(name))
// nfd.arguments().add(ast.newSimpleName(sType+"_ord"));
// }
// }
@SuppressWarnings("unchecked")
public void addSimpleEntity(String eName, String name, String eType, Set<String> modifiers, Set<String> allSubtypes) {
String eName_Ord = eName + "_ord";
String oldType = entities.put(eName, eType);
if (oldType == null) {
// public static final int [eName_ord] = [nextOrdinal];
FieldDeclaration fieldDecl = newFieldDeclaration("int", eName_Ord, newLiteral(nextOrdinal));
// assume ModifierKeyword.PRIVATE_KEYWORD
fieldDecl.modifiers().remove(0);
fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
addBodyDeclaration(nextOrdinal++, fieldDecl);
// public static final EntityDescriptor<eName> [eName] = (EntityDescriptor<eName>) instance.valueOf([eName_ord]);
fieldDecl = newFieldDeclaration(newParameterizedType(EntityDescriptor.class.getName(), eName), newVariableDeclarationFragment(eName, newCastExpression(newParameterizedType(EntityDescriptor.class.getName(), eName), newMethodInvocation("instance", "valueOf", ast.newSimpleName(eName_Ord)))));
// assume ModifierKeyword.PRIVATE_KEYWORD
fieldDecl.modifiers().remove(0);
fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
addBodyDeclaration(nextOrdinal * 2, fieldDecl);
} else if (oldType.equals(eType))
return;
// putSimpleEntity([eName_ord], "[eName]", [eType].class);
MethodInvocation callExp = newMethodInvocation("putSimpleEntity");
callExp.arguments().add(ast.newSimpleName(eName_Ord));
callExp.arguments().add(newLiteral(name));
if (!eName.equals(name))
callExp.arguments().add(newLiteral(eName));
callExp.arguments().add(newTypeLiteral(eType));
callExp.arguments().add(newLiteral(modifiers != null && modifiers.contains("ABSTRACT")));
if (!allSubtypes.isEmpty())
for (String type : allSubtypes) if (!type.equals(name))
callExp.arguments().add(ast.newSimpleName(type + "_ord"));
ExpressionStatement expStm = newExpressionStatement(callExp);
initEntityDescriptors.getBody().statements().add(expStm);
etypeExpressionMap.put(eType, expStm);
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project whole by wholeplatform.
the class EnumTypeBuilder method addValue.
public void addValue(String valueName, String valueImplName) {
String valueName_ord = valueImplName + "_ord";
String oldType = (String) values.put(valueImplName, valueType);
if (oldType == null) {
// public static final int [valueName_ord] = [nextOrdinal];
FieldDeclaration fieldDecl = newFieldDeclaration("int", valueName_ord, newLiteral(nextOrdinal));
// assume ModifierKeyword.PRIVATE_KEYWORD
fieldDecl.modifiers().remove(0);
fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
addBodyDeclaration(nextOrdinal++, fieldDecl);
// public static final Value [valueName] = instance.valueOf([valueName_ord]);
MethodInvocation callExp = newMethodInvocation("instance", "valueOf");
callExp.arguments().add(ast.newSimpleName(valueName_ord));
fieldDecl = newFieldDeclaration("Value", valueImplName, callExp);
// assume ModifierKeyword.PRIVATE_KEYWORD
fieldDecl.modifiers().remove(0);
fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
addBodyDeclaration(nextOrdinal * 2, fieldDecl);
// enumValue([valueName_ord], "[valueName]");
callExp = newMethodInvocation("enumValue");
callExp.arguments().add(ast.newSimpleName(valueName_ord));
callExp.arguments().add(newLiteral(valueName));
if (!valueName.equals(valueImplName))
callExp.arguments().add(newLiteral(valueImplName));
constructor.getBody().statements().add(ast.newExpressionStatement(callExp));
}
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project whole by wholeplatform.
the class CompilationUnitBuilder method addSingletonField.
public MethodDeclaration addSingletonField(String typeName, String implName) {
FieldDeclaration instanceField = newFieldDeclaration(typeName, "instance", newClassInstanceCreation(implName));
// assume ModifierKeyword.PRIVATE_KEYWORD
instanceField.modifiers().remove(0);
instanceField.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
instanceField.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
instanceField.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
addBodyDeclaration(0, instanceField);
MethodDeclaration constructor = null;
if (!isInterface) {
constructor = newConstructorDeclaration(typeDec);
// assume ModifierKeyword.PUBLIC_KEYWORD
constructor.modifiers().remove(0);
constructor.modifiers().add(ast.newModifier(ModifierKeyword.PRIVATE_KEYWORD));
addBodyDeclaration(1, constructor);
}
return constructor;
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project generator by mybatis.
the class JavaFileMerger method getMergedSource.
@SuppressWarnings({ "unchecked", "rawtypes" })
public String getMergedSource() throws ShellException, InvalidExistingFileException {
NewJavaFileVisitor newJavaFileVisitor = visitNewJavaFile();
IDocument document = new Document(existingJavaSource);
// delete generated stuff, and collect imports
ExistingJavaFileVisitor visitor = new ExistingJavaFileVisitor(javaDocTags);
CompilationUnit cu = getCompilationUnitFromSource(existingJavaSource);
AST ast = cu.getAST();
cu.recordModifications();
cu.accept(visitor);
TypeDeclaration typeDeclaration = visitor.getTypeDeclaration();
if (typeDeclaration == null) {
throw new InvalidExistingFileException(ErrorCode.NO_TYPES_DEFINED_IN_FILE);
}
// reconcile the superinterfaces
List<Type> newSuperInterfaces = getNewSuperInterfaces(typeDeclaration.superInterfaceTypes(), newJavaFileVisitor);
for (Type newSuperInterface : newSuperInterfaces) {
typeDeclaration.superInterfaceTypes().add(ASTNode.copySubtree(ast, newSuperInterface));
}
// set the superclass
if (newJavaFileVisitor.getSuperclass() != null) {
typeDeclaration.setSuperclassType((Type) ASTNode.copySubtree(ast, newJavaFileVisitor.getSuperclass()));
} else {
typeDeclaration.setSuperclassType(null);
}
// interface or class?
if (newJavaFileVisitor.isInterface()) {
typeDeclaration.setInterface(true);
} else {
typeDeclaration.setInterface(false);
}
// reconcile the imports
List<ImportDeclaration> newImports = getNewImports(cu.imports(), newJavaFileVisitor);
for (ImportDeclaration newImport : newImports) {
Name name = ast.newName(newImport.getName().getFullyQualifiedName());
ImportDeclaration newId = ast.newImportDeclaration();
newId.setName(name);
cu.imports().add(newId);
}
TextEdit textEdit = cu.rewrite(document, null);
try {
textEdit.apply(document);
} catch (BadLocationException e) {
throw new ShellException("BadLocationException removing prior fields and methods");
}
// regenerate the CompilationUnit to reflect all the deletes and changes
CompilationUnit strippedCu = getCompilationUnitFromSource(document.get());
// find the top level public type declaration
TypeDeclaration topLevelType = null;
Iterator iter = strippedCu.types().iterator();
while (iter.hasNext()) {
TypeDeclaration td = (TypeDeclaration) iter.next();
if (td.getParent().equals(strippedCu) && (td.getModifiers() & Modifier.PUBLIC) > 0) {
topLevelType = td;
break;
}
}
// now add all the new methods and fields to the existing
// CompilationUnit with a ListRewrite
ASTRewrite rewrite = ASTRewrite.create(topLevelType.getRoot().getAST());
ListRewrite listRewrite = rewrite.getListRewrite(topLevelType, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
Iterator<ASTNode> astIter = newJavaFileVisitor.getNewNodes().iterator();
int i = 0;
while (astIter.hasNext()) {
ASTNode node = astIter.next();
if (node.getNodeType() == ASTNode.TYPE_DECLARATION) {
String name = ((TypeDeclaration) node).getName().getFullyQualifiedName();
if (visitor.containsInnerClass(name)) {
continue;
}
} else if (node instanceof FieldDeclaration) {
addExistsAnnotations((BodyDeclaration) node, visitor.getFieldAnnotations((FieldDeclaration) node));
} else if (node instanceof MethodDeclaration) {
addExistsAnnotations((BodyDeclaration) node, visitor.getMethodAnnotations((MethodDeclaration) node));
}
listRewrite.insertAt(node, i++, null);
}
textEdit = rewrite.rewriteAST(document, JavaCore.getOptions());
try {
textEdit.apply(document);
} catch (BadLocationException e) {
throw new ShellException("BadLocationException adding new fields and methods");
}
String newSource = document.get();
return newSource;
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project flux by eclipse.
the class ScopeAnalyzer method addLocalDeclarations.
private boolean addLocalDeclarations(ASTNode node, int offset, int flags, IBindingRequestor requestor) {
if (hasFlag(VARIABLES, flags) || hasFlag(TYPES, flags)) {
BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(node);
if (declaration instanceof MethodDeclaration || declaration instanceof Initializer || declaration instanceof FieldDeclaration) {
ScopeAnalyzerVisitor visitor = new ScopeAnalyzerVisitor(offset, flags, requestor);
declaration.accept(visitor);
return visitor.fBreak;
}
}
return false;
}
Aggregations