List of usage examples for org.eclipse.jdt.core.dom EnumConstantDeclaration setAnonymousClassDeclaration
public void setAnonymousClassDeclaration(AnonymousClassDeclaration decl)
From source file:com.google.devtools.j2cpp.translate.AnonymousClassConverter.java
License:Open Source License
/** * Convert the anonymous class into an inner class. Fields are added for * final variables that are referenced, and a constructor is added. * * Note: endVisit is used for a depth-first traversal, to make it easier * to scan their containing nodes for references. *///from w w w.j a v a 2 s.c o m @Override @SuppressWarnings("unchecked") public void endVisit(AnonymousClassDeclaration node) { ASTNode parent = node.getParent(); ClassInstanceCreation newInvocation = null; EnumConstantDeclaration enumConstant = null; List<Expression> parentArguments; String newClassName; ITypeBinding innerType; boolean isStatic = staticParent(node); int modifiers = isStatic ? Modifier.STATIC : 0; if (parent instanceof ClassInstanceCreation) { newInvocation = (ClassInstanceCreation) parent; parentArguments = newInvocation.arguments(); innerType = Types.getTypeBinding(newInvocation); newClassName = innerType.getName(); innerType = RenamedTypeBinding.rename(newClassName, innerType.getDeclaringClass(), innerType, modifiers); } else if (parent instanceof EnumConstantDeclaration) { enumConstant = (EnumConstantDeclaration) parent; parentArguments = enumConstant.arguments(); innerType = Types.getTypeBinding(node); newClassName = Types.getTypeBinding(node).getName(); innerType = RenamedTypeBinding.rename(newClassName, innerType.getDeclaringClass(), innerType, modifiers); } else { throw new AssertionError("unknown anonymous class declaration parent: " + parent.getClass().getName()); } // Create a type declaration for this anonymous class. AST ast = node.getAST(); TypeDeclaration typeDecl = ast.newTypeDeclaration(); if (isStatic) { typeDecl.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD)); } Types.addBinding(typeDecl, innerType); typeDecl.setName(ast.newSimpleName(newClassName)); Types.addBinding(typeDecl.getName(), innerType); typeDecl.setSourceRange(node.getStartPosition(), node.getLength()); Type superType = Types.makeType(Types.mapType(innerType.getSuperclass())); typeDecl.setSuperclassType(superType); for (ITypeBinding interfaceType : innerType.getInterfaces()) { typeDecl.superInterfaceTypes().add(Types.makeType(Types.mapType(interfaceType))); } for (Object bodyDecl : node.bodyDeclarations()) { BodyDeclaration decl = (BodyDeclaration) bodyDecl; typeDecl.bodyDeclarations().add(NodeCopier.copySubtree(ast, decl)); } typeDecl.accept(new InitializationNormalizer()); // Fix up references to external types, if necessary. Set<IVariableBinding> methodVars = getMethodVars(node); final List<ReferenceDescription> references = findReferences(node, methodVars); final List<Expression> invocationArgs = parentArguments; if (!references.isEmpty() || !invocationArgs.isEmpty()) { // is there anything to fix-up? List<IVariableBinding> innerVars = getInnerVars(references); if (!innerVars.isEmpty() || !invocationArgs.isEmpty()) { GeneratedMethodBinding defaultConstructor = addInnerVars(typeDecl, innerVars, references, parentArguments); Types.addBinding(parent, defaultConstructor); for (IVariableBinding var : innerVars) { if (!isConstant(var)) { parentArguments.add(makeFieldRef(var, ast)); } } assert defaultConstructor.getParameterTypes().length == parentArguments.size(); typeDecl.accept(new ASTVisitor() { @Override public void endVisit(SimpleName node) { IVariableBinding var = Types.getVariableBinding(node); if (var != null) { for (ReferenceDescription ref : references) { if (var.isEqualTo(ref.binding)) { if (ref.innerField != null) { setProperty(node, makeFieldRef(ref.innerField, node.getAST())); } else { // In-line constant. Object o = var.getConstantValue(); assert o != null; Expression literal = makeLiteral(o, var.getType().getQualifiedName(), node.getAST()); setProperty(node, literal); } return; } } } } }); } } // If invocation, replace anonymous class invocation with the new constructor. if (newInvocation != null) { newInvocation.setAnonymousClassDeclaration(null); newInvocation.setType(Types.makeType(innerType)); IMethodBinding oldBinding = Types.getMethodBinding(newInvocation); if (oldBinding == null) { oldBinding = newInvocation.resolveConstructorBinding(); } if (oldBinding != null) { GeneratedMethodBinding invocationBinding = new GeneratedMethodBinding(oldBinding); invocationBinding.setDeclaringClass(innerType); Types.addBinding(newInvocation, invocationBinding); } } else { enumConstant.setAnonymousClassDeclaration(null); } // Add type declaration to enclosing type. ITypeBinding outerType = innerType.getDeclaringClass(); if (outerType.isAnonymous()) { // Get outerType node. ASTNode n = parent.getParent(); while (!(n instanceof AnonymousClassDeclaration) && !(n instanceof TypeDeclaration)) { n = n.getParent(); } if (n instanceof AnonymousClassDeclaration) { AnonymousClassDeclaration outerDecl = (AnonymousClassDeclaration) n; outerDecl.bodyDeclarations().add(typeDecl); } } else { AbstractTypeDeclaration outerDecl = (AbstractTypeDeclaration) unit.findDeclaringNode(outerType); outerDecl.bodyDeclarations().add(typeDecl); } Symbols.scanAST(typeDecl); super.endVisit(node); }
From source file:java5totext.input.JDTVisitor.java
License:Open Source License
@Override public void endVisit(org.eclipse.jdt.core.dom.EnumConstantDeclaration node) { EnumConstantDeclaration element = (EnumConstantDeclaration) this.binding.get(node); this.initializeNode(element, node); element.setName(node.getName().getIdentifier()); if (this.binding.get(node.getAnonymousClassDeclaration()) != null) element.setAnonymousClassDeclaration( (AnonymousClassDeclaration) this.binding.get(node.getAnonymousClassDeclaration())); for (Iterator<?> i = node.arguments().iterator(); i.hasNext();) { Expression itElement = (Expression) this.binding.get(i.next()); if (itElement != null) element.getArguments().add(itElement); }//w ww .j av a 2s. co m endVisitBD(node, element); JDTVisitorUtils.manageBindingDeclaration(element, node.getName(), this); }
From source file:org.decojer.cavaj.transformers.TrExpressions.java
License:Open Source License
/** * Rewrite PUT-access to a class (static) or instance field. * * @param bb/*from w ww . j a v a 2 s.co m*/ * current BB * @param f * lass (static) or instance field * @param rightOperand * right operand expression * @return {@code true} - success */ private boolean rewriteFieldInit(@Nonnull final BB bb, @Nonnull final F f, @Nonnull final Expression rightOperand) { if (!isFieldInit()) { return false; } if (!f.isDeclaration()) { return false; } final T ownerT = getM().getT(); if (ownerT == null) { return false; } if (!ownerT.is(f.getT())) { return false; } // set local field, could be initializer if (f.isStatic()) { if (!getM().isInitializer()) { return false; } if (getCfg().getStartBb() != bb || bb.getStmts() > 0) { return false; } if (f.getAstNode() == null) { // synthetic has been recognized before in TrJvmStruct2JavaAst, ignore assignment return true; } if (f.isEnum() && f.getT().isEnum() && !getCfg().getCu().check(DFlag.IGNORE_ENUM)) { // assignment to enum constant declaration if (!(rightOperand instanceof ClassInstanceCreation)) { log.warn(getM() + ": Enum field initialization for '" + f + "' has no class instance creation as operand!"); return false; } final ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) rightOperand; // first two arguments must be String (== field name) and int (ordinal) final List<Expression> arguments = classInstanceCreation.arguments(); if (arguments.size() < 2) { log.warn(getM() + ": Enum field initialization for '" + f + "' has less than 2 arguments!"); return false; } if (!(arguments.get(0) instanceof StringLiteral)) { log.warn(getM() + ": Enum field initialization for '" + f + "' has no string literal as first parameter!"); return false; } final String literalValue = ((StringLiteral) arguments.get(0)).getLiteralValue(); if (!literalValue.equals(f.getName())) { log.warn(getM() + ": Enum field initialization for '" + f + "' has no string literal equal to field name as first parameter!"); return false; } if (!(arguments.get(1) instanceof NumberLiteral)) { log.warn(getM() + ": Enum field initialization for '" + f + "' has no number literal as first parameter!"); return false; } final Object astNode = f.getAstNode(); if (!(astNode instanceof EnumConstantDeclaration)) { log.warn(getM() + ": Enum field initialization for '" + f + "' has no EnumConstantDeclaration as AST node!"); return false; } final EnumConstantDeclaration enumConstantDeclaration = (EnumConstantDeclaration) astNode; for (int i = arguments.size(); i-- > 2;) { final Expression e = arguments.get(i); e.delete(); enumConstantDeclaration.arguments().add(0, e); } final AnonymousClassDeclaration anonymousClassDeclaration = classInstanceCreation .getAnonymousClassDeclaration(); if (anonymousClassDeclaration != null) { final Element declarationForNode = f.getT().getCu() .getDeclarationForNode(anonymousClassDeclaration); if (declarationForNode == null) { log.warn(getM() + ": Enum field initialization for '" + f + "' with anonymous declaration has no declaration node!"); } else { anonymousClassDeclaration.delete(); enumConstantDeclaration.setAnonymousClassDeclaration(anonymousClassDeclaration); // normally contains one constructor, that calls a synthetic super // constructor with the enum class as additional last parameter, // this may contain field initializers, that we must keep, // so we can only remove the constructor in final merge (because // anonymous inner classes cannot have visible Java constructor) declarationForNode.setDeclarationOwner(f); } } return true; } } else { if (!getM().isConstructor()) { return false; } if (!(bb.peek() instanceof ThisExpression)) { return false; } if (getCfg().getStartBb() != bb || bb.getStmts() > 1 || bb.getStmts() == 1 && !(bb.getStmt(0) instanceof SuperConstructorInvocation)) { // initial super(<arguments>) is allowed for constructors return false; } if (f.getAstNode() == null) { // synthetic has been recognized before in TrJvmStruct2JavaAst, ignore assignment bb.pop(); return true; } // multiple constructors with different signatures possible, all of them // contain the same field initializer code after super() - simply overwrite } final Object astNode = f.getAstNode(); if (!(astNode instanceof FieldDeclaration)) { log.warn(getM() + ": Field initialization for '" + f + "' has no FieldDeclaration as AST node!"); return false; } ((VariableDeclarationFragment) ((FieldDeclaration) astNode).fragments().get(0)) .setInitializer(wrap(rightOperand, Priority.ASSIGNMENT)); // TODO move anonymous TD to FD as child!!! important for ClassEditor // select, if fixed change ClassEditor#findDeclarationForJavaElement too if (!f.isStatic()) { bb.pop(); } return true; }
From source file:org.eclipse.jdt.core.dom.ASTConverter.java
License:Open Source License
public EnumConstantDeclaration convert(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration enumConstant) { checkCanceled();/*ww w . j av a 2s. c o m*/ EnumConstantDeclaration enumConstantDeclaration = new EnumConstantDeclaration(this.ast); final SimpleName typeName = new SimpleName(this.ast); typeName.internalSetIdentifier(new String(enumConstant.name)); typeName.setSourceRange(enumConstant.sourceStart, enumConstant.sourceEnd - enumConstant.sourceStart + 1); enumConstantDeclaration.setName(typeName); int declarationSourceStart = enumConstant.declarationSourceStart; int declarationSourceEnd = enumConstant.declarationSourceEnd; final org.eclipse.jdt.internal.compiler.ast.Expression initialization = enumConstant.initialization; if (initialization != null) { if (initialization instanceof QualifiedAllocationExpression) { org.eclipse.jdt.internal.compiler.ast.TypeDeclaration anonymousType = ((QualifiedAllocationExpression) initialization).anonymousType; if (anonymousType != null) { AnonymousClassDeclaration anonymousClassDeclaration = new AnonymousClassDeclaration(this.ast); int start = retrieveStartBlockPosition(anonymousType.sourceEnd, anonymousType.bodyEnd); int end = retrieveRightBrace(anonymousType.bodyEnd, declarationSourceEnd); if (end == -1) end = anonymousType.bodyEnd; anonymousClassDeclaration.setSourceRange(start, end - start + 1); enumConstantDeclaration.setAnonymousClassDeclaration(anonymousClassDeclaration); buildBodyDeclarations(anonymousType, anonymousClassDeclaration); if (this.resolveBindings) { recordNodes(anonymousClassDeclaration, anonymousType); anonymousClassDeclaration.resolveBinding(); } enumConstantDeclaration.setSourceRange(declarationSourceStart, end - declarationSourceStart + 1); } } else { enumConstantDeclaration.setSourceRange(declarationSourceStart, declarationSourceEnd - declarationSourceStart + 1); } final org.eclipse.jdt.internal.compiler.ast.Expression[] arguments = ((org.eclipse.jdt.internal.compiler.ast.AllocationExpression) initialization).arguments; if (arguments != null) { for (int i = 0, max = arguments.length; i < max; i++) { enumConstantDeclaration.arguments().add(convert(arguments[i])); } } } else { enumConstantDeclaration.setSourceRange(declarationSourceStart, declarationSourceEnd - declarationSourceStart + 1); } setModifiers(enumConstantDeclaration, enumConstant); if (this.resolveBindings) { recordNodes(enumConstantDeclaration, enumConstant); recordNodes(typeName, enumConstant); enumConstantDeclaration.resolveVariable(); } convert(enumConstant.javadoc, enumConstantDeclaration); return enumConstantDeclaration; }
From source file:org.eclipse.modisco.java.discoverer.internal.io.java.JDTVisitor.java
License:Open Source License
@Override public void endVisit(final org.eclipse.jdt.core.dom.EnumConstantDeclaration node) { EnumConstantDeclaration element = (EnumConstantDeclaration) this.binding.get(node); initializeNode(element, node);/*from ww w. j a v a 2 s .c o m*/ element.setName(node.getName().getIdentifier()); if (this.binding.get(node.getAnonymousClassDeclaration()) != null) { element.setAnonymousClassDeclaration( (AnonymousClassDeclaration) this.binding.get(node.getAnonymousClassDeclaration())); } for (Iterator<?> i = node.arguments().iterator(); i.hasNext();) { ASTNode itElement = this.binding.get(i.next()); if (itElement != null) { element.getArguments().add(JDTVisitorUtils.completeExpression(itElement, this)); } } endVisitBD(node, element); JDTVisitorUtils.manageBindingDeclaration(element, node.getName(), this); }
From source file:org.jboss.forge.roaster.model.impl.EnumConstantBodyImpl.java
License:Open Source License
AnonymousClassDeclaration getBody() { final EnumConstantDeclaration enumConstantDeclaration = (EnumConstantDeclaration) enumConstant .getInternal();//from www. j av a 2 s . c o m synchronized (enumConstantDeclaration) { AnonymousClassDeclaration result = enumConstantDeclaration.getAnonymousClassDeclaration(); if (result == null) { final String stub = "enum StubEnum { FOO() {}; }"; final JavaEnumSource temp = Roaster.parse(JavaEnumSource.class, stub); final AnonymousClassDeclaration body = ((EnumConstantBodyImpl) temp.getEnumConstants().get(0) .getBody()).getBody(); final AST ast = ((ASTNode) javaEnum.getInternal()).getAST(); result = (AnonymousClassDeclaration) ASTNode.copySubtree(ast, body); enumConstantDeclaration.setAnonymousClassDeclaration(result); } return result; } }
From source file:org.whole.lang.java.util.JDTTransformerVisitor.java
License:Open Source License
@Override public boolean visit(EnumConstantDeclaration node) { org.whole.lang.java.model.EnumConstantDeclaration enumConstantDeclaration; appendBodyDeclaration(/*from www . ja v a 2 s .c o m*/ enumConstantDeclaration = createResolver(JavaEntityDescriptorEnum.EnumConstantDeclaration)); if (acceptChild(node.getJavadoc())) enumConstantDeclaration.setJavadoc(this.javadoc); List<?> modifiers = node.modifiers(); if (!modifiers.isEmpty()) { enumConstantDeclaration.setModifiers(lf.create(JavaEntityDescriptorEnum.ExtendedModifiers)); setExtendedModifiers(enumConstantDeclaration.getModifiers(), modifiers); } if (acceptChild(node.getName())) enumConstantDeclaration.setName((org.whole.lang.java.model.SimpleName) this.name); Iterator<?> i = node.arguments().iterator(); while (i.hasNext()) { ((ASTNode) i.next()).accept(this); enumConstantDeclaration.getArguments().wAdd(this.exp); } if (acceptChild(node.getAnonymousClassDeclaration())) enumConstantDeclaration.setAnonymousClassDeclaration(this.anonymousClassDeclaration); return false; }