List of usage examples for org.eclipse.jdt.core ICompilationUnit createImport
IImportDeclaration createImport(String name, IJavaElement sibling, int flags, IProgressMonitor monitor) throws JavaModelException;
From source file:com.sun.codemodel.ClassGenerator.java
License:Apache License
private void modifyObjectFactory(ElementClass ec, IProgressMonitor pm) throws JavaModelException { JDefinedClass c = ec.getjDefClass(); if (ec.getElementType() != null) { IPackageFragmentRoot pfr = project.getPackageFragmentRoot(generated); if (pfr != null && pfr.exists()) { IPackageFragment pf = pfr.getPackageFragment(c.getPackage().name()); if (pf != null && pf.exists()) { ICompilationUnit of = pf.getCompilationUnit("ObjectFactory.java"); if (of != null && !of.exists()) { of = pf.createCompilationUnit("ObjectFactory.java", "", true, pm); of.createPackageDeclaration(c.getPackage().name(), pm); of.createImport("javax.xml.bind.annotation.XmlRegistry", null, Flags.AccDefault, pm); String lineDelimiter = StubUtility.getLineDelimiterUsed(project); IType t = of.createType(generateObjectFactory(lineDelimiter), null, false, pm); t.createMethod("public ObjectFactory(){}" + lineDelimiter, null, false, pm); }//from w w w . j a va2 s.c o m of = addClass(ec, of, pm); } else throw new JavaModelException( new Exception("Error package " + c.getPackage().name() + " does not exists"), JavaModelStatus.ERROR); } else throw new JavaModelException( new Exception("Error package root " + generated.getName() + " does not exists"), JavaModelStatus.ERROR); } }
From source file:com.sun.codemodel.ClassGenerator.java
License:Apache License
private ICompilationUnit addClass(ElementClass ec, ICompilationUnit of, IProgressMonitor pm) throws JavaModelException { JDefinedClass c = ec.getjDefClass(); IImportDeclaration imp = of.getImport("javax.xml.namespace.QName"); if (imp == null || (!imp.exists())) { of.createImport("javax.xml.namespace.QName", null, Flags.AccDefault, pm); }//from ww w . j a v a2 s . c o m imp = of.getImport("javax.xml.bind.annotation.XmlElementDecl"); if (imp == null || (!imp.exists())) { of.createImport("javax.xml.bind.annotation.XmlElementDecl", null, Flags.AccDefault, pm); } imp = of.getImport("javax.xml.bind.JAXBElement"); if (imp == null || (!imp.exists())) { of.createImport("javax.xml.bind.JAXBElement", null, Flags.AccDefault, pm); } IType t = of.getType("ObjectFactory"); if (t != null && t.exists()) { String lineDelimiter = StubUtility.getLineDelimiterUsed(project); t.createField("private final static QName _" + c.name() + "_QNAME= new QName(\"" + ec.getElementType().getNamespaceURI() + "\", \"" + ec.getElementType().getLocalPart() + "\");" + lineDelimiter, null, false, pm); t.createMethod(generateFirstMethod(c, lineDelimiter), null, false, pm); t.createMethod(generateSecondMethod(ec, lineDelimiter), null, false, pm); } else throw new JavaModelException( new Exception("Error ObjectFactory class does not exists in compilation unit"), JavaModelStatus.ERROR); return of; }
From source file:org.evosuite.eclipse.popup.actions.TestExtensionJob.java
License:Open Source License
@Override protected IStatus run(IProgressMonitor monitor) { IStatus status = super.run(monitor); newTests.clear();// www .jav a 2 s . c o m IJavaElement element = JavaCore.create(target); if (element.getElementType() == IJavaElement.COMPILATION_UNIT) { ICompilationUnit compilationUnit = (ICompilationUnit) element; CodeFormatter formatter = ToolFactory.createCodeFormatter(null); try { if (compilationUnit.getTypes().length == 0) { System.out.println("The compilation unit is empty :|"); return status; } IType classType = compilationUnit.getTypes()[0]; // new tests loadTestSuiteContent(getTestClassName()); for (ImportDeclaration newImport : newImports) { if (!hasImport(compilationUnit, newImport)) { int flag = newImport.isStatic() ? Flags.AccStatic : Flags.AccDefault; String strImport = newImport.getName().toString(); // Names of onDemand import declarations do not contain the '*' if (newImport.isOnDemand()) strImport += ".*"; compilationUnit.createImport(strImport, null, flag, null); } } for (MethodDeclaration newMethod : newMethods) { if (hasMethod(classType, newMethod.getName().toString())) { System.out.println("Test suite already contains method called: " + newMethod.getName()); int num = 1; newMethod.setName( newMethod.getAST().newSimpleName(newMethod.getName().toString() + "_" + num)); while (hasMethod(classType, newMethod.getName().toString())) { num += 1; String name = newMethod.getName().toString(); newMethod.setName(newMethod.getAST() .newSimpleName(name.substring(0, name.length() - 2) + "_" + num)); } } String testContent = newMethod.toString(); IMethod methodToAdd = classType.createMethod(testContent, null, false, new NullProgressMonitor()); ISourceRange range = methodToAdd.getSourceRange(); TextEdit indent_edit = formatter.format(CodeFormatter.K_COMPILATION_UNIT, classType.getCompilationUnit().getSource(), range.getOffset(), range.getLength(), 0, null); classType.getCompilationUnit().applyTextEdit(indent_edit, null); newTests.add(newMethod.getName().toString()); } classType.getCompilationUnit().commitWorkingCopy(false, null); // write markers writeMarkersExtendedTestSuite(); } catch (JavaModelException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return status; }
From source file:org.jboss.tools.common.refactoring.MarkerResolutionUtils.java
License:Open Source License
public static boolean addImport(String qualifiedName, ICompilationUnit compilationUnit, boolean staticFlag, MultiTextEdit rootEdit) throws JavaModelException { if (primitives.contains(qualifiedName) || qualifiedName.indexOf(DOT) < 0) return false; if (qualifiedName != null) { String shortName = getShortName(qualifiedName); IPackageDeclaration[] packages = compilationUnit.getPackageDeclarations(); // local classes do not need to be imported String typePackage = qualifiedName.substring(0, qualifiedName.lastIndexOf(DOT)); for (IPackageDeclaration packageDeclaration : packages) { if (packageDeclaration.getElementName().equals(typePackage)) return false; }/*from www . j av a2s .co m*/ for (IPackageDeclaration packageDeclaration : packages) { IType type = compilationUnit.getJavaProject() .findType(packageDeclaration.getElementName() + DOT + shortName); if (type != null && type.exists()) return true; } IImportDeclaration[] importDeclarations = compilationUnit.getImports(); for (IImportDeclaration importDeclaration : importDeclarations) { String importName = importDeclaration.getElementName(); String elementShort = getShortName(importName); if (importDeclaration.isOnDemand()) { int importLastDot = importName.lastIndexOf(DOT); if (importLastDot == -1) return false; // invalid import declaration int elementLastDot = qualifiedName.lastIndexOf(DOT); if (elementLastDot == -1) return false; // invalid import declaration if (qualifiedName.substring(0, elementLastDot).equals(importName.substring(0, importLastDot))) return false; } if (importName.equals(qualifiedName)) return false; if (elementShort.equals(shortName)) return true; } if (rootEdit == null) { if (staticFlag) { compilationUnit.createImport(qualifiedName, null, Flags.AccStatic, new NullProgressMonitor()); } else { compilationUnit.createImport(qualifiedName, null, new NullProgressMonitor()); } } else { String staticStr = ""; if (staticFlag) { staticStr = STATIC + SPACE; } String text = compilationUnit.findRecommendedLineSeparator() + IMPORT + SPACE + staticStr + qualifiedName + SEMICOLON; if (!isDuplicate(rootEdit, text)) { int importPosition = findPositionForImport(compilationUnit); TextEdit edit = new InsertEdit(importPosition, text); rootEdit.addChild(edit); } } } return false; }
From source file:org.openquark.cal.eclipse.embedded.contained.ContainedEditorProperties.java
License:Open Source License
protected void createStaticImport(ICompilationUnit unit, String requiredImport) { try {/* w ww .j av a 2s .c om*/ unit.createImport(requiredImport, null, Flags.AccStatic, null); } catch (JavaModelException e) { EmbeddedCALPlugin.logError("Error importing " + requiredImport, e); } }