当前位置: 首页>>代码示例>>Java>>正文


Java CompilationUnit.getTypes方法代码示例

本文整理汇总了Java中japa.parser.ast.CompilationUnit.getTypes方法的典型用法代码示例。如果您正苦于以下问题:Java CompilationUnit.getTypes方法的具体用法?Java CompilationUnit.getTypes怎么用?Java CompilationUnit.getTypes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在japa.parser.ast.CompilationUnit的用法示例。


在下文中一共展示了CompilationUnit.getTypes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: Fact

import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
public Fact(File file, CompilationUnit compilationUnit) throws FileNotFoundException {
    if (compilationUnit.getPackage() != null)
        packageName = compilationUnit.getPackage().getName().toString().replace("package", "");
    
    comment = extractTopComment(file);

    imports = new HashSet<String>();
    if (compilationUnit.getImports() != null)
        for (ImportDeclaration imp : compilationUnit.getImports()) {
            String str = imp.getName().toString();
            if (!imp.isAsterisk())
                str = str.substring(0, str.lastIndexOf("."));
            imports.add(str);
        }
        
    declarations = new ArrayList<Declaration>();
    if (compilationUnit.getTypes() != null)
        for (TypeDeclaration decl : compilationUnit.getTypes()) {
            if (decl instanceof ClassOrInterfaceDeclaration)
                declarations.add(new Declaration(decl));
        }
}
 
开发者ID:amritbhat786,项目名称:DocIT,代码行数:23,代码来源:Fact.java

示例2: locateTypeDeclaration

import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
/**
 * Searches a compilation unit and locates the declaration with the given
 * type's simple name.
 * 
 * @param compilationUnit to scan (required)
 * @param javaType the target to locate (required)
 * @return the located type declaration or null if it could not be found
 */
public static TypeDeclaration locateTypeDeclaration(
        final CompilationUnit compilationUnit, final JavaType javaType) {
    Validate.notNull(compilationUnit, "Compilation unit required");
    Validate.notNull(javaType, "Java type to search for required");
    if (compilationUnit.getTypes() == null) {
        return null;
    }
    for (final TypeDeclaration candidate : compilationUnit.getTypes()) {
        if (javaType.getSimpleTypeName().equals(candidate.getName())) {
            // We have the required type declaration
            return candidate;
        }
    }
    return null;
}
 
开发者ID:gvSIGAssociation,项目名称:gvnix1,代码行数:24,代码来源:JavaParserUtils.java

示例3: parseType

import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
private JType parseType(String type, JCodeModel codeModel) {
	final String text = "public class Ignored extends " + type + " {}";
	try {
		CompilationUnit compilationUnit = JavaParser.parse(
				new ByteArrayInputStream(text.getBytes("UTF-8")), "UTF-8");
		final List<TypeDeclaration> typeDeclarations = compilationUnit
				.getTypes();
		final TypeDeclaration typeDeclaration = typeDeclarations.get(0);
		final ClassOrInterfaceDeclaration classDeclaration = (ClassOrInterfaceDeclaration) typeDeclaration;
		final List<ClassOrInterfaceType> _extended = classDeclaration
				.getExtends();
		final ClassOrInterfaceType classOrInterfaceType = _extended.get(0);

		return classOrInterfaceType.accept(
				this.typeToJTypeConvertingVisitor, codeModel);
	} catch (ParseException pex) {
		throw new IllegalArgumentException(
				"Could not parse the type definition [" + type + "].", pex);
	} catch (UnsupportedEncodingException uex) {
		throw new UnsupportedOperationException(uex);
	}
}
 
开发者ID:highsource,项目名称:jaxb2-basics,代码行数:23,代码来源:JavaTypeParser.java

示例4: getNode

import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
public static Node getNode(CompilationUnit unit, String fqName) {
	String pkg = getQualifiedName(unit.getPackage().getName());
	if (!fqName.startsWith(pkg)) {
		return null;
	}
	if (fqName.equals(pkg)) {
		return unit.getPackage();
	}

	String name = fqName.substring(pkg.length() + 1);

	for (TypeDeclaration typeDecl : unit.getTypes()) {
		if (name.startsWith(typeDecl.getName())) {
			if (name.length() == typeDecl.getName().length()) {
				return typeDecl;
			} else {
				return getNode(typeDecl, name.substring(typeDecl.getName().length() + 1));
			}
		}
	}

	return null;
}
 
开发者ID:jedwards1211,项目名称:breakout,代码行数:24,代码来源:JavaParserUtils.java

示例5: find

import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
private static TypeDeclaration find( CompilationUnit compunit , String[ ] nestedTypeNames )
{
	for( TypeDeclaration decl : compunit.getTypes( ) )
	{
		if( decl.getName( ).equals( nestedTypeNames[ 0 ] ) )
		{
			if( nestedTypeNames.length == 1 )
			{
				return decl;
			}
			else
			{
				return find( decl , nestedTypeNames , 1 );
			}
		}
	}
	return null;
}
 
开发者ID:jedwards1211,项目名称:breakout,代码行数:19,代码来源:BuilderGenerator.java

