List of usage examples for org.eclipse.jdt.core.dom AST newParameterizedType
public ParameterizedType newParameterizedType(Type type)
From source file:com.google.devtools.j2cpp.translate.DeadCodeEliminator.java
License:Open Source License
/** * Create a new Type for the given type binding. If the binding * represents a parameterized type reference, the returned Type * is a ParameterizedType with the same type parameters. Otherwise * the returned Type is a SimpleType.//from w w w . j av a 2 s .c o m */ @SuppressWarnings("unchecked") private Type createType(AST ast, ITypeBinding scope, ITypeBinding type) { Type newType; if (type.isArray()) { newType = ast.newArrayType(createType(ast, scope, type.getElementType()), type.getDimensions()); } else if (type.isPrimitive()) { newType = ast.newPrimitiveType(PrimitiveType.toCode(type.getName())); } else if (type.isWildcardType()) { WildcardType wildType = ast.newWildcardType(); ITypeBinding bound = type.getBound(); if (bound != null) { wildType.setBound(createType(ast, scope, bound), type.isUpperbound()); } newType = wildType; } else if (!type.isParameterizedType()) { String name = inScope(type, scope) ? type.getName() : type.getQualifiedName(); newType = ast.newSimpleType(ast.newName(name)); } else { ITypeBinding erasure = type.getErasure(); String name = inScope(type, scope) ? erasure.getName() : erasure.getQualifiedName(); Type rawType = ast.newSimpleType(ast.newName(name)); ParameterizedType paramType = ast.newParameterizedType(rawType); ITypeBinding[] typeArgs = type.getTypeArguments(); for (ITypeBinding param : typeArgs) { paramType.typeArguments().add(createType(ast, scope, param)); } newType = paramType; } generatedTypes.put(newType, type); return newType; }
From source file:com.google.gdt.eclipse.designer.builders.GwtBuilder.java
License:Open Source License
/** * Generates Async type for given <code>RemoteService</code>. */// www .j av a 2s.co m private void generateAsync(IPackageFragment servicePackage, ICompilationUnit serviceUnit) throws Exception { IJavaProject javaProject = serviceUnit.getJavaProject(); // parse service unit CompilationUnit serviceRoot = Utils.parseUnit(serviceUnit); // prepare AST and start modifications recording AST ast = serviceRoot.getAST(); serviceRoot.recordModifications(); // modify imports (-com.google.gwt.*, -*Exception, +AsyncCallback) { List<ImportDeclaration> imports = DomGenerics.imports(serviceRoot); // remove useless imports for (Iterator<ImportDeclaration> I = imports.iterator(); I.hasNext();) { ImportDeclaration importDeclaration = I.next(); String importName = importDeclaration.getName().getFullyQualifiedName(); if (importName.startsWith("com.google.gwt.user.client.rpc.") || importName.equals("com.google.gwt.core.client.GWT") || importName.endsWith("Exception")) { I.remove(); } } } // add Async to the name TypeDeclaration serviceType = (TypeDeclaration) serviceRoot.types().get(0); String remoteServiceAsyncName = serviceType.getName().getIdentifier() + "Async"; serviceType.setName(serviceRoot.getAST().newSimpleName(remoteServiceAsyncName)); // update interfaces updateInterfacesOfAsync(javaProject, serviceRoot, serviceType); // change methods, fields and inner classes { List<BodyDeclaration> bodyDeclarations = DomGenerics.bodyDeclarations(serviceType); for (Iterator<BodyDeclaration> I = bodyDeclarations.iterator(); I.hasNext();) { BodyDeclaration bodyDeclaration = I.next(); if (bodyDeclaration instanceof MethodDeclaration) { MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclaration; // make return type void Type returnType; { returnType = methodDeclaration.getReturnType2(); methodDeclaration.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID)); } // process JavaDoc { Javadoc javadoc = methodDeclaration.getJavadoc(); if (javadoc != null) { List<TagElement> tags = DomGenerics.tags(javadoc); for (Iterator<TagElement> tagIter = tags.iterator(); tagIter.hasNext();) { TagElement tag = tagIter.next(); if ("@gwt.typeArgs".equals(tag.getTagName())) { tagIter.remove(); } else if ("@return".equals(tag.getTagName())) { if (!tag.fragments().isEmpty()) { tag.setTagName("@param callback the callback to return"); } else { tagIter.remove(); } } else if ("@wbp.gwt.Request".equals(tag.getTagName())) { tagIter.remove(); addImport(serviceRoot, "com.google.gwt.http.client.Request"); methodDeclaration.setReturnType2(ast.newSimpleType(ast.newName("Request"))); } } // remove empty JavaDoc if (tags.isEmpty()) { methodDeclaration.setJavadoc(null); } } } // add AsyncCallback parameter { addImport(serviceRoot, "com.google.gwt.user.client.rpc.AsyncCallback"); // prepare "callback" type Type callbackType; { callbackType = ast.newSimpleType(ast.newName("AsyncCallback")); Type objectReturnType = getObjectType(returnType); ParameterizedType parameterizedType = ast.newParameterizedType(callbackType); DomGenerics.typeArguments(parameterizedType).add(objectReturnType); callbackType = parameterizedType; } // prepare "callback" parameter SingleVariableDeclaration asyncCallback = ast.newSingleVariableDeclaration(); asyncCallback.setType(callbackType); asyncCallback.setName(ast.newSimpleName("callback")); // add "callback" parameter DomGenerics.parameters(methodDeclaration).add(asyncCallback); } // remove throws methodDeclaration.thrownExceptions().clear(); } else if (bodyDeclaration instanceof FieldDeclaration || bodyDeclaration instanceof TypeDeclaration) { // remove the fields and inner classes I.remove(); } } } // apply modifications to prepare new source code String newSource; { String source = serviceUnit.getBuffer().getContents(); Document document = new Document(source); // prepare text edits MultiTextEdit edits = (MultiTextEdit) serviceRoot.rewrite(document, javaProject.getOptions(true)); removeAnnotations(serviceType, source, edits); // prepare new source code edits.apply(document); newSource = document.get(); } // update compilation unit { ICompilationUnit unit = servicePackage.createCompilationUnit(remoteServiceAsyncName + ".java", newSource, true, null); unit.getBuffer().save(null, true); } }
From source file:com.google.gwt.eclipse.core.util.Util.java
License:Open Source License
/** * Creates a GWT RPC async callback parameter declaration based on the sync * method return type./*from w w w . j a v a 2 s . c o m*/ * * @param ast {@link AST} associated with the destination compilation unit * @param syncReturnType the sync method return type * @param callbackParameterName name of the callback parameter * @param imports {@link ImportsRewrite} for the destination compilation unit * @return callback paramter declaration */ @SuppressWarnings("unchecked") public static SingleVariableDeclaration createAsyncCallbackParameter(AST ast, Type syncReturnType, String callbackParameterName, ImportRewrite imports) { ITypeBinding syncReturnTypeBinding = syncReturnType.resolveBinding(); SingleVariableDeclaration parameter = ast.newSingleVariableDeclaration(); String gwtCallbackTypeSig = Signature .createTypeSignature(RemoteServiceUtilities.ASYNCCALLBACK_QUALIFIED_NAME, true); Type gwtCallbackType = imports.addImportFromSignature(gwtCallbackTypeSig, ast); if (syncReturnTypeBinding.isPrimitive()) { String wrapperName = JavaASTUtils.getWrapperTypeName(syncReturnTypeBinding.getName()); String wrapperTypeSig = Signature.createTypeSignature(wrapperName, true); syncReturnType = imports.addImportFromSignature(wrapperTypeSig, ast); } else { syncReturnType = JavaASTUtils.normalizeTypeAndAddImport(ast, syncReturnType, imports); } ParameterizedType type = ast.newParameterizedType(gwtCallbackType); List<Type> typeArgs = type.typeArguments(); typeArgs.add(syncReturnType); parameter.setType(type); parameter.setName(ast.newSimpleName(callbackParameterName)); return parameter; }
From source file:com.idega.eclipse.ejbwizards.BeanCreator.java
License:Open Source License
protected Type getType(AST ast, String type) { Type returnType = null;/*from w ww . j a v a2s .com*/ if (type != null) { boolean isArray = false; boolean isGenericType = false; if (type.indexOf("[") != -1) { isArray = true; } if (type.indexOf("<") != -1) { isGenericType = true; } try { if (isArray) { returnType = ast.newArrayType( ast.newSimpleType(ast.newSimpleName(type.substring(0, type.indexOf("["))))); } else if (isGenericType) { returnType = ast.newParameterizedType( ast.newSimpleType(ast.newSimpleName(type.substring(0, type.indexOf("<"))))); StringTokenizer tokens = new StringTokenizer( type.substring(type.indexOf("<") + 1, type.indexOf(">")), ","); while (tokens.hasMoreTokens()) { ((ParameterizedType) returnType).typeArguments() .add(getType(ast, tokens.nextToken().trim())); } } else { returnType = ast.newSimpleType(ast.newSimpleName(type)); } } catch (IllegalArgumentException iae) { if (isArray) { returnType = ast.newArrayType( ast.newPrimitiveType(PrimitiveType.toCode(type.substring(0, type.indexOf("["))))); } else { returnType = ast.newPrimitiveType(PrimitiveType.toCode(type)); } } } else { returnType = ast.newPrimitiveType(PrimitiveType.VOID); } return returnType; }
From source file:com.idega.eclipse.ejbwizards.IBOEntityCreator.java
License:Open Source License
private void createHomeImplementation(IProgressMonitor monitor, ICompilationUnit iUnit, String typePackage, String name) throws JavaModelException, MalformedTreeException, BadLocationException { iUnit.getBuffer().setContents(""); String source = iUnit.getBuffer().getContents(); Document document = new Document(source); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(iUnit);/*w ww .j av a 2 s . c o m*/ CompilationUnit unit = (CompilationUnit) parser.createAST(monitor); unit.recordModifications(); AST ast = unit.getAST(); // Package statement PackageDeclaration packageDeclaration = ast.newPackageDeclaration(); unit.setPackage(packageDeclaration); packageDeclaration.setName(ast.newName(typePackage)); // class declaration TypeDeclaration classType = getTypeDeclaration(ast, name + "HomeImpl", false, "IBOHomeImpl", null, getHomeImplImports()); classType.superInterfaceTypes().add(ast.newSimpleType(ast.newSimpleName(name + "Home"))); addHomeImplImport("com.idega.business.IBOHomeImpl"); unit.types().add(classType); // create() method MethodDeclaration methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); if (this.isJDK1_5) { MarkerAnnotation annotation = ast.newMarkerAnnotation(); annotation.setTypeName(ast.newSimpleName("Override")); methodConstructor.modifiers().add(annotation); } methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); if (this.isJDK1_5) { ParameterizedType returnType = ast.newParameterizedType(ast.newSimpleType(ast.newSimpleName("Class"))); returnType.typeArguments().add(ast.newSimpleType(ast.newSimpleName(name))); methodConstructor.setReturnType2(returnType); } else { methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName("Class"))); } methodConstructor.setName(ast.newSimpleName("getBeanInterfaceClass")); classType.bodyDeclarations().add(methodConstructor); Block constructorBlock = ast.newBlock(); methodConstructor.setBody(constructorBlock); TypeLiteral typeLiteral = ast.newTypeLiteral(); typeLiteral.setType(ast.newSimpleType(ast.newSimpleName(name))); ReturnStatement returnStatement = ast.newReturnStatement(); returnStatement.setExpression(typeLiteral); constructorBlock.statements().add(returnStatement); // create() method methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name))); methodConstructor.setName(ast.newSimpleName("create")); methodConstructor.thrownExceptions().add(ast.newName("CreateException")); addHomeImplImport("javax.ejb.CreateException"); classType.bodyDeclarations().add(methodConstructor); constructorBlock = ast.newBlock(); methodConstructor.setBody(constructorBlock); SuperMethodInvocation superMethodInvocation = ast.newSuperMethodInvocation(); superMethodInvocation.setName(ast.newSimpleName("createIBO")); CastExpression ce = ast.newCastExpression(); ce.setType(ast.newSimpleType(ast.newSimpleName(name))); ce.setExpression(superMethodInvocation); returnStatement = ast.newReturnStatement(); returnStatement.setExpression(ce); constructorBlock.statements().add(returnStatement); writeImports(ast, unit, getHomeImplImports()); commitChanges(iUnit, unit, document); }
From source file:com.idega.eclipse.ejbwizards.IDOEntityCreator.java
License:Open Source License
private void createHomeImplementation(IProgressMonitor monitor, ICompilationUnit iUnit, String typePackage, String name) throws JavaModelException, MalformedTreeException, BadLocationException { iUnit.getBuffer().setContents(""); String source = iUnit.getBuffer().getContents(); Document document = new Document(source); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(iUnit);/*from w w w.j a v a2 s . c o m*/ CompilationUnit unit = (CompilationUnit) parser.createAST(monitor); unit.recordModifications(); AST ast = unit.getAST(); // Package statement unit.setPackage(getPackageDeclaration(ast, typePackage)); // class declaration TypeDeclaration classType = getTypeDeclaration(ast, name + "HomeImpl", false, "IDOFactory", null, getHomeImplImports()); classType.superInterfaceTypes().add(ast.newSimpleType(ast.newSimpleName(name + "Home"))); addHomeImplImport("com.idega.data.IDOFactory"); unit.types().add(classType); // create() method MethodDeclaration methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); if (this.isJDK1_5) { MarkerAnnotation annotation = ast.newMarkerAnnotation(); annotation.setTypeName(ast.newSimpleName("Override")); methodConstructor.modifiers().add(annotation); } methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); if (this.isJDK1_5) { ParameterizedType returnType = ast.newParameterizedType(ast.newSimpleType(ast.newSimpleName("Class"))); returnType.typeArguments().add(ast.newSimpleType(ast.newSimpleName(name + "Home"))); methodConstructor.setReturnType2(returnType); } else { methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName("Class"))); } methodConstructor.setName(ast.newSimpleName("getEntityInterfaceClass")); classType.bodyDeclarations().add(methodConstructor); Block constructorBlock = ast.newBlock(); methodConstructor.setBody(constructorBlock); TypeLiteral typeLiteral = ast.newTypeLiteral(); typeLiteral.setType(ast.newSimpleType(ast.newSimpleName(name))); ReturnStatement returnStatement = ast.newReturnStatement(); returnStatement.setExpression(typeLiteral); constructorBlock.statements().add(returnStatement); // create() method methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name))); methodConstructor.setName(ast.newSimpleName("create")); methodConstructor.thrownExceptions().add(ast.newName("CreateException")); addHomeImplImport("javax.ejb.CreateException"); classType.bodyDeclarations().add(methodConstructor); constructorBlock = ast.newBlock(); methodConstructor.setBody(constructorBlock); SuperMethodInvocation superMethodInvocation = ast.newSuperMethodInvocation(); superMethodInvocation.setName(ast.newSimpleName("createIDO")); CastExpression ce = ast.newCastExpression(); ce.setType(ast.newSimpleType(ast.newSimpleName(name))); ce.setExpression(superMethodInvocation); returnStatement = ast.newReturnStatement(); returnStatement.setExpression(ce); constructorBlock.statements().add(returnStatement); // findByPrimarKey(Object) method methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name))); methodConstructor.setName(ast.newSimpleName("findByPrimaryKey")); methodConstructor.thrownExceptions().add(ast.newName("FinderException")); addHomeImplImport("javax.ejb.FinderException"); classType.bodyDeclarations().add(methodConstructor); SingleVariableDeclaration variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE)); variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName("Object"))); variableDeclaration.setName(ast.newSimpleName("pk")); methodConstructor.parameters().add(variableDeclaration); constructorBlock = ast.newBlock(); methodConstructor.setBody(constructorBlock); superMethodInvocation = ast.newSuperMethodInvocation(); superMethodInvocation.setName(ast.newSimpleName("findByPrimaryKeyIDO")); superMethodInvocation.arguments().add(ast.newSimpleName("pk")); ce = ast.newCastExpression(); ce.setType(ast.newSimpleType(ast.newSimpleName(name))); ce.setExpression(superMethodInvocation); returnStatement = ast.newReturnStatement(); returnStatement.setExpression(ce); constructorBlock.statements().add(returnStatement); if (this.isLegacyEntity) { // createLegacy() method methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name))); methodConstructor.setName(ast.newSimpleName("createLegacy")); classType.bodyDeclarations().add(methodConstructor); constructorBlock = ast.newBlock(); methodConstructor.setBody(constructorBlock); TryStatement tryStatement = ast.newTryStatement(); constructorBlock.statements().add(tryStatement); Block tryBlock = ast.newBlock(); tryStatement.setBody(tryBlock); MethodInvocation mi = ast.newMethodInvocation(); mi.setName(ast.newSimpleName("create")); returnStatement = ast.newReturnStatement(); returnStatement.setExpression(mi); tryBlock.statements().add(returnStatement); CatchClause catchClause = ast.newCatchClause(); tryStatement.catchClauses().add(catchClause); variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE)); variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName(("CreateException")))); variableDeclaration.setName(ast.newSimpleName("ce")); catchClause.setException(variableDeclaration); Block catchBlock = ast.newBlock(); catchClause.setBody(catchBlock); ClassInstanceCreation cc = ast.newClassInstanceCreation(); cc.setType(ast.newSimpleType(ast.newSimpleName("RuntimeException"))); mi = ast.newMethodInvocation(); mi.setExpression(ast.newSimpleName("ce")); mi.setName(ast.newSimpleName("getMessage")); cc.arguments().add(mi); ThrowStatement throwStatement = ast.newThrowStatement(); throwStatement.setExpression(cc); catchBlock.statements().add(throwStatement); // findByPrimarKey(int) method methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name))); methodConstructor.setName(ast.newSimpleName("findByPrimaryKey")); methodConstructor.thrownExceptions().add(ast.newName("FinderException")); classType.bodyDeclarations().add(methodConstructor); variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE)); variableDeclaration.setType(ast.newPrimitiveType(PrimitiveType.INT)); variableDeclaration.setName(ast.newSimpleName("id")); methodConstructor.parameters().add(variableDeclaration); constructorBlock = ast.newBlock(); methodConstructor.setBody(constructorBlock); superMethodInvocation = ast.newSuperMethodInvocation(); superMethodInvocation.setName(ast.newSimpleName("findByPrimaryKeyIDO")); superMethodInvocation.arguments().add(ast.newSimpleName("id")); ce = ast.newCastExpression(); ce.setType(ast.newSimpleType(ast.newSimpleName(name))); ce.setExpression(superMethodInvocation); returnStatement = ast.newReturnStatement(); returnStatement.setExpression(ce); constructorBlock.statements().add(returnStatement); // findByPrimarKeyLegacy(int) method methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name))); methodConstructor.setName(ast.newSimpleName("findByPrimaryKeyLegacy")); methodConstructor.thrownExceptions().add(ast.newName("SQLException")); addHomeImplImport("java.sql.SQLException"); classType.bodyDeclarations().add(methodConstructor); variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE)); variableDeclaration.setType(ast.newPrimitiveType(PrimitiveType.INT)); variableDeclaration.setName(ast.newSimpleName("id")); methodConstructor.parameters().add(variableDeclaration); constructorBlock = ast.newBlock(); methodConstructor.setBody(constructorBlock); tryStatement = ast.newTryStatement(); constructorBlock.statements().add(tryStatement); tryBlock = ast.newBlock(); tryStatement.setBody(tryBlock); mi = ast.newMethodInvocation(); mi.setName(ast.newSimpleName("findByPrimaryKey")); mi.arguments().add(ast.newSimpleName("id")); returnStatement = ast.newReturnStatement(); returnStatement.setExpression(mi); tryBlock.statements().add(returnStatement); catchClause = ast.newCatchClause(); tryStatement.catchClauses().add(catchClause); variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE)); variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName(("FinderException")))); variableDeclaration.setName(ast.newSimpleName("fe")); catchClause.setException(variableDeclaration); catchBlock = ast.newBlock(); catchClause.setBody(catchBlock); cc = ast.newClassInstanceCreation(); cc.setType(ast.newSimpleType(ast.newSimpleName("SQLException"))); mi = ast.newMethodInvocation(); mi.setExpression(ast.newSimpleName("fe")); mi.setName(ast.newSimpleName("getMessage")); cc.arguments().add(mi); throwStatement = ast.newThrowStatement(); throwStatement.setExpression(cc); catchBlock.statements().add(throwStatement); } MethodFilter[] validFilter = { new MethodFilter(WizardConstants.EJB_CREATE_START, MethodFilter.TYPE_PREFIX), new MethodFilter(WizardConstants.EJB_HOME_START, MethodFilter.TYPE_PREFIX), new MethodFilter(WizardConstants.EJB_FIND_START, MethodFilter.TYPE_PREFIX) }; List methods = filterMethods(getType().getMethods(), validFilter, null); for (Iterator iter = methods.iterator(); iter.hasNext();) { IMethod method = (IMethod) iter.next(); String fullMethodName = method.getElementName(); String methodName = cutAwayEJBSuffix(fullMethodName); String[] parameterNames = method.getParameterNames(); String returnType = method.getReturnType(); if (fullMethodName.startsWith(WizardConstants.EJB_FIND_START) || fullMethodName.startsWith(WizardConstants.EJB_CREATE_START)) { if (!(Signature.getSimpleName(Signature.toString(method.getReturnType())) .indexOf(Signature.getSimpleName("java.util.Collection")) != -1) && !(Signature.getSimpleName(Signature.toString(method.getReturnType())) .indexOf(Signature.getSimpleName("java.util.Set")) != -1)) { returnType = name; } } if (!returnType.equals(name)) { returnType = getReturnType(returnType); } methodConstructor = getMethodDeclaration(ast, method, methodName, returnType, getHomeImplImports(), false); classType.bodyDeclarations().add(methodConstructor); constructorBlock = ast.newBlock(); methodConstructor.setBody(constructorBlock); constructorBlock.statements().add(getIDOCheckOutStatement(ast, getHomeImplImports())); if (fullMethodName.startsWith(WizardConstants.EJB_FIND_START)) { if (Signature.getSimpleName(Signature.toString(method.getReturnType())) .indexOf(Signature.getSimpleName("java.util.Collection")) != -1) { constructorBlock.statements().add( getDataCollectingStatement(ast, returnType, "ids", fullMethodName, parameterNames)); constructorBlock.statements().add(getIDOCheckInStatement(ast)); constructorBlock.statements() .add(getObjectReturnStatement(ast, "getEntityCollectionForPrimaryKeys", "ids")); } else if (Signature.getSimpleName(Signature.toString(method.getReturnType())) .indexOf(Signature.getSimpleName("java.util.Set")) != -1) { constructorBlock.statements().add( getDataCollectingStatement(ast, returnType, "ids", fullMethodName, parameterNames)); constructorBlock.statements().add(getIDOCheckInStatement(ast)); constructorBlock.statements() .add(getObjectReturnStatement(ast, "getEntitySetForPrimaryKeys", "ids")); } else { constructorBlock.statements() .add(getDataCollectingStatement(ast, "Object", "pk", fullMethodName, parameterNames)); constructorBlock.statements().add(getIDOCheckInStatement(ast)); constructorBlock.statements().add(getObjectReturnStatement(ast, "findByPrimaryKey", "pk")); } } else if (fullMethodName.startsWith(WizardConstants.EJB_HOME_START)) { constructorBlock.statements().add( getDataCollectingStatement(ast, returnType, "theReturn", fullMethodName, parameterNames)); constructorBlock.statements().add(getIDOCheckInStatement(ast)); constructorBlock.statements().add(getPrimitiveReturnStatement(ast, "theReturn")); } else if (fullMethodName.startsWith(WizardConstants.EJB_CREATE_START)) { constructorBlock.statements() .add(getDataCollectingStatement(ast, "Object", "pk", fullMethodName, parameterNames)); ce = ast.newCastExpression(); ce.setType(ast.newSimpleType(ast.newSimpleName(getType().getTypeQualifiedName()))); ce.setExpression(ast.newSimpleName("entity")); ParenthesizedExpression pe = ast.newParenthesizedExpression(); pe.setExpression(ce); MethodInvocation mi = ast.newMethodInvocation(); mi.setExpression(pe); mi.setName(ast.newSimpleName("ejbPostCreate")); constructorBlock.statements().add(ast.newExpressionStatement(mi)); constructorBlock.statements().add(getIDOCheckInStatement(ast)); TryStatement tryStatement = ast.newTryStatement(); constructorBlock.statements().add(tryStatement); Block tryBlock = ast.newBlock(); tryStatement.setBody(tryBlock); mi = ast.newMethodInvocation(); mi.setName(ast.newSimpleName("findByPrimaryKey")); mi.arguments().add(ast.newSimpleName("pk")); returnStatement = ast.newReturnStatement(); returnStatement.setExpression(mi); tryBlock.statements().add(returnStatement); CatchClause catchClause = ast.newCatchClause(); tryStatement.catchClauses().add(catchClause); variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE)); variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName(("FinderException")))); variableDeclaration.setName(ast.newSimpleName("fe")); catchClause.setException(variableDeclaration); Block catchBlock = ast.newBlock(); catchClause.setBody(catchBlock); ClassInstanceCreation cc = ast.newClassInstanceCreation(); cc.setType(ast.newSimpleType(ast.newSimpleName("IDOCreateException"))); addHomeImplImport("com.idega.data.IDOCreateException"); cc.arguments().add(ast.newSimpleName(("fe"))); ThrowStatement throwStatement = ast.newThrowStatement(); throwStatement.setExpression(cc); catchBlock.statements().add(throwStatement); catchClause = ast.newCatchClause(); tryStatement.catchClauses().add(catchClause); variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE)); variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName(("Exception")))); variableDeclaration.setName(ast.newSimpleName("e")); catchClause.setException(variableDeclaration); catchBlock = ast.newBlock(); catchClause.setBody(catchBlock); cc = ast.newClassInstanceCreation(); cc.setType(ast.newSimpleType(ast.newSimpleName("IDOCreateException"))); cc.arguments().add(ast.newSimpleName(("e"))); throwStatement = ast.newThrowStatement(); throwStatement.setExpression(cc); catchBlock.statements().add(throwStatement); } } writeImports(ast, unit, getHomeImplImports()); commitChanges(iUnit, unit, document); }
From source file:de.crowdcode.kissmda.core.jdt.JdtHelper.java
License:Apache License
/** * Create return type for JDT MethodDeclaration as Java Collection. * /*from w w w . j a va 2 s . c o m*/ * @param ast * JDT AST tree * @param td * JDT abstract type declaration * @param md * JDT method declaration * @param umlTypeName * UML type name * @param umlQualifiedTypeName * UML qualified type name * @param sourceDirectoryPackageName * source directory of UML * @param collectionTypeConstant * type of the collection: Collection, Set or List */ @SuppressWarnings("unchecked") public void createReturnTypeAsCollection(AST ast, AbstractTypeDeclaration td, MethodDeclaration md, String umlTypeName, String umlQualifiedTypeName, String sourceDirectoryPackageName, String collectionTypeConstant) { Type type = getChosenType(ast, umlTypeName, umlQualifiedTypeName, sourceDirectoryPackageName); // Create Collection SimpleType collectionType = ast.newSimpleType(ast.newName(collectionTypeConstant)); ParameterizedType pt = ast.newParameterizedType(collectionType); pt.typeArguments().add(type); md.setReturnType2(pt); td.bodyDeclarations().add(md); }
From source file:de.crowdcode.kissmda.core.jdt.JdtHelper.java
License:Apache License
/** * Get JDT ParameterizedType for the given type name. * //from w w w . j av a2 s. c o m * @param ast * JDT AST tree * @param typeName * input type name * @return JDT ParameterizedType */ @SuppressWarnings("unchecked") public ParameterizedType getAstParameterizedType(AST ast, String typeName) { // Get the component type and parameters <Type, Type, ...> String componentTypeName = StringUtils.substringBefore(typeName, "<"); Type componentType = getAstSimpleType(ast, componentTypeName); ParameterizedType parameterizedType = ast.newParameterizedType(componentType); String paramTypeNames = StringUtils.substringAfter(typeName, "<"); paramTypeNames = StringUtils.removeEnd(paramTypeNames, ">"); // Result: String, Integer, List<Boolean>, de.test.Company String[] parametersAsString = StringUtils.split(paramTypeNames, ","); for (int index = 0; index < parametersAsString.length; index++) { String paramTypeName = parametersAsString[index]; paramTypeName = StringUtils.remove(paramTypeName, ","); paramTypeName = StringUtils.trim(paramTypeName); Type paramType = getChosenType(ast, paramTypeName, paramTypeName, ""); // Add the type arguments parameterizedType.typeArguments().add(paramType); } return parameterizedType; }
From source file:de.crowdcode.kissmda.core.jdt.JdtHelper.java
License:Apache License
/** * Create parameter types as Collection for MethodDeclaration. * /* ww w.ja v a 2 s. c o m*/ * @param ast * JDT AST tree * @param td * JDT AbstractTypeDeclaration * @param md * JDT MethodDeclaration * @param umlTypeName * UML type name * @param umlQualifiedTypeName * UML fully qualified type name * @param umlPropertyName * UML property name * @param sourceDirectoryPackageName * UML source directory start * @param collectionTypeConstant * type of the collection: Collection, Set or List */ @SuppressWarnings("unchecked") public void createParameterTypesAsCollection(AST ast, AbstractTypeDeclaration td, MethodDeclaration md, String umlTypeName, String umlQualifiedTypeName, String umlPropertyName, String sourceDirectoryPackageName, String collectionTypeConstant) { Type chosenType = getChosenType(ast, umlTypeName, umlQualifiedTypeName, sourceDirectoryPackageName); // Create Collection SimpleType collectionType = ast.newSimpleType(ast.newName(collectionTypeConstant)); ParameterizedType pt = ast.newParameterizedType(collectionType); pt.typeArguments().add(chosenType); SingleVariableDeclaration variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.setType(pt); variableDeclaration.setName(ast.newSimpleName(umlPropertyName)); md.parameters().add(variableDeclaration); }
From source file:de.dentrassi.varlink.generator.JdtGenerator.java
License:Open Source License
@SuppressWarnings("unchecked") private void createInternalMethod(final TypeDeclaration parentTypeDeclaration, final MethodInformation m, final MethodDeclaration md) { final AST ast = md.getAST(); final Block body = ast.newBlock(); md.setBody(body);//w w w .j ava 2 s . c o m /* * return this.connection.call(CallRequest.of("io.systemd.network.List")) * .thenApply(cr -> { check(cr); * * final Iterator<JsonElement> i = cr.getParameters().values().iterator(); * * return asList( this.varlink .fromJson( Netdev[].class, i.next())); }); } */ // add arguments if (!m.getParameters().isEmpty()) { // code: Map<String,Object> parameters = new HashMap<> (); final VariableDeclarationFragment parameters = ast.newVariableDeclarationFragment(); parameters.setName(ast.newSimpleName("parameters")); final VariableDeclarationStatement decl = ast.newVariableDeclarationStatement(parameters); body.statements().add(decl); final ParameterizedType map = ast.newParameterizedType(ast.newSimpleType(ast.newName("java.util.Map"))); map.typeArguments().add(ast.newSimpleType(ast.newName("java.lang.String"))); map.typeArguments().add(ast.newSimpleType(ast.newName("java.lang.Object"))); decl.setType(map); final ClassInstanceCreation init = ast.newClassInstanceCreation(); init.setType(ast.newParameterizedType(ast.newSimpleType(ast.newName("java.util.HashMap")))); init.arguments().add(ast.newNumberLiteral(Integer.toString(m.getParameters().size()))); parameters.setInitializer(init); for (final String argName : m.getParameters().keySet()) { final MethodInvocation mi = ast.newMethodInvocation(); mi.setExpression(ast.newSimpleName("parameters")); mi.setName(ast.newSimpleName("put")); mi.arguments().add(JdtHelper.newStringLiteral(ast, argName)); mi.arguments().add(ast.newSimpleName(argName)); } } // return final ReturnStatement ret = ast.newReturnStatement(); body.statements().add(ret); final MethodInvocation mi = ast.newMethodInvocation(); mi.setName(ast.newSimpleName("call")); final FieldAccess fa = ast.newFieldAccess(); fa.setExpression(ast.newThisExpression()); fa.setName(ast.newSimpleName("connection")); mi.setExpression(fa); final MethodInvocation cr = ast.newMethodInvocation(); cr.setExpression(ast.newName("de.dentrassi.varlink.spi.CallRequest")); cr.setName(ast.newSimpleName("of")); cr.arguments().add(newStringLiteral(ast, m.getInterface().getName() + "." + toUpperFirst(m.getName()))); if (!m.getParameters().isEmpty()) { cr.arguments().add(ast.newSimpleName("parameters")); } mi.arguments().add(cr); final MethodInvocation thenApply = ast.newMethodInvocation(); thenApply.setName(ast.newSimpleName("thenApply")); thenApply.setExpression(mi); // add transformation final LambdaExpression le = ast.newLambdaExpression(); le.setParentheses(false); thenApply.arguments().add(le); final VariableDeclarationFragment p = ast.newVariableDeclarationFragment(); p.setName(ast.newSimpleName("result")); le.parameters().add(p); final Block transform = ast.newBlock(); le.setBody(transform); { // check result final MethodInvocation check = ast.newMethodInvocation(); check.setName(ast.newSimpleName("checkError")); transform.statements().add(ast.newExpressionStatement(check)); check.arguments().add(ast.newSimpleName("result")); } if (m.getReturnTypes().isEmpty()) { final ReturnStatement transformRet = ast.newReturnStatement(); transformRet.setExpression(ast.newNullLiteral()); transform.statements().add(transformRet); } else { final int returns = m.getReturnTypes().size(); if (returns > 0) { // return this.varlink.fromJson(DriveCondition.class, result.getParameters()); // return this.varlink.fromJson(DriveCondition.class, // result.getFirstParameter()); final FieldAccess varlink = ast.newFieldAccess(); varlink.setExpression(ast.newThisExpression()); varlink.setName(ast.newSimpleName("varlink")); final MethodInvocation fromJson = ast.newMethodInvocation(); fromJson.setExpression(varlink); fromJson.setName(ast.newSimpleName("fromJson")); // FIXME: add to parent { final ParameterizedType ttt = ast .newParameterizedType(ast.newSimpleType(ast.newName(TYPE_TOKEN_TYPE_NAME))); ttt.typeArguments().add(m.createMainReturnType(ast)); final ClassInstanceCreation tt = ast.newClassInstanceCreation(); tt.setType(JdtHelper.copyNode(ast, ttt)); final AnonymousClassDeclaration decl = ast.newAnonymousClassDeclaration(); tt.setAnonymousClassDeclaration(decl); final MethodInvocation getType = ast.newMethodInvocation(); getType.setExpression(tt); getType.setName(ast.newSimpleName("getType")); final VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment(); vdf.setName(ast.newSimpleName(m.getName() + "_returnTypeToken")); vdf.setInitializer(getType); final FieldDeclaration fd = ast.newFieldDeclaration(vdf); fd.setType(ast.newSimpleType(ast.newName("java.lang.reflect.Type"))); make(fd, PRIVATE_KEYWORD, FINAL_KEYWORD, STATIC_KEYWORD); parentTypeDeclaration.bodyDeclarations().add(fd); } fromJson.arguments().add(ast.newSimpleName(m.getName() + "_returnTypeToken")); // json fragment final MethodInvocation fragment = ast.newMethodInvocation(); if (returns == 1) { fragment.setName(ast.newSimpleName("getFirstParameter")); } else { fragment.setName(ast.newSimpleName("getParameters")); } fragment.setExpression(ast.newSimpleName("result")); fromJson.arguments().add(fragment); // return final ReturnStatement transformRet = ast.newReturnStatement(); transformRet.setExpression(fromJson); transform.statements().add(transformRet); } // FIXME: handle return type // FIXME: handle n } // set return ret.setExpression(thenApply); }