List of usage examples for org.eclipse.jdt.core.dom AST newSuperConstructorInvocation
public SuperConstructorInvocation newSuperConstructorInvocation()
From source file:com.google.devtools.j2cpp.translate.AnonymousClassConverter.java
License:Open Source License
private void addArguments(List<Expression> invocationArguments, AST ast, ITypeBinding clazz, MethodDeclaration constructor, GeneratedMethodBinding binding) { // Create a parameter list, based on the invocation arguments. @SuppressWarnings("unchecked") // safe by definition List<SingleVariableDeclaration> parameters = constructor.parameters(); int parameterOffset = binding.getParameterTypes().length; for (int i = 0; i < invocationArguments.size(); i++) { Expression arg = invocationArguments.get(i); ITypeBinding argType = Types.getTypeBinding(arg); SimpleName paramName = ast.newSimpleName("arg$" + i); GeneratedVariableBinding paramBinding = new GeneratedVariableBinding(paramName.getIdentifier(), 0, argType, false, true, clazz, null); Types.addBinding(paramName, paramBinding); SingleVariableDeclaration param = ast.newSingleVariableDeclaration(); param.setName(paramName);//from w ww.ja v a2 s. c o m param.setType(Types.makeType(argType)); Types.addBinding(param, paramBinding); parameters.add(param); binding.addParameter(i + parameterOffset, argType); } // Add super constructor call, forwarding the invocation arguments. SuperConstructorInvocation superInvocation = ast.newSuperConstructorInvocation(); @SuppressWarnings("unchecked") List<Expression> superArgs = superInvocation.arguments(); // safe by definition for (SingleVariableDeclaration param : parameters) { superArgs.add(NodeCopier.copySubtree(ast, param.getName())); } Types.addBinding(superInvocation, findSuperConstructorBinding(clazz.getSuperclass(), invocationArguments)); @SuppressWarnings("unchecked") List<Statement> statements = constructor.getBody().statements(); // safe by definition statements.add(superInvocation); }
From source file:com.google.devtools.j2cpp.translate.ClassConverter.java
License:Open Source License
protected void addInnerParameters(MethodDeclaration constructor, GeneratedMethodBinding binding, List<IVariableBinding> innerFields, AST ast, boolean methodVars) { // Add parameters and initializers for each field. @SuppressWarnings("unchecked") // safe by definition List<SingleVariableDeclaration> parameters = constructor.parameters(); List<SingleVariableDeclaration> newParams = createConstructorArguments(innerFields, Types.getMethodBinding(constructor), ast, "outer$"); Block body = constructor.getBody();//from w w w. j ava 2 s . co m @SuppressWarnings("unchecked") // safe by definition List<Statement> statements = body.statements(); Statement first = statements.size() > 0 ? statements.get(0) : null; int offset = first != null && (first instanceof SuperConstructorInvocation || first instanceof ConstructorInvocation) ? 1 : 0; boolean firstIsThisCall = first != null && first instanceof ConstructorInvocation; // If superclass constructor takes an outer$ parameter, create or edit // an invocation for it first if (!innerFields.isEmpty() && !methodVars) { if (firstIsThisCall) { ConstructorInvocation thisCall = (ConstructorInvocation) first; IMethodBinding cons = Types.getMethodBinding(thisCall); GeneratedMethodBinding newCons = new GeneratedMethodBinding(cons.getMethodDeclaration()); // Create a new this invocation to the updated constructor. @SuppressWarnings("unchecked") List<Expression> args = ((ConstructorInvocation) first).arguments(); int index = 0; for (SingleVariableDeclaration param : newParams) { IVariableBinding paramBinding = Types.getVariableBinding(param); newCons.addParameter(index, Types.getTypeBinding(param)); args.add(index++, makeFieldRef(paramBinding, ast)); } Types.addBinding(thisCall, newCons); } else { ITypeBinding superType = binding.getDeclaringClass().getSuperclass().getTypeDeclaration(); if ((superType.getDeclaringClass() != null || superType.getDeclaringMethod() != null) && (superType.getModifiers() & Modifier.STATIC) == 0) { // There may be more than one outer var supplied, find the right one. IVariableBinding outerVar = null; for (SingleVariableDeclaration param : newParams) { IVariableBinding paramBinding = Types.getVariableBinding(param); if (paramBinding.getType().isAssignmentCompatible(superType.getDeclaringClass())) { outerVar = paramBinding; } } assert outerVar != null; IMethodBinding cons = null; if (offset > 0) { cons = Types.getMethodBinding(statements.get(0)); } else { for (IMethodBinding method : superType.getDeclaredMethods()) { // The super class's constructor may or may not have been already // modified. if (method.isConstructor()) { if (method.getParameterTypes().length == 0) { cons = method; break; } else if (method.getParameterTypes().length == 1 && outerVar.getType().isAssignmentCompatible(method.getParameterTypes()[0]) && method instanceof GeneratedMethodBinding) { cons = method; break; } } } } assert cons != null; if (!updatedConstructors.contains(cons)) { GeneratedMethodBinding newSuperCons = new GeneratedMethodBinding( cons.getMethodDeclaration()); newSuperCons.addParameter(0, superType.getDeclaringClass()); cons = newSuperCons; } SimpleName outerRef = makeFieldRef(outerVar, ast); SuperConstructorInvocation superInvocation = offset == 0 ? ast.newSuperConstructorInvocation() : (SuperConstructorInvocation) statements.get(0); @SuppressWarnings("unchecked") List<Expression> args = superInvocation.arguments(); // safe by definition args.add(0, outerRef); if (offset == 0) { statements.add(0, superInvocation); offset = 1; } Types.addBinding(superInvocation, cons); } } } for (int i = 0; i < newParams.size(); i++) { SingleVariableDeclaration parameter = newParams.get(i); // Only add an assignment statement for fields. if (innerFields.get(i).isField()) { statements.add(i + offset, createAssignment(innerFields.get(i), Types.getVariableBinding(parameter), ast)); } // Add methodVars at the end of the method invocation. if (methodVars) { parameters.add(parameter); binding.addParameter(Types.getVariableBinding(parameter)); } else { parameters.add(i, parameter); binding.addParameter(i, Types.getVariableBinding(parameter)); } } Symbols.scanAST(constructor); updatedConstructors.add(binding); assert constructor.parameters().size() == binding.getParameterTypes().length; }
From source file:com.google.devtools.j2cpp.translate.DeadCodeEliminator.java
License:Open Source License
/** * Adds a nullary constructor that invokes a superclass constructor with * default arguments.//from w ww . ja v a 2 s . com */ @SuppressWarnings("unchecked") private void generateConstructor(TypeDeclaration node) { ITypeBinding clazz = node.resolveBinding(); IMethodBinding superConstructor = getVisible(getConstructors(clazz.getSuperclass())).next(); // Add an explicit constructor that calls super with suitable default arguments. AST ast = node.getAST(); MethodDeclaration constructor = ast.newMethodDeclaration(); constructor.setConstructor(true); constructor.setName(ast.newSimpleName(node.getName().getIdentifier())); constructor.modifiers().add(ast.newModifier(ModifierKeyword.PROTECTED_KEYWORD)); node.bodyDeclarations().add(constructor); Block block = ast.newBlock(); constructor.setBody(block); SuperConstructorInvocation invocation = ast.newSuperConstructorInvocation(); block.statements().add(invocation); addAssertionError(block); for (ITypeBinding type : superConstructor.getParameterTypes()) { Expression value = getDefaultValue(ast, type); CastExpression cast = ast.newCastExpression(); cast.setExpression(value); cast.setType(createType(ast, clazz, type)); invocation.arguments().add(cast); } }
From source file:com.google.devtools.j2cpp.translate.InitializationNormalizer.java
License:Open Source License
void addDefaultConstructor(ITypeBinding type, List<BodyDeclaration> members, List<Statement> initStatements, AST ast) { SuperConstructorInvocation superCall = ast.newSuperConstructorInvocation(); GeneratedMethodBinding binding = new GeneratedMethodBinding(INIT_NAME, Modifier.PUBLIC, null, type.getSuperclass(), true, false, true); Types.addBinding(superCall, binding); initStatements.add(0, superCall);// w ww.j a v a2 s . co m addMethod(INIT_NAME, Modifier.PUBLIC, type, initStatements, members, ast, true); }
From source file:com.google.devtools.j2objc.translate.ASTFactory.java
License:Apache License
public static SuperConstructorInvocation newSuperConstructorInvocation(AST ast, IMethodBinding binding) { SuperConstructorInvocation invocation = ast.newSuperConstructorInvocation(); Types.addBinding(invocation, binding); return invocation; }
From source file:de.crowdcode.kissmda.cartridges.simplejava.ExceptionGenerator.java
License:Apache License
@SuppressWarnings("unchecked") public void generateConstructorWithParams(Classifier clazz, AST ast, TypeDeclaration td, String[] varTypes, String[] varNames) {// ww w . ja v a 2s . c o m MethodDeclaration constructor = ast.newMethodDeclaration(); constructor.setConstructor(true); constructor.setName(ast.newSimpleName(clazz.getName())); constructor.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); for (int index = 0; index < varTypes.length; index++) { SingleVariableDeclaration variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName(varTypes[index]))); variableDeclaration.setName(ast.newSimpleName(varNames[index])); constructor.parameters().add(variableDeclaration); } Block block = ast.newBlock(); SuperConstructorInvocation constructorInvocation = ast.newSuperConstructorInvocation(); for (int index = 0; index < varNames.length; index++) { constructorInvocation.arguments().add(ast.newSimpleName(varNames[index])); } block.statements().add(constructorInvocation); constructor.setBody(block); td.bodyDeclarations().add(constructor); }
From source file:org.jboss.forge.roaster.model.impl.statements.SuperStatementImpl.java
License:Open Source License
@Override public SuperConstructorInvocation materialize(AST ast) { if (invoke != null) { return invoke; }//from w w w . j av a2 s . c o m invoke = ast.newSuperConstructorInvocation(); for (Argument argument : argumentList) { invoke.arguments().add(wireAndGetExpression(argument, this, ast)); } return invoke; }