示例6: visit

import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
public R visit(CompilationUnit n, A arg) {
    if (n.getPackage() != null) {
        n.getPackage().accept(this, arg);
    }
    if (n.getImports() != null) {
        for (ImportDeclaration i : n.getImports()) {
            i.accept(this, arg);
        }
    }
    if (n.getTypes() != null) {
        for (TypeDeclaration typeDeclaration : n.getTypes()) {
            typeDeclaration.accept(this, arg);
        }
    }
    return null;
}
 
开发者ID:rfw,项目名称:genja,代码行数:17,代码来源:GenericVisitorAdapter.java

示例7: visit

import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
public void visit(CompilationUnit n, Object arg) {
    if (n.getPackage() != null) {
        n.getPackage().accept(this, arg);
    }
    if (n.getImports() != null) {
        for (ImportDeclaration i : n.getImports()) {
            i.accept(this, arg);
        }
        printer.printLn();
    }
    if (n.getTypes() != null) {
        for (Iterator<TypeDeclaration> i = n.getTypes().iterator(); i.hasNext();) {
            i.next().accept(this, arg);
            printer.printLn();
            if (i.hasNext()) {
                printer.printLn();
            }
        }
    }
}
 
开发者ID:rfw,项目名称:genja,代码行数:21,代码来源:DumpVisitor.java

示例8: visit

import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
public Node visit(CompilationUnit n, A arg) {
    if (n.getPackage() != null) {
        n.setPackage((PackageDeclaration) n.getPackage().accept(this, arg));
    }
    List<ImportDeclaration> imports = n.getImports();
    if (imports != null) {
        for (int i = 0; i < imports.size(); i++) {
            imports.set(i, (ImportDeclaration) imports.get(i).accept(this, arg));
        }
        removeNulls(imports);
    }
    List<TypeDeclaration> types = n.getTypes();
    if (types != null) {
        for (int i = 0; i < types.size(); i++) {
            types.set(i, (TypeDeclaration) types.get(i).accept(this, arg));
        }
        removeNulls(types);
    }
    return n;
}
 
开发者ID:rfw,项目名称:genja,代码行数:21,代码来源:ModifierVisitorAdapter.java

示例9: changeMethods

import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
private static void changeMethods(CompilationUnit cu, String rootClass) {
    addImport(cu, rootClass);
    String rootClassShort = rootClass
            .substring(rootClass.lastIndexOf(".") + 1);

    List<TypeDeclaration> types = cu.getTypes();
    for (TypeDeclaration type : types) {
        List<BodyDeclaration> members = type.getMembers();
        for (BodyDeclaration member : members) {
            if (member instanceof MethodDeclaration) {
                MethodDeclaration method = (MethodDeclaration) member;
                if (isSetRootComponentMethod(method)) {
                    changeSetRootMethod(method, rootClassShort);
                }
            } else if (member instanceof FieldDeclaration) {
                FieldDeclaration field = (FieldDeclaration) member;
                if (isRootField(field)) {
                    changeRootField(field, rootClassShort);
                }
            }
        }
    }
}
 
开发者ID:ahn,项目名称:mideaas,代码行数:24,代码来源:JavaUtil.java

示例10: getJavaType

import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
public final JavaType getJavaType(final String fileIdentifier) {
    Validate.notBlank(fileIdentifier, "Compilation unit path required");
    Validate.isTrue(new File(fileIdentifier).exists(),
            "The file doesn't exist");
    Validate.isTrue(new File(fileIdentifier).isFile(),
            "The identifier doesn't represent a file");
    try {
        final File file = new File(fileIdentifier);
        String typeContents = "";
        try {
            typeContents = FileUtils.readFileToString(file);
        }
        catch (IOException ignored) {
        }
        if (StringUtils.isBlank(typeContents)) {
            return null;
        }
        final CompilationUnit compilationUnit = JavaParser
                .parse(new ByteArrayInputStream(typeContents.getBytes()));
        final String typeName = fileIdentifier.substring(
                fileIdentifier.lastIndexOf(File.separator) + 1,
                fileIdentifier.lastIndexOf("."));
        for (final TypeDeclaration typeDeclaration : compilationUnit
                .getTypes()) {
            if (typeName.equals(typeDeclaration.getName())) {
                return new JavaType(compilationUnit.getPackage().getName()
                        .getName()
                        + "." + typeDeclaration.getName());
            }
        }
        return null;
    }
    catch (final ParseException e) {
        throw new IllegalStateException("Failed to parse " + fileIdentifier
                + " : " + e.getMessage());
    }
}
 
开发者ID:gvSIGAssociation,项目名称:gvnix1,代码行数:38,代码来源:JavaParserTypeResolutionService.java

示例11: testParse

