List of usage examples for org.eclipse.jdt.core IType createMethod
IMethod createMethod(String contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException;
From source file:at.bestsolution.efxclipse.tooling.fxgraph.ui.quickfix.FXGraphQuickfixProvider.java
License:Open Source License
@Fix(FXGraphJavaValidator.UNKNOWN_CONTROLLER_METHOD) public void fixAddControllerMethod(final Issue issue, IssueResolutionAcceptor acceptor) { final String methodName = issue.getData()[0]; final String controllerClass = issue.getData()[1]; final String argType = issue.getData()[2]; final String simpleArgType = Signature.getSimpleName(argType); acceptor.accept(issue, "Add controller method " + methodName + "(" + simpleArgType + ")", "Add a controller method '" + methodName + "(" + simpleArgType + ")' to controller '" + controllerClass + "'", null, new IModification() { @Override//from w ww . j ava2 s .c om public void apply(IModificationContext context) throws Exception { IJavaProject p = context.getXtextDocument() .readOnly(new IUnitOfWork<IJavaProject, XtextResource>() { @Override public IJavaProject exec(XtextResource state) throws Exception { return provider.getJavaProject(state.getResourceSet()); } }); IType type = p.findType(controllerClass); String[][] resolvedType = type.resolveType("FXML"); if (resolvedType == null) { type.getCompilationUnit().createImport("javafx.fxml.FXML", null, new NullProgressMonitor()); } resolvedType = type.resolveType(simpleArgType); if (resolvedType == null) { type.getCompilationUnit().createImport(argType, null, new NullProgressMonitor()); } type.createMethod("@FXML public void " + methodName + "(" + simpleArgType + " event) {}", null, true, new NullProgressMonitor()); } }); acceptor.accept(issue, "Add controller method " + methodName + "()", "Add a controller method '" + methodName + "()' to controller '" + controllerClass + "'", null, new IModification() { @Override public void apply(IModificationContext context) throws Exception { IJavaProject p = context.getXtextDocument() .readOnly(new IUnitOfWork<IJavaProject, XtextResource>() { @Override public IJavaProject exec(XtextResource state) throws Exception { return provider.getJavaProject(state.getResourceSet()); } }); IType type = p.findType(controllerClass); String[][] resolvedType = type.resolveType("FXML"); if (resolvedType == null) { type.getCompilationUnit().createImport("javafx.fxml.FXML", null, new NullProgressMonitor()); } type.createMethod("@FXML public void " + methodName + "() {}", null, true, new NullProgressMonitor()); } }); }
From source file:cn.ieclipse.adt.ext.jdt.SourceGenerator.java
License:Apache License
public static void merge(IType type, String authority, String dbName, List<String> tableCreators, List<String> tables) throws Exception { type.createField("public static final String AUTH=\"" + authority + "\";", null, true, null); type.createField("public static final Uri URI=Uri.parse(\"content://\" + AUTH);", null, true, null); type.createField("private SQLiteOpenHelper mOpenHelper;", null, true, null); type.createField("private static Session session;", null, true, null); type.createMethod("public static Session getSession(){" + "return session;" + "}", null, true, null); IMethod onCreate = type.getMethod("onCreate", null); // String doc = onCreate.getAttachedJavadoc(null); StringBuilder sb = new StringBuilder(onCreate.getSource()); int start = sb.indexOf("{") + 1; int end = sb.indexOf("}"); sb.delete(start, end);// ww w. java2 s.com StringBuilder sb2 = new StringBuilder(); sb2.append("mOpenHelper=new SQLiteOpenHelper(this.getContext(),\""); sb2.append(dbName); sb2.append("\",null,1){"); sb2.append("public void onCreate(SQLiteDatabase db){"); // sb2.append(" String sql=\"\";"); // for (String creator : tableCreators) { // String[] lines = creator.split(SourceAnalysis.LF); // for (int i = 0; i < lines.length; i++) { // if (i == 0) { // sb2.append("sql=\""); // } else { // sb2.append("sql+=\""); // } // sb2.append(lines[i]); // sb2.append("\";"); // } // sb2.append("db.execSQL(sql);"); // } for (String table : tables) { sb2.append(table); } sb2.append("}"); sb2.append("public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){"); sb2.append(""); sb2.append("}};"); sb2.append("session = new Session(mOpenHelper, getContext().getContentResolver());"); sb2.append("return true;"); sb.insert(start, sb2); onCreate.delete(true, null); type.createMethod(sb.toString(), null, true, null); }
From source file:cn.ieclipse.adt.ext.wizards.NewActivityWizardPage.java
License:Apache License
private void generateOnCreate(IType type, ImportsManager imports) throws CoreException, JavaModelException { StringBuilder buf = new StringBuilder(); final String lineDelim = "\n"; // OK, since content is formatted afterwards //$NON-NLS-1$ buf.append("/* (non-Javadoc)").append(lineDelim); buf.append("* @see android.app.Activity#onCreate(android.os.Bundle)").append(lineDelim); buf.append("*/").append(lineDelim); buf.append("@Override"); buf.append(lineDelim);/* ww w. j av a 2 s. c o m*/ buf.append("public void onCreate("); //$NON-NLS-1$ buf.append(imports.addImport("android.os.Bundle")); //$NON-NLS-1$ buf.append(" savedInstanceState) {"); //$NON-NLS-1$ buf.append(lineDelim); buf.append("super.onCreate(savedInstanceState);"); buf.append(lineDelim); final String content = CodeGeneration.getMethodBodyContent(type.getCompilationUnit(), type.getTypeQualifiedName('.'), "onCreate", false, "", lineDelim); //$NON-NLS-1$ //$NON-NLS-2$ if (content != null && content.length() != 0) buf.append(content); buf.append(lineDelim); buf.append("}"); //$NON-NLS-1$ type.createMethod(buf.toString(), null, false, null); }
From source file:cn.ieclipse.adt.ext.wizards.NewActivityWizardPage.java
License:Apache License
private void generateOnStartCommand(IType type, ImportsManager imports) throws CoreException, JavaModelException { StringBuilder buf = new StringBuilder(); final String lineDelim = "\n"; // OK, since content is formatted afterwards //$NON-NLS-1$ buf.append("/* (non-Javadoc)").append(lineDelim); buf.append("* @see android.app.Service#onStartCommand(android.content.Intent, int, int)").append(lineDelim); buf.append("*/").append(lineDelim); buf.append("@Override"); buf.append(lineDelim);/*from w w w .j ava2s. c o m*/ buf.append("public int onStartCommand("); //$NON-NLS-1$ buf.append(imports.addImport("android.content.Intent")); //$NON-NLS-1$ buf.append(" intent, int flags, int startId) {"); //$NON-NLS-1$ buf.append(lineDelim); final String content = CodeGeneration.getMethodBodyContent(type.getCompilationUnit(), type.getTypeQualifiedName('.'), "super.onStartCommand", false, "", lineDelim); //$NON-NLS-1$ //$NON-NLS-2$ if (content != null && content.length() != 0) buf.append(content); buf.append(lineDelim); buf.append("return super.onStartCommand(intent, flags, startId);"); buf.append(lineDelim); buf.append("}"); //$NON-NLS-1$ type.createMethod(buf.toString(), null, false, null); }
From source file:cn.ieclipse.adt.ext.wizards.NewActivityWizardPage.java
License:Apache License
private void generateStub(String method, IType type, ImportsManager imports) throws CoreException, JavaModelException { List<String> supers = ProjectHelper.getSuperTypeName(getJavaProject(), getSuperClass(), false); if (supers.contains(AdtConstants.ACTIVITY_QNAME) && "onCreate".equals(method)) { generateOnCreate(type, imports); return;//from www . j av a 2s. com } else if (supers.contains(AdtConstants.SERVICE_QNAME) && "onStartCommand".equals(method)) { generateOnStartCommand(type, imports); return; } StringBuilder buf = new StringBuilder(); final String lineDelim = "\n"; // OK, since content is formatted afterwards //$NON-NLS-1$ buf.append("/* (non-Javadoc)").append(lineDelim); buf.append("* @see " + getSuperClass() + "#" + method + "()").append(lineDelim); buf.append("*/").append(lineDelim); buf.append("@Override"); buf.append(lineDelim); buf.append("public void " + method + "(){"); //$NON-NLS-1$ // buf.append(lineDelim); buf.append("super." + method + "();"); buf.append(lineDelim); final String content = CodeGeneration.getMethodBodyContent(type.getCompilationUnit(), type.getTypeQualifiedName('.'), method, false, "", lineDelim); //$NON-NLS-1$ //$NON-NLS-2$ if (content != null && content.length() != 0) buf.append(content); buf.append(lineDelim); buf.append("}"); //$NON-NLS-1$ type.createMethod(buf.toString(), null, false, null); }
From source file:cn.ieclipse.aorm.eclipse.jdt.SourceGenerator.java
License:Apache License
public static void merge(IType type, String authority, String dbName, List<String> tableCreators) throws Exception { type.createField("public static final String AUTH=\"" + authority + "\";", null, true, null); type.createField("public static final Uri URI=Uri.parse(\"content://\" + AUTH);", null, true, null); type.createField("private SQLiteOpenHelper mOpenHelper;", null, true, null); type.createField("private static Session session;", null, true, null); type.createMethod("public static Session getSession(){" + "return session;" + "}", null, true, null); IMethod onCreate = type.getMethod("onCreate", null); // String doc = onCreate.getAttachedJavadoc(null); StringBuilder sb = new StringBuilder(onCreate.getSource()); int start = sb.indexOf("{") + 1; int end = sb.indexOf("}"); sb.delete(start, end);//from www .j a v a 2s . c o m StringBuilder sb2 = new StringBuilder(); sb2.append("mOpenHelper=new SQLiteOpenHelper(this.getContext(),\""); sb2.append(dbName); sb2.append("\",null,1){"); sb2.append("public void onCreate(SQLiteDatabase db){"); sb2.append(" String sql=\"\";"); for (String creator : tableCreators) { String[] lines = creator.split(SourceAnalysis.LF); for (int i = 0; i < lines.length; i++) { if (i == 0) { sb2.append("sql=\""); } else { sb2.append("sql+=\""); } sb2.append(lines[i]); sb2.append("\";"); } sb2.append("db.execSQL(sql);"); } sb2.append("}"); sb2.append("public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){"); sb2.append(""); sb2.append("}};"); sb2.append("session = new Session(mOpenHelper, getContext().getContentResolver());"); sb2.append("return true;"); sb.insert(start, sb2); onCreate.delete(true, null); type.createMethod(sb.toString(), null, true, null); }
From source file:com.aliyun.odps.eclipse.create.wizard.NewDriverWizardPage.java
License:Apache License
@Override protected void createTypeMembers(final IType newType, ImportsManager imports, final IProgressMonitor monitor) throws CoreException { super.createTypeMembers(newType, imports, monitor); imports.addImport("com.aliyun.odps.OdpsException"); imports.addImport("com.aliyun.odps.data.TableInfo"); imports.addImport("com.aliyun.odps.mapred.JobClient"); imports.addImport("com.aliyun.odps.mapred.RunningJob"); imports.addImport("com.aliyun.odps.mapred.conf.JobConf"); imports.addImport("com.aliyun.odps.mapred.utils.InputUtils"); imports.addImport("com.aliyun.odps.mapred.utils.OutputUtils"); imports.addImport("com.aliyun.odps.mapred.utils.SchemaUtils"); getContainer().getShell().getDisplay().syncExec(new Runnable() { public void run() { String method = "public static void main(String[] args) throws OdpsException {\n"; method += "JobConf job = new JobConf();\n\n"; method += "// TODO: specify map output types\n"; method += "job.setMapOutputKeySchema(SchemaUtils.fromString(\"word:string\"));\n"; method += "job.setMapOutputValueSchema(SchemaUtils.fromString(\"count:bigint\"));\n\n"; method += "// TODO: specify input and output tables\n"; method += "InputUtils.addTable(TableInfo.builder().tableName(\"wc_in1\").build(), job);\n"; method += "OutputUtils.addTable(TableInfo.builder().tableName(\"wc_out\").build(), job);\n\n"; if (mapperText.getText().length() > 0) { method += "job.setMapperClass(" + mapperText.getText() + ".class);\n"; } else { method += "// TODO: specify a mapper\njob.setMapperClass(\"?\");\n"; }/*from w w w. jav a 2s . co m*/ if (reducerText.getText().length() > 0) { method += "job.setReducerClass(" + reducerText.getText() + ".class);\n"; } else { method += "// TODO: specify a reducer\njob.setReducerClass(\"?\");\n"; } method += "\nRunningJob rj = JobClient.runJob(job);"; method += "\nrj.waitForCompletion();"; method += "}\n"; try { newType.createMethod(method, null, false, monitor); } catch (JavaModelException e) { e.printStackTrace(); } } }); }
From source file:com.aliyun.odps.eclipse.create.wizard.NewUDFWizardPage.java
License:Apache License
@Override protected void createTypeMembers(IType newType, ImportsManager imports, IProgressMonitor monitor) throws CoreException { imports.addImport("com.aliyun.odps.udf.UDF"); super.createTypeMembers(newType, imports, monitor); newType.createMethod( "// TODO define parameters and return type, e.g., public Long evaluate(Long a, Long b)\npublic ... evaluate(...){\n}", null, false, monitor);// w ww . ja v a 2 s . c o m }
From source file:com.aliyun.odps.eclipse.create.wizard.NewUDTFWizardPage.java
License:Apache License
@Override protected void createTypeMembers(IType newType, ImportsManager imports, IProgressMonitor monitor) throws CoreException { imports.addImport("com.aliyun.odps.udf.annotation.Resolve"); imports.addImport("com.aliyun.odps.udf.ExecutionContext"); imports.addImport("com.aliyun.odps.udf.UDFException"); imports.addImport("com.aliyun.odps.udf.UDTF"); super.createTypeMembers(newType, imports, monitor); // newType.create newType.createMethod("public void setup(ExecutionContext ctx) throws UDFException {\n}", null, false, monitor);/*from w w w .ja va 2s. com*/ newType.createMethod("public void process(Object[] args) throws UDFException {\n}", null, false, monitor); newType.createMethod("public void close() throws UDFException {\n}", null, false, monitor); }
From source file:com.android.ide.eclipse.adt.internal.editors.manifest.descriptors.PostActivityCreationAction.java
License:Open Source License
/** * Processes a newly created Activity./*from w ww.ja v a 2 s.c o m*/ * */ @Override public void processNewType(IType newType) { try { String methodContent = " /** Called when the activity is first created. */\n" + " @Override\n" + " public void onCreate(Bundle savedInstanceState) {\n" + " super.onCreate(savedInstanceState);\n" + "\n" + " // TODO Auto-generated method stub\n" + " }"; newType.createMethod(methodContent, null /* sibling*/, false /* force */, new NullProgressMonitor()); // we need to add the import for Bundle, so we need the compilation unit. // Since the type could be enclosed in other types, we loop till we find it. ICompilationUnit compilationUnit = null; IJavaElement element = newType; do { IJavaElement parentElement = element.getParent(); if (parentElement != null) { if (parentElement.getElementType() == IJavaElement.COMPILATION_UNIT) { compilationUnit = (ICompilationUnit) parentElement; } element = parentElement; } else { break; } } while (compilationUnit == null); if (compilationUnit != null) { compilationUnit.createImport(SdkConstants.CLASS_BUNDLE, null /* sibling */, new NullProgressMonitor()); } } catch (JavaModelException e) { } }