List of usage examples for org.eclipse.jdt.core.dom.rewrite ImportRewrite getCompilationUnit
public ICompilationUnit getCompilationUnit()
From source file:org.eclipse.gmf.internal.common.codegen.OrganizeImportsPostprocessor.java
License:Open Source License
private static ImportRewrite configureImportRewrite(ImportRewrite rewrite) { IJavaProject project = rewrite.getCompilationUnit().getJavaProject(); String order = PreferenceConstants.getPreference(PreferenceConstants.ORGIMPORTS_IMPORTORDER, project); rewrite.setImportOrder(order.split(";", 0)); //$NON-NLS-1$ String thres = PreferenceConstants.getPreference(PreferenceConstants.ORGIMPORTS_ONDEMANDTHRESHOLD, project); try {/* w w w .jav a2 s . c om*/ int num = Integer.parseInt(thres); if (num == 0) { num = 1; } rewrite.setOnDemandImportThreshold(num); } catch (NumberFormatException e) { // ignore } String thresStatic = PreferenceConstants .getPreference(PreferenceConstants.ORGIMPORTS_STATIC_ONDEMANDTHRESHOLD, project); try { int num = Integer.parseInt(thresStatic); if (num == 0) { num = 1; } rewrite.setStaticOnDemandImportThreshold(num); } catch (NumberFormatException e) { // ignore } return rewrite; }
From source file:org.jboss.tools.arquillian.ui.internal.utils.ArquillianUIUtil.java
License:Open Source License
/** * Creates a deployment method//from w w w . j av a2 s . c o m * * @param icu * @param type * @param imports * @param isAddComments * @param delimiter * @param deploymentDescriptor * @param sibling * @param force * @throws CoreException */ public static void createDeploymentMethod(ICompilationUnit icu, IType type, ImportsManager imports, boolean isAddComments, String delimiter, IDeploymentDescriptor deploymentDescriptor, IJavaElement sibling, boolean force) throws CoreException { String content = null; ImportRewrite importsRewrite = null; if (icu != null) { importsRewrite = StubUtility.createImportRewrite(icu, true); } String annotation = '@' + addImport(imports, importsRewrite, ArquillianUtility.ORG_JBOSS_ARQUILLIAN_CONTAINER_TEST_API_DEPLOYMENT); String methodName = CREATE_DEPLOYMENT; addImport(imports, importsRewrite, ORG_JBOSS_SHRINKWRAP_API_SHRINK_WRAP); addImport(imports, importsRewrite, ORG_JBOSS_SHRINKWRAP_API_ARCHIVE); GenStubSettings settings = JUnitStubUtility.getCodeGenerationSettings(type.getJavaProject()); settings.createComments = isAddComments; StringBuffer buffer = new StringBuffer(); if (settings.createComments) { String retTypeSig = Signature.createTypeSignature(ORG_JBOSS_SHRINKWRAP_API_ARCHIVE, true); String comment = CodeGeneration.getMethodComment(type.getCompilationUnit(), type.getElementName(), methodName, new String[0], new String[0], retTypeSig, null, delimiter); if (comment != null) { buffer.append(comment); } } String archiveType = ArquillianConstants.JAR; String archiveName = ""; //$NON-NLS-1$ String deploymentName = null; String deploymentOrder = null; boolean addBeansXml = true; IType[] types = null; List<String> resources = new ArrayList<String>(); List<String> webInfResources = new ArrayList<String>(); if (deploymentDescriptor != null) { methodName = deploymentDescriptor.getMethodName(); archiveType = deploymentDescriptor.getArchiveType(); archiveName = deploymentDescriptor.getArchiveName(); addBeansXml = deploymentDescriptor.addBeansXml(); deploymentName = deploymentDescriptor.getDeploymentName(); deploymentOrder = deploymentDescriptor.getDeploymentOrder(); types = deploymentDescriptor.getTypes(); ProjectResource[] allResources = deploymentDescriptor.getResources(); for (ProjectResource resource : allResources) { if (ArquillianConstants.WAR.equals(archiveType) && resource.isDeployAsWebInfResource()) { webInfResources.add(resource.getPath().toString()); } else { resources.add(resource.getPath().toString()); } } } buffer.append(annotation); if ((deploymentName != null && !deploymentName.isEmpty()) || (deploymentOrder != null && !deploymentOrder.isEmpty())) { buffer.append("("); //$NON-NLS-1$ if ((deploymentName != null && !deploymentName.isEmpty())) { buffer.append("name = \""); //$NON-NLS-1$ buffer.append(deploymentName); buffer.append("\""); //$NON-NLS-1$ if (deploymentOrder != null && !deploymentOrder.isEmpty()) { buffer.append(" , "); //$NON-NLS-1$ } } if (deploymentOrder != null && !deploymentOrder.isEmpty()) { buffer.append("order = "); //$NON-NLS-1$ buffer.append(deploymentOrder); } buffer.append(")"); //$NON-NLS-1$ } buffer.append(delimiter); buffer.append("public static Archive<?> "); //$NON-NLS-1$ buffer.append(methodName); buffer.append("()"); //$NON-NLS-1$ buffer.append(" {").append(delimiter); //$NON-NLS-1$ if (ArquillianConstants.JAR.equals(archiveType)) { addImport(imports, importsRewrite, ArquillianUtility.ORG_JBOSS_SHRINKWRAP_API_SPEC_JAVA_ARCHIVE); buffer.append("JavaArchive archive = ShrinkWrap.create(JavaArchive.class"); //$NON-NLS-1$ } if (ArquillianConstants.WAR.equals(archiveType)) { addImport(imports, importsRewrite, ArquillianUtility.ORG_JBOSS_SHRINKWRAP_API_SPEC_WEB_ARCHIVE); buffer.append("WebArchive archive = ShrinkWrap.create(WebArchive.class"); //$NON-NLS-1$ } if (ArquillianConstants.EAR.equals(archiveType)) { addImport(imports, importsRewrite, "org.jboss.shrinkwrap.api.spec.EnterpriseArchive"); //$NON-NLS-1$ buffer.append("EnterpriseArchive archive = ShrinkWrap.create(EnterpriseArchive.class"); //$NON-NLS-1$ } if (ArquillianConstants.RAR.equals(archiveType)) { addImport(imports, importsRewrite, "org.jboss.shrinkwrap.api.spec.ResourceAdapterArchive"); //$NON-NLS-1$ buffer.append("ResourceAdapterArchive archive = ShrinkWrap.create(ResourceAdapterArchive.class"); //$NON-NLS-1$ } if (archiveName != null && !archiveName.isEmpty()) { if (archiveName.indexOf(PERIOD) == -1) { archiveName = archiveName + PERIOD + archiveType; } buffer.append(", "); //$NON-NLS-1$ buffer.append("\""); //$NON-NLS-1$ buffer.append(archiveName); buffer.append("\""); //$NON-NLS-1$ } buffer.append(")"); //$NON-NLS-1$ if (types != null && types.length > 0) { buffer.append(delimiter); buffer.append(".addClasses( "); //$NON-NLS-1$ boolean first = true; for (IType t : types) { if (!first) { buffer.append(" , "); //$NON-NLS-1$ } else { first = false; } String typeName = t.getFullyQualifiedName().replaceAll("\\$", "."); int lastPeriod = typeName.lastIndexOf(PERIOD); String className = typeName; if (lastPeriod >= 0 && lastPeriod < typeName.length()) { className = typeName.substring(lastPeriod + 1, typeName.length()); addImport(imports, importsRewrite, typeName); } buffer.append(className); buffer.append(".class"); //$NON-NLS-1$ } buffer.append(" )"); //$NON-NLS-1$ } for (String resource : resources) { buffer.append(delimiter); buffer.append(".addAsResource( "); //$NON-NLS-1$ buffer.append("\""); //$NON-NLS-1$ buffer.append(resource); buffer.append("\""); //$NON-NLS-1$ buffer.append(" )"); //$NON-NLS-1$ } for (String resource : webInfResources) { buffer.append(delimiter); buffer.append(".addAsWebInfResource( "); //$NON-NLS-1$ buffer.append("\""); //$NON-NLS-1$ buffer.append(resource); buffer.append("\""); //$NON-NLS-1$ buffer.append(" )"); //$NON-NLS-1$ } if (addBeansXml && !ArquillianConstants.EAR.equals(archiveType)) { addImport(imports, importsRewrite, "org.jboss.shrinkwrap.api.asset.EmptyAsset"); //$NON-NLS-1$ buffer.append(delimiter); if (ArquillianConstants.WAR.equals(archiveType)) { buffer.append(".addAsWebInfResource(EmptyAsset.INSTANCE, \"beans.xml\")"); //$NON-NLS-1$ } else { buffer.append(".addAsManifestResource(EmptyAsset.INSTANCE, \"beans.xml\")"); //$NON-NLS-1$ } } buffer.append(";").append(delimiter); //$NON-NLS-1$ buffer.append("// System.out.println(archive.toString(true));").append( //$NON-NLS-1$ delimiter); buffer.append("return archive;").append(delimiter); //$NON-NLS-1$ buffer.append("}"); //$NON-NLS-1$ buffer.append(delimiter); content = buffer.toString(); if (icu == null) { type.createMethod(content, sibling, force, null); } else { TextEdit edit = importsRewrite.rewriteImports(null); JavaModelUtil.applyEdit(importsRewrite.getCompilationUnit(), edit, false, null); IMethod createdMethod = type.createMethod(content, sibling, force, null); ISourceRange range = createdMethod.getSourceRange(); IBuffer buf = icu.getBuffer(); String originalContent = buf.getText(range.getOffset(), range.getLength()); int indent = StubUtility.getIndentUsed(type) + 1; String formattedContent = CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, originalContent, indent, delimiter, type.getJavaProject()); //formattedContent = Strings // .trimLeadingTabsAndSpaces(formattedContent); while (formattedContent.length() > 0) { char ch = formattedContent.charAt(0); if (ScannerHelper.isWhitespace(ch)) { formattedContent = formattedContent.substring(1); } else { break; } } buf.replace(range.getOffset(), range.getLength(), formattedContent); icu.reconcile(ICompilationUnit.NO_AST, false, null, null); icu.commitWorkingCopy(false, null); } }