import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
public void testParse() throws Exception {

		final String text = "public class Dummy implements java.util.Comparator<java.lang.Integer>{}";

		final CompilationUnit compilationUnit = JavaParser.parse(
				new ByteArrayInputStream(text.getBytes("UTF-8")), "UTF-8");
		final List<TypeDeclaration> typeDeclarations = compilationUnit
				.getTypes();
		assertEquals(1, typeDeclarations.size());
		final TypeDeclaration typeDeclaration = typeDeclarations.get(0);
		assertTrue(typeDeclaration instanceof ClassOrInterfaceDeclaration);
		final ClassOrInterfaceDeclaration classDeclaration = (ClassOrInterfaceDeclaration) typeDeclaration;
		assertEquals("Dummy", classDeclaration.getName());
		final List<ClassOrInterfaceType> implementedInterfaces = classDeclaration
				.getImplements();
		assertEquals(1, implementedInterfaces.size());
		final ClassOrInterfaceType implementedInterface = implementedInterfaces
				.get(0);
		assertEquals("Comparator", implementedInterface.getName());
		final List<Type> typeArgs = implementedInterface.getTypeArgs();
		assertEquals(1, typeArgs.size());
		final Type typeArg = typeArgs.get(0);
		assertTrue(typeArg instanceof ReferenceType);
		final ReferenceType referenceTypeArg = (ReferenceType) typeArg;
		final Type referencedType = referenceTypeArg.getType();
		assertTrue(referencedType instanceof ClassOrInterfaceType);
		final ClassOrInterfaceType typeArgType = (ClassOrInterfaceType) referencedType;
		assertEquals("Integer", typeArgType.getName());

	}
 
开发者ID:highsource,项目名称:jaxb2-basics,代码行数:31,代码来源:JavaTypeParserTest.java

示例12: writeBuilder

import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
/**
 * Finds the java file a generated builder belongs in, and attempts to insert the builder code, or replace the existing auto-generated types and methods if
 * necessary.<br>
 * <i>Requres JavaParser 1.0.8 or greater.</i><br>
 * <br>
 * This method is crazy. Finding and modifying the file is complicated, and this method makes a backup, but who knows what could go wrong. USE AT YOUR OWN
 * RISK!
 * 
 * @param builder
 *            a builder generated by {@code #generateBuilders(Class...)}.
 * @throws Exception
 *             for any number of reasons...
 */
public static void writeBuilder( String builder ) throws Exception
{
	CompilationUnit compunit = JavaParser.parse( new ByteArrayInputStream( builder.getBytes( ) ) );
	
	for( TypeDeclaration typeDecl : compunit.getTypes( ) )
	{
		File javaFile = findJavaFile( typeDecl );
		if( javaFile != null )
		{
			List<String> lines = insertBuilder( typeDecl , javaFile );
			
			File backup = new File( javaFile + "_backup" );
			File newFile = new File( javaFile + "_new" );
			System.out.println( "Copying " + javaFile + " to " + backup );
			copy( javaFile , backup );
			System.out.println( "Writing " + newFile + "..." );
			write( lines , newFile );
			System.out.println( "Writing " + javaFile + "..." );
			write( lines , javaFile );
			System.out.println( "Deleting " + backup + "..." );
			backup.delete( );
			System.out.println( "Deleting " + newFile + "..." );
			newFile.delete( );
		}
	}
}
 
开发者ID:jedwards1211,项目名称:breakout,代码行数:40,代码来源:BuilderGenerator.java

示例13: visit

import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
public void visit(CompilationUnit n, A arg) {
    if (n.getPackage() != null) {
        n.getPackage().accept(this, arg);
    }
    if (n.getImports() != null) {
        for (ImportDeclaration i : n.getImports()) {
            i.accept(this, arg);
        }
    }
    if (n.getTypes() != null) {
        for (TypeDeclaration typeDeclaration : n.getTypes()) {
            typeDeclaration.accept(this, arg);
        }
    }
}
 
开发者ID:rfw,项目名称:genja,代码行数:16,代码来源:VoidVisitorAdapter.java

示例14: addTypeDeclaration

import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
/**
 * Adds the given type declaration to the compilation unit. The list of
 * types will be initialized if it is <code>null</code>.
 * 
 * @param cu
 *            compilation unit
 * @param type
 *            type declaration
 */
public static void addTypeDeclaration(CompilationUnit cu, TypeDeclaration type) {
    List<TypeDeclaration> types = cu.getTypes();
    if (types == null) {
        types = new ArrayList<TypeDeclaration>();
        cu.setTypes(types);
    }
    types.add(type);

}
 
开发者ID:rfw,项目名称:genja,代码行数:19,代码来源:ASTHelper.java

示例15: cunstructShirinked

import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
public static String cunstructShirinked(String original) throws ParseException {
	CompilationUnit cu = null;
	try {
		cu = JavaParser.parse(new ByteArrayInputStream(original.getBytes("UTF-8")), "UTF-8");
	} catch (UnsupportedEncodingException e) {
		throw new RuntimeException(e);
	}
	for (TypeDeclaration td : cu.getTypes())
		processType(td);
	original = cu.toString();
	original = shrink(original);
	return original;
}
 
开发者ID:kongbomb,项目名称:judgesubmit,代码行数:14,代码来源:JavaShrinker.java


注:本文中的japa.parser.ast.CompilationUnit.getTypes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。